#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <vector>
#include <map>
#include <algorithm>
#include <set>
#include <time.h>
#include <queue>
using namespace std;
int main () {
int k, n;
scanf("%d %d", &n, &k);
vector<int> a;
vector<bool> ok(100000000, true);
for (int i = 0; i < n; ++i) {
int x;
scanf("%d", &x);
a.push_back(x);
}
int next = 0;
int search = 0;
int64_t res = 0;
while (1) {
if (search >= n) {
printf("-1\n");
return 0;
}
if (next >= k) {
printf("%lld\n", res);
return 0;
}
if (ok[a[search]]) {
ok[a[search]] = false;
res += search - next;
++next;
}
else {
++search;
}
}
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 37 38 39 40 41 42 43 44 45 | #include <cstdio> #include <cstdlib> #include <cstdint> #include <vector> #include <map> #include <algorithm> #include <set> #include <time.h> #include <queue> using namespace std; int main () { int k, n; scanf("%d %d", &n, &k); vector<int> a; vector<bool> ok(100000000, true); for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); a.push_back(x); } int next = 0; int search = 0; int64_t res = 0; while (1) { if (search >= n) { printf("-1\n"); return 0; } if (next >= k) { printf("%lld\n", res); return 0; } if (ok[a[search]]) { ok[a[search]] = false; res += search - next; ++next; } else { ++search; } } return 0; } |
English