#include <cstdio> #include <algorithm> #include <vector> #include <set> using namespace std; typedef long long LL; #define M 500007 int n, k; int t[M]; bool exists[M]; int main() { scanf("%d%d", &n, &k); for (int i=0; i<n; i++) scanf("%d", &t[i]); int last=0, different=0; LL result=0; for (; last<n && different<k; last++) { if (exists[t[last]]) result+=(LL)(k-different); else exists[t[last]]=true, different++; } if (different<k) { printf("-1\n"); return 0; } printf("%lld\n", result); 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 | #include <cstdio> #include <algorithm> #include <vector> #include <set> using namespace std; typedef long long LL; #define M 500007 int n, k; int t[M]; bool exists[M]; int main() { scanf("%d%d", &n, &k); for (int i=0; i<n; i++) scanf("%d", &t[i]); int last=0, different=0; LL result=0; for (; last<n && different<k; last++) { if (exists[t[last]]) result+=(LL)(k-different); else exists[t[last]]=true, different++; } if (different<k) { printf("-1\n"); return 0; } printf("%lld\n", result); return 0; } |