#include <cstdio>
#include <vector>
#include <cstdlib>
// #define DEBUG_M
#ifdef DEBUG_M
#define DEBUGF(...) printf(stderr, __VA_ARGS__)
#define DEBUG(expr) expr
#else
#define DEBUGF(...)
#define DEBUG(expr)
#endif
using namespace std;
int main() {
int n;
int k;
scanf(" %d %d", &n, &k);
std::vector<int> bottle(n);
std::vector<int> lookup(n, 0);
int lkIdx = 1;
for (int i = 0; i < n; i++) {
int tmp;
scanf(" %d", &tmp);
if (!lookup[tmp-1]) {
lookup[tmp-1] = lkIdx++;
}
bottle[i] = lookup[tmp-1];
}
if (k == 1) {
puts("0");
return 0;
}
if (lkIdx <= k) {
puts("-1");
return 0;
}
int lastIn = 1;
long long count = ((long long) k) * (k-1);
count >>= 1; // Epic bind moment B)
count = -count;
for (int i = 1; lastIn < k; i++) { // && i < n ale wyżej to gwarantujemy
if (bottle[i] > lastIn) {
count += i;
lastIn++;
}
}
if (lastIn == k) {
printf("%lld\n", count);
} else {
puts("-1");
}
}
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 58 59 60 61 | #include <cstdio> #include <vector> #include <cstdlib> // #define DEBUG_M #ifdef DEBUG_M #define DEBUGF(...) printf(stderr, __VA_ARGS__) #define DEBUG(expr) expr #else #define DEBUGF(...) #define DEBUG(expr) #endif using namespace std; int main() { int n; int k; scanf(" %d %d", &n, &k); std::vector<int> bottle(n); std::vector<int> lookup(n, 0); int lkIdx = 1; for (int i = 0; i < n; i++) { int tmp; scanf(" %d", &tmp); if (!lookup[tmp-1]) { lookup[tmp-1] = lkIdx++; } bottle[i] = lookup[tmp-1]; } if (k == 1) { puts("0"); return 0; } if (lkIdx <= k) { puts("-1"); return 0; } int lastIn = 1; long long count = ((long long) k) * (k-1); count >>= 1; // Epic bind moment B) count = -count; for (int i = 1; lastIn < k; i++) { // && i < n ale wyżej to gwarantujemy if (bottle[i] > lastIn) { count += i; lastIn++; } } if (lastIn == k) { printf("%lld\n", count); } else { puts("-1"); } } |
English