// Autor: Mikołaj Janusz
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
int main() {
ios::sync_with_stdio(false);
ll n, k, different, res = 0;
cin >> n >> k;
set<ll> brands;
set<ll> on_position;
vector<ll> bottles;
for (ll i = 0; i < n; i++) {
ll temp;
cin >> temp;
bottles.push_back(temp);
brands.insert(temp);
}
if (brands.size() < k) {
cout << "-1";
return 0;
}
ll index = 0;
ll insert_index = 0;
while (index < bottles.size()) {
if (on_position.find(bottles[index]) == on_position.end()) {
k--;
on_position.insert(bottles[index]);
insert_index++;
index++;
} else {
for (ll i = index; i < bottles.size(); i++) {
if (on_position.find(bottles[i]) == on_position.end()) {
k--;
on_position.insert(bottles[i]);
res += i - insert_index;
index = i;
insert_index++;
break;
}
}
}
if (k <= 0) {
cout << res;
return 0;
}
}
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 48 49 50 51 52 53 54 55 56 57 | // Autor: Mikołaj Janusz #include <bits/stdc++.h> typedef long long ll; using namespace std; int main() { ios::sync_with_stdio(false); ll n, k, different, res = 0; cin >> n >> k; set<ll> brands; set<ll> on_position; vector<ll> bottles; for (ll i = 0; i < n; i++) { ll temp; cin >> temp; bottles.push_back(temp); brands.insert(temp); } if (brands.size() < k) { cout << "-1"; return 0; } ll index = 0; ll insert_index = 0; while (index < bottles.size()) { if (on_position.find(bottles[index]) == on_position.end()) { k--; on_position.insert(bottles[index]); insert_index++; index++; } else { for (ll i = index; i < bottles.size(); i++) { if (on_position.find(bottles[i]) == on_position.end()) { k--; on_position.insert(bottles[i]); res += i - insert_index; index = i; insert_index++; break; } } } if (k <= 0) { cout << res; return 0; } } return 0; } |
English