#include <bits/stdc++.h> using namespace std; #if DEBUG #define LOG(...) printf(__VA_ARGS__) #else #define LOG(...) #endif int n, k; int main() { scanf("%d %d", &n, &k); vector<int> v(n); for (int i = 0; i < n; ++i) scanf("%d", &v[i]); vector<bool> dis(n + 1); int dupi = -1; for (int i = 0; i < k; ++i) { if (dis[v[i]]) { dupi = i; break; } dis[v[i]] = true; } if (dupi == -1) { printf("0\n"); return 0; } int ness = k - dupi; LOG("ness: %d\n", ness); int has = 0; int out = 0; for (int i = dupi + 1; i < n; ++i) { if (!dis[v[i]]) { out += i - dupi - has; has++; if (has == ness) break; } dis[v[i]] = true; } if (has == ness) printf("%d\n", out); else printf("-1\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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | #include <bits/stdc++.h> using namespace std; #if DEBUG #define LOG(...) printf(__VA_ARGS__) #else #define LOG(...) #endif int n, k; int main() { scanf("%d %d", &n, &k); vector<int> v(n); for (int i = 0; i < n; ++i) scanf("%d", &v[i]); vector<bool> dis(n + 1); int dupi = -1; for (int i = 0; i < k; ++i) { if (dis[v[i]]) { dupi = i; break; } dis[v[i]] = true; } if (dupi == -1) { printf("0\n"); return 0; } int ness = k - dupi; LOG("ness: %d\n", ness); int has = 0; int out = 0; for (int i = dupi + 1; i < n; ++i) { if (!dis[v[i]]) { out += i - dupi - has; has++; if (has == ness) break; } dis[v[i]] = true; } if (has == ness) printf("%d\n", out); else printf("-1\n"); } |