#include <iostream>
#include <unordered_map>
int main() {
int n, k, x, pos, off;
long long res = 0LL;
std::unordered_map<int, int> data;
std::cin >> n >> k;
pos = 0;
off = 0;
while(pos < n) {
std::cin >> x;
auto found = data.find(x);
if(found == data.end()) {
data[x] = 0;
} else {
if(data.size() < (long unsigned int)k) {
res += k-pos+off;
off++;
}
}
data[x] += 1;
pos++;
}
if(data.size() < (long unsigned int)k) {
res = -1;
}
std::cout << res << std::endl;
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 27 28 29 30 | #include <iostream> #include <unordered_map> int main() { int n, k, x, pos, off; long long res = 0LL; std::unordered_map<int, int> data; std::cin >> n >> k; pos = 0; off = 0; while(pos < n) { std::cin >> x; auto found = data.find(x); if(found == data.end()) { data[x] = 0; } else { if(data.size() < (long unsigned int)k) { res += k-pos+off; off++; } } data[x] += 1; pos++; } if(data.size() < (long unsigned int)k) { res = -1; } std::cout << res << std::endl; return 0; } |
English