#include <iostream> using namespace std; unsigned long a[500000], n; int b[500000]; int main() { unsigned long k, i = 0, j, bi, l, ret = 0; cin >> n >> k; while (i < n) cin >> a[i++]; i = j = 0; while (k && i < n) { if (!b[a[i]]) { b[a[i]] = 1; ret += i - j++; k--; } i++; } if (k) { cout << -1; return 0; } cout << ret; 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 | #include <iostream> using namespace std; unsigned long a[500000], n; int b[500000]; int main() { unsigned long k, i = 0, j, bi, l, ret = 0; cin >> n >> k; while (i < n) cin >> a[i++]; i = j = 0; while (k && i < n) { if (!b[a[i]]) { b[a[i]] = 1; ret += i - j++; k--; } i++; } if (k) { cout << -1; return 0; } cout << ret; return 0; } |