#include <bits/stdc++.h> int main() { using namespace std; int n, k; cin >> n >> k; vector<int> brand(n); for (int i = 0; i < n; ++i) { cin >> brand[i]; } unordered_set<int> seen; seen.insert(brand[0]); int to_skip = 0; int unique = 1; long long swaps = 0; for (int i = 1; i < n; ++i) { if (seen.count(brand[i])) { ++to_skip; } else { swaps += to_skip; ++unique; if (unique == k) break; } } cout << (unique == k ? swaps : -1) << 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 31 32 33 34 35 36 | #include <bits/stdc++.h> int main() { using namespace std; int n, k; cin >> n >> k; vector<int> brand(n); for (int i = 0; i < n; ++i) { cin >> brand[i]; } unordered_set<int> seen; seen.insert(brand[0]); int to_skip = 0; int unique = 1; long long swaps = 0; for (int i = 1; i < n; ++i) { if (seen.count(brand[i])) { ++to_skip; } else { swaps += to_skip; ++unique; if (unique == k) break; } } cout << (unique == k ? swaps : -1) << endl; return 0; } |