#include <iostream> using namespace std; int main() { int n, k; cin >> n >> k; int *brandMinPos = new int[n]; bool *brandWas = new bool[n]; int j = 0; for (int i=0;i<n;++i) { brandMinPos[i] = -1; brandWas[i] = false; } int brand; for (int i=0;i<n;++i) { cin >> brand; if (!brandWas[brand]) brandMinPos[j++] = i; brandWas[brand] = true; } long long sum = 0; for (int i=0;i<k;++i) { if (brandMinPos[i] == -1) { sum = -1; break; } else { sum += brandMinPos[i] - i; } } cout << sum; delete[] brandMinPos; delete[] brandWas; 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 42 43 44 45 46 47 | #include <iostream> using namespace std; int main() { int n, k; cin >> n >> k; int *brandMinPos = new int[n]; bool *brandWas = new bool[n]; int j = 0; for (int i=0;i<n;++i) { brandMinPos[i] = -1; brandWas[i] = false; } int brand; for (int i=0;i<n;++i) { cin >> brand; if (!brandWas[brand]) brandMinPos[j++] = i; brandWas[brand] = true; } long long sum = 0; for (int i=0;i<k;++i) { if (brandMinPos[i] == -1) { sum = -1; break; } else { sum += brandMinPos[i] - i; } } cout << sum; delete[] brandMinPos; delete[] brandWas; return 0; } |