#include <iostream>
#include <set>
using namespace std;
int main()
{
set<int> brands;
int n, k, left_counter = 0, turns = 0, next_bottle;
cin >> n >> k;
for (int i = 0; i < n; i++)
{
cin >> next_bottle;
if (brands.count(next_bottle) == 0)
{
//New brand
brands.insert(next_bottle);
turns += i - left_counter;
left_counter++;
}
if (left_counter >= k) break;
}
cout << ((left_counter >= k) ? turns : -1);
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 | #include <iostream> #include <set> using namespace std; int main() { set<int> brands; int n, k, left_counter = 0, turns = 0, next_bottle; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> next_bottle; if (brands.count(next_bottle) == 0) { //New brand brands.insert(next_bottle); turns += i - left_counter; left_counter++; } if (left_counter >= k) break; } cout << ((left_counter >= k) ? turns : -1); return 0; } |
English