#include <bits/stdc++.h> #include <unordered_set> using namespace std; const int N = int(5e5) + 99; int n, x, k; int a[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; unordered_set<int> wines = {}; for (int i = 0; i <n; i++) { cin >> a[i]; } int sum = 0; int collected = 0; for (int i = 0; i < n; i++) { if (wines.count(a[i]) == 0) { wines.insert(a[i]); sum += i - collected; collected++; if (collected >= k) { cout << sum << endl; return 0; } } } cout << -1 << endl; }
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 <bits/stdc++.h> #include <unordered_set> using namespace std; const int N = int(5e5) + 99; int n, x, k; int a[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; unordered_set<int> wines = {}; for (int i = 0; i <n; i++) { cin >> a[i]; } int sum = 0; int collected = 0; for (int i = 0; i < n; i++) { if (wines.count(a[i]) == 0) { wines.insert(a[i]); sum += i - collected; collected++; if (collected >= k) { cout << sum << endl; return 0; } } } cout << -1 << endl; } |