#include <cstdio> #include <unordered_set> using namespace std; unordered_set<int> D; int main() { int n, k, c = 0, s = 0; scanf("%d %d", &n, &k); int A[n]; for (int i=0;i<n;i++) scanf("%d", &A[i]); for (int i=0;i<n;i++) { if (D.count(A[i]) == 0) { D.insert(A[i]); s += i-c; c++; if (c == k) { printf("%d", s); return 0; } } } printf("-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 | #include <cstdio> #include <unordered_set> using namespace std; unordered_set<int> D; int main() { int n, k, c = 0, s = 0; scanf("%d %d", &n, &k); int A[n]; for (int i=0;i<n;i++) scanf("%d", &A[i]); for (int i=0;i<n;i++) { if (D.count(A[i]) == 0) { D.insert(A[i]); s += i-c; c++; if (c == k) { printf("%d", s); return 0; } } } printf("-1"); } |