#include <iostream> #include <queue> using namespace std; int main() { long long n, k, c = 0, q, u[500005] = {0}, r = 0; cin>>n>>k; for (long long i = 0; i < n; ++i) { cin>>q; if (u[q] == 0) { u[q] = 1; ++c; r += i - c + 1; if (c == k) break; } } cout<<(c < k ? -1 : r)<<endl; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include <iostream> #include <queue> using namespace std; int main() { long long n, k, c = 0, q, u[500005] = {0}, r = 0; cin>>n>>k; for (long long i = 0; i < n; ++i) { cin>>q; if (u[q] == 0) { u[q] = 1; ++c; r += i - c + 1; if (c == k) break; } } cout<<(c < k ? -1 : r)<<endl; } |