#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
#include <map>
#include <set>
using namespace std;
// #define USE_CERR_LOG
#ifdef USE_CERR_LOG
#define LOG if (true) cerr
#else
#define LOG if (false) cerr
#endif
#ifdef USE_FILE_CIN
ifstream input("ora.in");
#define cin input
#endif
size_t bottleCount, diffCount;
vector<int> bottles;
enum InitialCheck {
ZERO, SOLVABLE, NOT_SOLVABLE
};
InitialCheck isSolvable() {
set<int> brands;
size_t i;
InitialCheck result;
for (i = 0; i < diffCount; i++) {
brands.insert(bottles[i]);
}
if (brands.size() == diffCount) {
result = ZERO;
} else {
for (; i < bottleCount; i++) {
brands.insert(bottles[i]);
}
result = (brands.size() >= diffCount) ? SOLVABLE : NOT_SOLVABLE;
}
return result;
}
long long solve() {
map<int, int> brandToIdx;
map<int, int> idxToBrand;
long long result = 0;
for (size_t i = 0; i < bottleCount; i++) {
brandToIdx.insert({bottles[i], i});
}
for (auto const& bi : brandToIdx) {
idxToBrand.insert({bi.second, bi.first});
}
auto idxIter = idxToBrand.begin();
for (size_t i = 0; i < diffCount; i++) {
LOG << "(" << i << "," << idxIter->first << ":" << idxIter->second << ") ";
result += idxIter->first - i;
idxIter ++;
}
return result;
}
int main() {
ios_base::sync_with_stdio(false);
cin >> bottleCount >> diffCount;
bottles.resize(bottleCount);
for (size_t i = 0; i < bottleCount; i++) {
cin >> bottles[i];
}
InitialCheck initialCheck = isSolvable();
if (initialCheck == SOLVABLE) {
cout << solve();
} else {
cout << (initialCheck == ZERO ? 0 : -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 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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | #include <iostream> #include <vector> #include <algorithm> #include <fstream> #include <map> #include <set> using namespace std; // #define USE_CERR_LOG #ifdef USE_CERR_LOG #define LOG if (true) cerr #else #define LOG if (false) cerr #endif #ifdef USE_FILE_CIN ifstream input("ora.in"); #define cin input #endif size_t bottleCount, diffCount; vector<int> bottles; enum InitialCheck { ZERO, SOLVABLE, NOT_SOLVABLE }; InitialCheck isSolvable() { set<int> brands; size_t i; InitialCheck result; for (i = 0; i < diffCount; i++) { brands.insert(bottles[i]); } if (brands.size() == diffCount) { result = ZERO; } else { for (; i < bottleCount; i++) { brands.insert(bottles[i]); } result = (brands.size() >= diffCount) ? SOLVABLE : NOT_SOLVABLE; } return result; } long long solve() { map<int, int> brandToIdx; map<int, int> idxToBrand; long long result = 0; for (size_t i = 0; i < bottleCount; i++) { brandToIdx.insert({bottles[i], i}); } for (auto const& bi : brandToIdx) { idxToBrand.insert({bi.second, bi.first}); } auto idxIter = idxToBrand.begin(); for (size_t i = 0; i < diffCount; i++) { LOG << "(" << i << "," << idxIter->first << ":" << idxIter->second << ") "; result += idxIter->first - i; idxIter ++; } return result; } int main() { ios_base::sync_with_stdio(false); cin >> bottleCount >> diffCount; bottles.resize(bottleCount); for (size_t i = 0; i < bottleCount; i++) { cin >> bottles[i]; } InitialCheck initialCheck = isSolvable(); if (initialCheck == SOLVABLE) { cout << solve(); } else { cout << (initialCheck == ZERO ? 0 : -1); } return 0; } |
English