#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; vector <int> a(n); vector <int> wys(n); int ile = 0; int x = n + 1; for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } for (int i = 0; i < n; i++) { if (wys[a[i]] == 0) { ile++; wys[a[i]] = i + 1; } if (ile == k) { x = i; break; } } if (x == n + 1) { cout << "-1\n"; return 0; } long long res = 0, w = 0; for (int i = x; i >= 0; i--) { if (wys[a[i]] != i + 1) res += w; else w++; } cout << res << "\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 35 36 37 38 39 40 | #include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, k; cin >> n >> k; vector <int> a(n); vector <int> wys(n); int ile = 0; int x = n + 1; for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; } for (int i = 0; i < n; i++) { if (wys[a[i]] == 0) { ile++; wys[a[i]] = i + 1; } if (ile == k) { x = i; break; } } if (x == n + 1) { cout << "-1\n"; return 0; } long long res = 0, w = 0; for (int i = x; i >= 0; i--) { if (wys[a[i]] != i + 1) res += w; else w++; } cout << res << "\n"; return 0; } |