#include <iostream>
#include <set>
#include <algorithm>
int main() {
std::ios::sync_with_stdio(false);
int k, n;
std::cin >> n >> k;
std::set<int> v;
int res = 0;
int count = 0;
bool end = false;
for (int i = 0 ; i < n ; ++i) {
int x;
std::cin >> x;
if (!end && (v.find(x) == v.end())) {
res += i - count;
count += 1;
v.insert(x);
}
if (count == k) end = true;
}
if (end) {
std::cout << res;
} else {
std::cout << -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 27 28 29 30 | #include <iostream> #include <set> #include <algorithm> int main() { std::ios::sync_with_stdio(false); int k, n; std::cin >> n >> k; std::set<int> v; int res = 0; int count = 0; bool end = false; for (int i = 0 ; i < n ; ++i) { int x; std::cin >> x; if (!end && (v.find(x) == v.end())) { res += i - count; count += 1; v.insert(x); } if (count == k) end = true; } if (end) { std::cout << res; } else { std::cout << -1; } return 0; } |
English