#include <iostream>
#include <vector>
#include <algorithm>
#include <unordered_set>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
unordered_set<int> values;
unordered_set<int> diff_so_far;
int n, k;
cin >> n >> k;
vector<int> t(n, 0);
for (int i = 0; i < n; i++) {
cin >> t[i];
values.insert(t[i]);
}
int diff = values.size();
if (diff < k) {
cout << -1;
return 0;
}
vector<int> positions;
bool found = 0;
int pos = 0;
for (int i = 0; i < n; i++) {
if (diff_so_far.find(t[i]) == diff_so_far.end()) {
positions.push_back(i);
diff_so_far.insert(t[i]);
if (positions.size() == k) break;
if (!found) pos++;
}
else found = true;
}
pos = 0;
unsigned long long ans = 0;
for (auto x : positions) {
//cout << "x: " << x << " pos: " << pos << "\n";
ans += (x - pos);
pos++;
//cout << "ans: " << ans << endl;
}
cout << ans;
}
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | #include <iostream> #include <vector> #include <algorithm> #include <unordered_set> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); unordered_set<int> values; unordered_set<int> diff_so_far; int n, k; cin >> n >> k; vector<int> t(n, 0); for (int i = 0; i < n; i++) { cin >> t[i]; values.insert(t[i]); } int diff = values.size(); if (diff < k) { cout << -1; return 0; } vector<int> positions; bool found = 0; int pos = 0; for (int i = 0; i < n; i++) { if (diff_so_far.find(t[i]) == diff_so_far.end()) { positions.push_back(i); diff_so_far.insert(t[i]); if (positions.size() == k) break; if (!found) pos++; } else found = true; } pos = 0; unsigned long long ans = 0; for (auto x : positions) { //cout << "x: " << x << " pos: " << pos << "\n"; ans += (x - pos); pos++; //cout << "ans: " << ans << endl; } cout << ans; } |
English