#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, k; cin >> n >> k;
int count_unique = 0;
long long res = 0;
vector<bool> bset(n+1);
int leftmost_free = 0;
vector<int> free_positions;
for (int i = 0; i < k; ++i) {
int a; cin >> a;
if (bset[a])
free_positions.push_back(i);
else bset[a] = true;
}
const int sz = free_positions.size();
auto prefix_free = [&leftmost_free, &sz](){ return leftmost_free < sz; };
for (int i = k; i < n and prefix_free(); ++i) {
int a; cin >> a;
if (not bset[a]) {
res += i - free_positions[leftmost_free++];
bset[a] = true;
}
}
if (prefix_free())
cout << -1;
else
cout << res;
}
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> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; int count_unique = 0; long long res = 0; vector<bool> bset(n+1); int leftmost_free = 0; vector<int> free_positions; for (int i = 0; i < k; ++i) { int a; cin >> a; if (bset[a]) free_positions.push_back(i); else bset[a] = true; } const int sz = free_positions.size(); auto prefix_free = [&leftmost_free, &sz](){ return leftmost_free < sz; }; for (int i = k; i < n and prefix_free(); ++i) { int a; cin >> a; if (not bset[a]) { res += i - free_positions[leftmost_free++]; bset[a] = true; } } if (prefix_free()) cout << -1; else cout << res; } |
English