#include <iostream> #include <string> #include <vector> #include <set> using namespace std; int main() { int n, k, e; cin >> n >> k; set<int> s; int current_index = 0; long moves = 0; for(int i = 0; i < n; i++) { cin >> e; if (s.find(e) == s.end()) { s.emplace(e); moves += (i-current_index); current_index++; if (current_index == k) { cout << moves << endl; return 0; } } } cout << -1 << endl; 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 <iostream> #include <string> #include <vector> #include <set> using namespace std; int main() { int n, k, e; cin >> n >> k; set<int> s; int current_index = 0; long moves = 0; for(int i = 0; i < n; i++) { cin >> e; if (s.find(e) == s.end()) { s.emplace(e); moves += (i-current_index); current_index++; if (current_index == k) { cout << moves << endl; return 0; } } } cout << -1 << endl; return 0; } |