#include <iostream> using namespace std; const int maxN = 500000; int count[maxN + 5]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin>>n>>k; int good = 0; int result = 0; int a; for(int i=1; i <= n; i++){ cin>>a; if(good < k && count[a] == 0){ result += (i - good - 1); good++; } count[a]++; } if(good == k) cout<<result<<"\n"; else cout<<"-1\n"; }
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 33 | #include <iostream> using namespace std; const int maxN = 500000; int count[maxN + 5]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n, k; cin>>n>>k; int good = 0; int result = 0; int a; for(int i=1; i <= n; i++){ cin>>a; if(good < k && count[a] == 0){ result += (i - good - 1); good++; } count[a]++; } if(good == k) cout<<result<<"\n"; else cout<<"-1\n"; } |