#include <iostream> #include <map> #include <vector> typedef long long ll; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int n, k; std::cin >> n >> k; std::map<int, bool> m; ll ans = 0; int cnt = 0; for (int i = 0; i < n; i++) { int x; std::cin >> x; if (!m[x]) { m[x] = true; ans += (ll) i - cnt; cnt++; if (cnt >= k) break; } } if (cnt < k) std::cout << "-1\n"; else std::cout << ans << '\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 | #include <iostream> #include <map> #include <vector> typedef long long ll; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); int n, k; std::cin >> n >> k; std::map<int, bool> m; ll ans = 0; int cnt = 0; for (int i = 0; i < n; i++) { int x; std::cin >> x; if (!m[x]) { m[x] = true; ans += (ll) i - cnt; cnt++; if (cnt >= k) break; } } if (cnt < k) std::cout << "-1\n"; else std::cout << ans << '\n'; return 0; } |