#include <iostream> const int MAXN = 500001; int butelki[MAXN]; int count[MAXN]; bool mark[MAXN]; int stamps = 0; int n,k; int main() { std::ios_base::sync_with_stdio(false); std::cin >> n >> k; for(int i = 0; i < n; ++i) { int temp; std::cin >> temp; butelki[i] = temp; count[i] = 0; } int sum = 0; for(int i = 0; i < k; ++i) { count[butelki[i]-1]++; mark[i] = false; if(count[butelki[i] - 1] > 1) { sum++; mark[i] = true; } } stamps += sum*sum; //int lastPosition = k-1; int freeFields = 0; for(int i = k-1; i <= 0; --i) { if(mark[i] == true) { stamps += freeFields; } else{ freeFields++; } } freeFields = 0; int passed = 0; for(int i = k; i < n; ++i) { if(count[butelki[i] - 1] == 0) { count[butelki[i] - 1]++; stamps += freeFields; passed++; } else{ freeFields++; } if(passed == sum) break; } if(passed != sum ) std::cout << "-1\n"; else std::cout << stamps << "\n"; 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 | #include <iostream> const int MAXN = 500001; int butelki[MAXN]; int count[MAXN]; bool mark[MAXN]; int stamps = 0; int n,k; int main() { std::ios_base::sync_with_stdio(false); std::cin >> n >> k; for(int i = 0; i < n; ++i) { int temp; std::cin >> temp; butelki[i] = temp; count[i] = 0; } int sum = 0; for(int i = 0; i < k; ++i) { count[butelki[i]-1]++; mark[i] = false; if(count[butelki[i] - 1] > 1) { sum++; mark[i] = true; } } stamps += sum*sum; //int lastPosition = k-1; int freeFields = 0; for(int i = k-1; i <= 0; --i) { if(mark[i] == true) { stamps += freeFields; } else{ freeFields++; } } freeFields = 0; int passed = 0; for(int i = k; i < n; ++i) { if(count[butelki[i] - 1] == 0) { count[butelki[i] - 1]++; stamps += freeFields; passed++; } else{ freeFields++; } if(passed == sum) break; } if(passed != sum ) std::cout << "-1\n"; else std::cout << stamps << "\n"; return 0; } |