#include <iostream> #include <vector> using namespace std; typedef long long int lint; int main() { ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; vector<bool> present(n+1); vector<int> first_appearances; for (int i = 0; i < n; ++i) { int ai; cin >> ai; if (!present[ai]) { present[ai] = true; first_appearances.push_back(i); } } if (k > first_appearances.size()) { cout << -1 << endl; return 0; } lint sum = 0; for (int j = 0; j < k; ++j) { int diff = first_appearances[j] - j; sum += diff; } cout << sum << endl; 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 | #include <iostream> #include <vector> using namespace std; typedef long long int lint; int main() { ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; vector<bool> present(n+1); vector<int> first_appearances; for (int i = 0; i < n; ++i) { int ai; cin >> ai; if (!present[ai]) { present[ai] = true; first_appearances.push_back(i); } } if (k > first_appearances.size()) { cout << -1 << endl; return 0; } lint sum = 0; for (int j = 0; j < k; ++j) { int diff = first_appearances[j] - j; sum += diff; } cout << sum << endl; return 0; } |