#include <iostream> using namespace std; int n, k; int but[1000000]; bool is_at_start[1000000]; int at_start; long long res, final_res; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; at_start = 0; res = 0; final_res = -1; for(int i = 0; i < n; i++) { cin>>but[i]; is_at_start[i+1] = false; } for(int i = 0; i < n && at_start < k; i++) { if(!is_at_start[but[i]]) { res += (i - at_start); at_start++; is_at_start[but[i]] = true; } } if(at_start == k) final_res = res; cout<<final_res; 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 31 32 33 34 | #include <iostream> using namespace std; int n, k; int but[1000000]; bool is_at_start[1000000]; int at_start; long long res, final_res; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; at_start = 0; res = 0; final_res = -1; for(int i = 0; i < n; i++) { cin>>but[i]; is_at_start[i+1] = false; } for(int i = 0; i < n && at_start < k; i++) { if(!is_at_start[but[i]]) { res += (i - at_start); at_start++; is_at_start[but[i]] = true; } } if(at_start == k) final_res = res; cout<<final_res; return 0; } |