#include<cstdio> using namespace std; int butelki[500009]; bool used[500009]; int main() { int n,k; scanf("%d %d", &n, &k); for(int i = 0; i < n; i++) { scanf("%d", &butelki[i]); } int act = 1; used[butelki[0]] = true; long long moves = 0; for(int i = 1; i < n; i++) { if (act == k) break; if(used[butelki[i]]) continue; moves += (i - act); act++; used[butelki[i]] = true; } if (act == k) printf("%lld", moves); else printf("-1"); }
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 | #include<cstdio> using namespace std; int butelki[500009]; bool used[500009]; int main() { int n,k; scanf("%d %d", &n, &k); for(int i = 0; i < n; i++) { scanf("%d", &butelki[i]); } int act = 1; used[butelki[0]] = true; long long moves = 0; for(int i = 1; i < n; i++) { if (act == k) break; if(used[butelki[i]]) continue; moves += (i - act); act++; used[butelki[i]] = true; } if (act == k) printf("%lld", moves); else printf("-1"); } |