#include <bits/stdc++.h> using namespace std; typedef long long LL; int main() { ios_base::sync_with_stdio(false); int n, k, a; cin >> n >> k; unordered_set<int> diffset; int lastdiff = 0; LL ct = 0; for(int i = 0; i < n && lastdiff < k; ++i) { cin >> a; if(diffset.count(a) > 0) { continue; } else { ct += i-lastdiff; lastdiff++; diffset.insert(a); } } if(lastdiff < k) cout << -1 << endl; else cout << ct << endl; }
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 <bits/stdc++.h> using namespace std; typedef long long LL; int main() { ios_base::sync_with_stdio(false); int n, k, a; cin >> n >> k; unordered_set<int> diffset; int lastdiff = 0; LL ct = 0; for(int i = 0; i < n && lastdiff < k; ++i) { cin >> a; if(diffset.count(a) > 0) { continue; } else { ct += i-lastdiff; lastdiff++; diffset.insert(a); } } if(lastdiff < k) cout << -1 << endl; else cout << ct << endl; } |