#include <iostream> #include <unordered_set> using namespace std; int main() { long long n, k; cin >> n >> k; std::unordered_set<long long> brands; long long res = 0; for (int i = 0; i < n; ++i) { long long brand; cin >> brand; if (brands.find(brand) == end(brands)) { brands.insert(brand); res += i; if (size(brands) == k) break; } } if (size(brands) < k) { cout << -1; return 0; } cout << res - (k * (k - 1) / 2); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <iostream> #include <unordered_set> using namespace std; int main() { long long n, k; cin >> n >> k; std::unordered_set<long long> brands; long long res = 0; for (int i = 0; i < n; ++i) { long long brand; cin >> brand; if (brands.find(brand) == end(brands)) { brands.insert(brand); res += i; if (size(brands) == k) break; } } if (size(brands) < k) { cout << -1; return 0; } cout << res - (k * (k - 1) / 2); } |