#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int n, k; long result = 0; cin >> n >> k; vector<int> tab(n); vector<int> found; for(int i = 0; i < n; i++) { cin >> tab[i]; } found.push_back(tab[0]); for(int i = 1; i < n; i++) { if(std::find(found.begin(), found.end(), tab[i]) != found.end()) { /* found contains element */ continue; } else { /* vector found does not contain it yet - new uniique element */ found.push_back(tab[i]); result += i+1 - found.size(); //cout << i << " " << tab[i] << " " << found.size() << endl; if(found.size() == k) break; } } if(found.size() == k) { cout << result; } else { cout << -1; } 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ int n, k; long result = 0; cin >> n >> k; vector<int> tab(n); vector<int> found; for(int i = 0; i < n; i++) { cin >> tab[i]; } found.push_back(tab[0]); for(int i = 1; i < n; i++) { if(std::find(found.begin(), found.end(), tab[i]) != found.end()) { /* found contains element */ continue; } else { /* vector found does not contain it yet - new uniique element */ found.push_back(tab[i]); result += i+1 - found.size(); //cout << i << " " << tab[i] << " " << found.size() << endl; if(found.size() == k) break; } } if(found.size() == k) { cout << result; } else { cout << -1; } return 0; } |