#include <bits/stdc++.h>
using namespace std;
#define MAX_N 500007
int n, k;
bool visited[MAX_N];
int ora[MAX_N];
long long resValue, curPos;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> ora[i];
}
curPos = 0;
for (long long i = 0; i < n; i++) {
if (!visited[ora[i]]) {
visited[ora[i]] = true;
resValue += i - curPos;
curPos++;
}
if (curPos >= k) {
break;
}
}
if (curPos >= k) {
cout << resValue;
}
else {
cout << "-1";
}
}
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 | #include <bits/stdc++.h> using namespace std; #define MAX_N 500007 int n, k; bool visited[MAX_N]; int ora[MAX_N]; long long resValue, curPos; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> ora[i]; } curPos = 0; for (long long i = 0; i < n; i++) { if (!visited[ora[i]]) { visited[ora[i]] = true; resValue += i - curPos; curPos++; } if (curPos >= k) { break; } } if (curPos >= k) { cout << resValue; } else { cout << "-1"; } } |
English