#include <stdio.h>
#include <set>
int main(){
int n, k, b;
long long int res = 0;
std::set<int> unikalne;
int ostatnieMiejsce = -1;
scanf("%d %d", &n, &k);
for(int i = 0; i < n && (int)unikalne.size() < k; ++i){
scanf("%d", &b);
if (unikalne.find(b) == unikalne.end()){
unikalne.insert(b);
if (ostatnieMiejsce != -1){
res += (i - ostatnieMiejsce);
++ostatnieMiejsce;
}
} else if (ostatnieMiejsce == -1){
ostatnieMiejsce = i;
}
}
printf("%lld\n", (int)unikalne.size() == k ? res : -1);
return 0;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #include <stdio.h> #include <set> int main(){ int n, k, b; long long int res = 0; std::set<int> unikalne; int ostatnieMiejsce = -1; scanf("%d %d", &n, &k); for(int i = 0; i < n && (int)unikalne.size() < k; ++i){ scanf("%d", &b); if (unikalne.find(b) == unikalne.end()){ unikalne.insert(b); if (ostatnieMiejsce != -1){ res += (i - ostatnieMiejsce); ++ostatnieMiejsce; } } else if (ostatnieMiejsce == -1){ ostatnieMiejsce = i; } } printf("%lld\n", (int)unikalne.size() == k ? res : -1); return 0; } |
English