#include<bits/stdc++.h> using namespace std; int main(){ int n, k; cin>>n>>k; int A[n+1], Z[n+1]; for (int i = 1; i <= n; i++){ cin>>A[i]; Z[i] = 0; } int wynik = 0; int poz = 2, akt = 2; Z[A[1]] = 1; while (akt <= n && poz <= k){ while (akt <= n && Z[A[akt]]) akt++; if (akt <= n){ wynik += akt-poz; poz++; Z[A[akt]] = 1; } } if (poz <= k) cout<<-1<<endl; else cout<<wynik<<endl; } //5 3 3 3 3 1 2 //3 2 1 1 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<bits/stdc++.h> using namespace std; int main(){ int n, k; cin>>n>>k; int A[n+1], Z[n+1]; for (int i = 1; i <= n; i++){ cin>>A[i]; Z[i] = 0; } int wynik = 0; int poz = 2, akt = 2; Z[A[1]] = 1; while (akt <= n && poz <= k){ while (akt <= n && Z[A[akt]]) akt++; if (akt <= n){ wynik += akt-poz; poz++; Z[A[akt]] = 1; } } if (poz <= k) cout<<-1<<endl; else cout<<wynik<<endl; } //5 3 3 3 3 1 2 //3 2 1 1 1 |