#include <cstdlib>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <cstdio>
#include <iostream>
using namespace std;
int nn[500005];
bool kk[500005];
int main()
{
for(int i=0; i<500005; i++){
nn[i]=0;
kk[i]=false;
}
int n, k;
int c=0, d=0, x;
scanf("%d %d", &n, &k);
for(int i=1; i<=n; i++){
scanf("%d", &x);
if(!kk[x]){
kk[x]=true;
d++;
c+=(i-d);
if(d==k){
printf("%d\n", c);
return 0;
}
}
}
printf("-1\n");
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 | #include <cstdlib> #include <cstring> #include <string> #include <vector> #include <algorithm> #include <map> #include <cstdio> #include <iostream> using namespace std; int nn[500005]; bool kk[500005]; int main() { for(int i=0; i<500005; i++){ nn[i]=0; kk[i]=false; } int n, k; int c=0, d=0, x; scanf("%d %d", &n, &k); for(int i=1; i<=n; i++){ scanf("%d", &x); if(!kk[x]){ kk[x]=true; d++; c+=(i-d); if(d==k){ printf("%d\n", c); return 0; } } } printf("-1\n"); return 0; } |
English