#include <iostream> #include <set> int main() { int result = 0; int found = 0; int pos = 0; int n, k, v; std::set<int> used; std::cin >> n >> k; for (int i = 0; i < n; i++) { std::cin >> v; if (found < k) { if (used.find(v) == used.end()) { used.insert(v); found += 1; result += i - pos; pos += 1; } } } if (found < k) { std::cout << -1; } else { std::cout << result; } 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 | #include <iostream> #include <set> int main() { int result = 0; int found = 0; int pos = 0; int n, k, v; std::set<int> used; std::cin >> n >> k; for (int i = 0; i < n; i++) { std::cin >> v; if (found < k) { if (used.find(v) == used.end()) { used.insert(v); found += 1; result += i - pos; pos += 1; } } } if (found < k) { std::cout << -1; } else { std::cout << result; } return 0; } |