#include <bits/stdc++.h> using namespace std; #ifdef D #define DEBUG(x) \ do { \ x \ cout.flush(); \ } while (0) #else #define DEBUG(x) #endif const int MAXN = 5e5+7; int n, k, a; long long ans; vector<int> r; bool vis[MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> a; if (!vis[a]) { vis[a] = true; r.push_back(i); } } if (r.size() < k) { ans = -1; } else { for (int i = 0; i < k; i++) { ans += r[i] - i; } } cout << ans; 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 41 | #include <bits/stdc++.h> using namespace std; #ifdef D #define DEBUG(x) \ do { \ x \ cout.flush(); \ } while (0) #else #define DEBUG(x) #endif const int MAXN = 5e5+7; int n, k, a; long long ans; vector<int> r; bool vis[MAXN]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> a; if (!vis[a]) { vis[a] = true; r.push_back(i); } } if (r.size() < k) { ans = -1; } else { for (int i = 0; i < k; i++) { ans += r[i] - i; } } cout << ans; return 0; } |