#include <stdio.h> #include <map> using namespace std; int main(int argc, char const *argv[]) { int bottlesNum, leftNum, uniquesNum = 0, diff, result = 0, i; map<int, bool> brandSetup; if (!scanf("%d %d", &bottlesNum, &leftNum)) return 1; int bottles[bottlesNum], uniques[leftNum]; for (i = 1; i <= bottlesNum; ++i) { brandSetup[i] = false; } for (i = 0; i < bottlesNum; ++i) { if (!scanf("%d", &bottles[i])) return 1; if (!brandSetup[bottles[i]]) { brandSetup[bottles[i]] = true; uniques[uniquesNum++] = i; } } if (leftNum > uniquesNum) { printf("-1"); return 0; } result = 0; diff = 0; for (i = 1; i < leftNum; ++i) { diff += uniques[i] - uniques[i-1] - 1; result += diff; } printf("%d", result); 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 | #include <stdio.h> #include <map> using namespace std; int main(int argc, char const *argv[]) { int bottlesNum, leftNum, uniquesNum = 0, diff, result = 0, i; map<int, bool> brandSetup; if (!scanf("%d %d", &bottlesNum, &leftNum)) return 1; int bottles[bottlesNum], uniques[leftNum]; for (i = 1; i <= bottlesNum; ++i) { brandSetup[i] = false; } for (i = 0; i < bottlesNum; ++i) { if (!scanf("%d", &bottles[i])) return 1; if (!brandSetup[bottles[i]]) { brandSetup[bottles[i]] = true; uniques[uniquesNum++] = i; } } if (leftNum > uniquesNum) { printf("-1"); return 0; } result = 0; diff = 0; for (i = 1; i < leftNum; ++i) { diff += uniques[i] - uniques[i-1] - 1; result += diff; } printf("%d", result); return 0; } |