#include<bits/stdc++.h> using namespace std; bool set_contains(set<int> & my_set, int & value){ return my_set.find(value) != my_set.end(); } int main(){ ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; vector<int> bottles (n); for(int i=0; i<n; i++){ int bottle; cin >> bottle; bottles[i] = bottle; } set<int> used_type_of_bottles; int first_to_use_space = 0; long long result = 0; for(int i=0; i<n && used_type_of_bottles.size() < k; i++){ if(!set_contains(used_type_of_bottles, bottles[i])){ result += i-first_to_use_space; ++first_to_use_space; used_type_of_bottles.insert(bottles[i]); } } if(used_type_of_bottles.size() < k){ cout << -1; } else{ cout << result; } }
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 35 36 37 | #include<bits/stdc++.h> using namespace std; bool set_contains(set<int> & my_set, int & value){ return my_set.find(value) != my_set.end(); } int main(){ ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; vector<int> bottles (n); for(int i=0; i<n; i++){ int bottle; cin >> bottle; bottles[i] = bottle; } set<int> used_type_of_bottles; int first_to_use_space = 0; long long result = 0; for(int i=0; i<n && used_type_of_bottles.size() < k; i++){ if(!set_contains(used_type_of_bottles, bottles[i])){ result += i-first_to_use_space; ++first_to_use_space; used_type_of_bottles.insert(bottles[i]); } } if(used_type_of_bottles.size() < k){ cout << -1; } else{ cout << result; } } |