#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 7;
bool v[N];
int a[N];
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n,k;
cin >> n >> k;
set<int> s;
for (int i = 0; i < n; i++){
cin >> a[i];
s.insert(a[i]);
}
if (s.size() < k) {
cout << "-1\n";
return 0;
}
long long ans = 0;
long long pos_cor = 0;
for (long long i = 0; i < n; i++){
if (!v[a[i]]){
ans += (i - pos_cor);
pos_cor++;
v[a[i]] = true;
}
}
cout << ans << "\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 29 30 31 32 33 34 35 36 | #include <bits/stdc++.h> using namespace std; const int N = 5e5 + 7; bool v[N]; int a[N]; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n,k; cin >> n >> k; set<int> s; for (int i = 0; i < n; i++){ cin >> a[i]; s.insert(a[i]); } if (s.size() < k) { cout << "-1\n"; return 0; } long long ans = 0; long long pos_cor = 0; for (long long i = 0; i < n; i++){ if (!v[a[i]]){ ans += (i - pos_cor); pos_cor++; v[a[i]] = true; } } cout << ans << "\n"; } |
English