#include <stdio.h>
using ll=long long;
const int C=1200001;
int count[C], a[C], position[C];
int main(){
int i, n, k, j=0;
ll counter=0, res=0;
scanf ("%d %d", &n, &k);
for (i=0; i<n; i++) scanf ("%d", &a[i]);
for (i=0; i<n; i++){
if (count[a[i]] == 0){
count[a[i]] = 1;
counter++;
position[i]=k;
continue;
}
else position[i] = counter;
}
if (counter < k){
printf ("-1\n");
return 0;
}
for (i=0; i<n; i++){
if (position[i]<k) res += (k-position[i]); //O tyle przesunąć frajera do przodu
}
printf ("%lld\n", res);
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 | #include <stdio.h> using ll=long long; const int C=1200001; int count[C], a[C], position[C]; int main(){ int i, n, k, j=0; ll counter=0, res=0; scanf ("%d %d", &n, &k); for (i=0; i<n; i++) scanf ("%d", &a[i]); for (i=0; i<n; i++){ if (count[a[i]] == 0){ count[a[i]] = 1; counter++; position[i]=k; continue; } else position[i] = counter; } if (counter < k){ printf ("-1\n"); return 0; } for (i=0; i<n; i++){ if (position[i]<k) res += (k-position[i]); //O tyle przesunąć frajera do przodu } printf ("%lld\n", res); return 0;} |
English