#include <bits/stdc++.h> using namespace std; const int N = 500'007; int n, k; int in[N]; bool was[N]; int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%d", &in[i]); } int picked = 0; long long ans = 0; for (int i = 1; i <= n; ++i) { int v = in[i]; if (!was[v]) { picked++; was[v] = true; } else { ans += k - picked; } if (picked == k) { break; } } if (picked < k) { ans = -1; } printf("%lld\n", ans); 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 | #include <bits/stdc++.h> using namespace std; const int N = 500'007; int n, k; int in[N]; bool was[N]; int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= n; ++i) { scanf("%d", &in[i]); } int picked = 0; long long ans = 0; for (int i = 1; i <= n; ++i) { int v = in[i]; if (!was[v]) { picked++; was[v] = true; } else { ans += k - picked; } if (picked == k) { break; } } if (picked < k) { ans = -1; } printf("%lld\n", ans); return 0; } |