#include <iostream>
#pragma GCC optimize("O3")
using namespace std;
int main() {
int n, k;
cin >> n >> k;
int bottle;
int brands[k];
int brandPlace[k];
auto brandCount = 0;
bool check = true;
auto x=0;
while(check) {
cin >> bottle;
if(n-x < k-brandCount) check = false;
bool contains = false;
for(int i = 0; i < brandCount; ++i){
if(brands[i] == bottle){
contains = true;
}
}
if (!contains) {
if (brandCount < k) {
brands[brandCount] = bottle;
brandPlace[brandCount] = x;
}
++brandCount;
} else {
if (brandCount > k) {
check = false;
}
}
++x;
}
if (brandCount < k) {
cout << "-1";
} else {
int moveCount = 0;
for(int i=0; i<k; ++i) {
moveCount += (brandPlace[i]-i);
}
cout << moveCount;
}
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 | #include <iostream> #pragma GCC optimize("O3") using namespace std; int main() { int n, k; cin >> n >> k; int bottle; int brands[k]; int brandPlace[k]; auto brandCount = 0; bool check = true; auto x=0; while(check) { cin >> bottle; if(n-x < k-brandCount) check = false; bool contains = false; for(int i = 0; i < brandCount; ++i){ if(brands[i] == bottle){ contains = true; } } if (!contains) { if (brandCount < k) { brands[brandCount] = bottle; brandPlace[brandCount] = x; } ++brandCount; } else { if (brandCount > k) { check = false; } } ++x; } if (brandCount < k) { cout << "-1"; } else { int moveCount = 0; for(int i=0; i<k; ++i) { moveCount += (brandPlace[i]-i); } cout << moveCount; } return 0; } |
English