#include <iostream> #define MAX_SIZE 500000 int main() { unsigned long long n, k; bool* isUnique=new bool[MAX_SIZE](); unsigned int *notUniquePos=new unsigned int[MAX_SIZE](); int notUniqueCount = -1; unsigned int swaps = 0; unsigned int mark; std::cin >> n >> k; for (size_t i = 0; i < n; i++) { std::cin >> mark; if (i < k) { if (isUnique[mark] == false) { isUnique[mark] = true; } else { notUniqueCount++; notUniquePos[notUniqueCount] = i; } } else { if (notUniqueCount<0) break; if (isUnique[mark] == false) { isUnique[mark] = true; swaps += i - notUniquePos[notUniqueCount--]; } } } if (notUniqueCount>=0) { std::cout << -1; return 0; } std::cout << swaps; }
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 | #include <iostream> #define MAX_SIZE 500000 int main() { unsigned long long n, k; bool* isUnique=new bool[MAX_SIZE](); unsigned int *notUniquePos=new unsigned int[MAX_SIZE](); int notUniqueCount = -1; unsigned int swaps = 0; unsigned int mark; std::cin >> n >> k; for (size_t i = 0; i < n; i++) { std::cin >> mark; if (i < k) { if (isUnique[mark] == false) { isUnique[mark] = true; } else { notUniqueCount++; notUniquePos[notUniqueCount] = i; } } else { if (notUniqueCount<0) break; if (isUnique[mark] == false) { isUnique[mark] = true; swaps += i - notUniquePos[notUniqueCount--]; } } } if (notUniqueCount>=0) { std::cout << -1; return 0; } std::cout << swaps; } |