#include <iostream> #include <set> int main() { int n, k; std::cin >> n >> k; std::set<int> seen; int how_many = 0; long long result = 0; for (int i = 0; i < n; i++) { int x; std::cin >> x; if (seen.find(x) == seen.end()) { result += (i - how_many); ++how_many; } seen.insert(x); if (seen.size() == k) { std::cout << result << '\n'; return 0; } } std::cout << "-1\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 | #include <iostream> #include <set> int main() { int n, k; std::cin >> n >> k; std::set<int> seen; int how_many = 0; long long result = 0; for (int i = 0; i < n; i++) { int x; std::cin >> x; if (seen.find(x) == seen.end()) { result += (i - how_many); ++how_many; } seen.insert(x); if (seen.size() == k) { std::cout << result << '\n'; return 0; } } std::cout << "-1\n"; return 0; } |