#include <iostream> using namespace std; typedef long long ll; bool seen[500000]; int main() { ios::sync_with_stdio(false); cin.tie(); int n, k; cin >> n >> k; ll swaps = 0; int unique = 0; for (int i = 0; i < n && unique < k; i++) { int a; cin >> a; a--; if (!seen[a]) { seen[a] = true; swaps += i - unique; unique++; } } if (unique != k) { cout << "-1\n"; } else { cout << swaps << '\n'; } }
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 | #include <iostream> using namespace std; typedef long long ll; bool seen[500000]; int main() { ios::sync_with_stdio(false); cin.tie(); int n, k; cin >> n >> k; ll swaps = 0; int unique = 0; for (int i = 0; i < n && unique < k; i++) { int a; cin >> a; a--; if (!seen[a]) { seen[a] = true; swaps += i - unique; unique++; } } if (unique != k) { cout << "-1\n"; } else { cout << swaps << '\n'; } } |