#include <iostream> #include <array> #define MAX_N 500000 int solve() { std::array<bool, MAX_N> alreadyUsed{}; int n = 0; int k = 0; std::cin >> n; std::cin >> k; long long seconds = 0; int alreadyUsedNumber = 0; int currentBottle = 0; for (int i = 0; i < n; i++) { std::cin >> currentBottle; if (alreadyUsed[currentBottle]) { continue; } else { seconds += (i - alreadyUsedNumber); alreadyUsed[currentBottle] = true; alreadyUsedNumber++; } if (alreadyUsedNumber == k) return seconds; } return -1; } int main() { int result = solve(); std::cout << result; }
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 | #include <iostream> #include <array> #define MAX_N 500000 int solve() { std::array<bool, MAX_N> alreadyUsed{}; int n = 0; int k = 0; std::cin >> n; std::cin >> k; long long seconds = 0; int alreadyUsedNumber = 0; int currentBottle = 0; for (int i = 0; i < n; i++) { std::cin >> currentBottle; if (alreadyUsed[currentBottle]) { continue; } else { seconds += (i - alreadyUsedNumber); alreadyUsed[currentBottle] = true; alreadyUsedNumber++; } if (alreadyUsedNumber == k) return seconds; } return -1; } int main() { int result = solve(); std::cout << result; } |