#include <iostream>
#include <unordered_set>
using namespace std;
int n,k,x;
long long out;
unordered_set<int> s;
int main() {
cin >> n >> k;
for(int i=0; i<n; i++) {
cin >> x;
if(s.find(x) == s.end()) {
out += i-s.size();
s.insert(x);
}
if(s.size() == k) break;
}
if(s.size() == k) cout << out;
else cout << -1;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | #include <iostream> #include <unordered_set> using namespace std; int n,k,x; long long out; unordered_set<int> s; int main() { cin >> n >> k; for(int i=0; i<n; i++) { cin >> x; if(s.find(x) == s.end()) { out += i-s.size(); s.insert(x); } if(s.size() == k) break; } if(s.size() == k) cout << out; else cout << -1; } |
English