// Created by Michal Kowalski on 07/12/2021.
//
#include <iostream>
#include <set>
int N,K;
int main() {
scanf("%d %d",&N,&K);
std::set<int> S;
int o = 0;
long long steps = 0;
for(int i=0;i<N;++i) {
int a;
scanf("%d", &a);
// not found
if (S.count(a) == 0) {
S.insert(a);
steps += (long long)i-o;
++o;
}
if (S.size() == K) {
printf("%lld\n",steps);
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 | // Created by Michal Kowalski on 07/12/2021. // #include <iostream> #include <set> int N,K; int main() { scanf("%d %d",&N,&K); std::set<int> S; int o = 0; long long steps = 0; for(int i=0;i<N;++i) { int a; scanf("%d", &a); // not found if (S.count(a) == 0) { S.insert(a); steps += (long long)i-o; ++o; } if (S.size() == K) { printf("%lld\n",steps); return 0; } } printf("-1\n"); return 0; } |
English