#include <iostream> #include <vector> #include <unordered_set> using std::vector; using std::unordered_set; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; int k; std::cin >> n; std:: cin >> k; unordered_set<int> firmy{}; vector<int> butelki(n, -1); for (int i = 0; i < n; ++i) { int b; std::cin >> b; butelki[i] = b; firmy.insert(b); } if (firmy.size() < k) { std::cout << -1 << "\n"; return 0; } int t = 0; int r = 0; unordered_set<int> rozne_pref{}; for (int i = 0; i < n; ++i) { if (r == k) { break; } if (rozne_pref.find(butelki[i]) != rozne_pref.end()) { continue; } t += i - r; rozne_pref.insert(butelki[i]); ++r; } std::cout << t << "\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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include <iostream> #include <vector> #include <unordered_set> using std::vector; using std::unordered_set; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(NULL); int n; int k; std::cin >> n; std:: cin >> k; unordered_set<int> firmy{}; vector<int> butelki(n, -1); for (int i = 0; i < n; ++i) { int b; std::cin >> b; butelki[i] = b; firmy.insert(b); } if (firmy.size() < k) { std::cout << -1 << "\n"; return 0; } int t = 0; int r = 0; unordered_set<int> rozne_pref{}; for (int i = 0; i < n; ++i) { if (r == k) { break; } if (rozne_pref.find(butelki[i]) != rozne_pref.end()) { continue; } t += i - r; rozne_pref.insert(butelki[i]); ++r; } std::cout << t << "\n"; return 0; } |