#include <bits/stdc++.h> using namespace std; int cache[500001]; int main(){ int n, k, temp; cin >> n >> k; long seconds = 0L; int iter = 0; for(int i = 0; i < n; ++i){ if(iter >= k){ break; } cin >> temp; if(cache[temp] == 0){ seconds += (i - iter); ++iter; cache[temp] = 1; } } if(iter < k){ cout << -1 << '\n'; } else{ cout << seconds << '\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 | #include <bits/stdc++.h> using namespace std; int cache[500001]; int main(){ int n, k, temp; cin >> n >> k; long seconds = 0L; int iter = 0; for(int i = 0; i < n; ++i){ if(iter >= k){ break; } cin >> temp; if(cache[temp] == 0){ seconds += (i - iter); ++iter; cache[temp] = 1; } } if(iter < k){ cout << -1 << '\n'; } else{ cout << seconds << '\n'; } return 0; } |