#include <bits/stdc++.h> using namespace std; bool used[500007]; int main() { // unordered_map<int, bool> m; cin.tie(0); ios_base::sync_with_stdio(false); int n; int k; long long a; long long wyn = 0; bool pos = false; bool t2[500007] = {}; int t[500007] = {}; cin >> n; cin >> k; a = 1; for (int i = 0; i < n; i++) { cin >> t[i]; if (t[i] != 0) { t2[i] = true; } } used[t[0]] = true; for (long long i = 1; i < n; i++) { if (used[t[i]] == false && t2[i] == true) { wyn += i - a; a++; used[t[i]] = true; } if (k == a) { pos = true; break; } } if (pos == true) { cout << wyn; } else { cout << -1; } }
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 36 37 38 39 40 41 42 43 | #include <bits/stdc++.h> using namespace std; bool used[500007]; int main() { // unordered_map<int, bool> m; cin.tie(0); ios_base::sync_with_stdio(false); int n; int k; long long a; long long wyn = 0; bool pos = false; bool t2[500007] = {}; int t[500007] = {}; cin >> n; cin >> k; a = 1; for (int i = 0; i < n; i++) { cin >> t[i]; if (t[i] != 0) { t2[i] = true; } } used[t[0]] = true; for (long long i = 1; i < n; i++) { if (used[t[i]] == false && t2[i] == true) { wyn += i - a; a++; used[t[i]] = true; } if (k == a) { pos = true; break; } } if (pos == true) { cout << wyn; } else { cout << -1; } } |