#include<bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; int tab[500009], col[500009]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k, a = 1, wyn = 0; cin>>n>>k; for(int i=1; i<=n; i++) cin>>tab[i]; for(int i=1; i<=n; i++) { if(col[tab[i]] == 0) { col[tab[i]] = 1; wyn += i - a; a++; if(a == k+1) break; } } if(a < k+1) cout<<-1; else cout<<wyn; 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<bits/stdc++.h> #define fi first #define se second #define pb push_back using namespace std; int tab[500009], col[500009]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k, a = 1, wyn = 0; cin>>n>>k; for(int i=1; i<=n; i++) cin>>tab[i]; for(int i=1; i<=n; i++) { if(col[tab[i]] == 0) { col[tab[i]] = 1; wyn += i - a; a++; if(a == k+1) break; } } if(a < k+1) cout<<-1; else cout<<wyn; return 0; } |