#include <iostream> #include <vector> #include <algorithm> #include<string> #include<fstream> using namespace std; const int maxA = 500000; int n, k, tab[maxA], byl[maxA], ostByl = 0, ile[maxA], zaw[maxA]; bool zastap(int x, bool porz) { int L = 0; int R = ostByl; while (L < R) { int sr = (L + R + 1) / 2; if (byl[sr] > x) R = sr - 1; else L = sr; } if (byl[L] == x && !porz) return true; if (L == 0 && x < byl[L]) L = -1; for (int i = ostByl; i > L; i--) byl[i + 1] = byl[i]; ostByl++; byl[L + 1] = x; return false; } bool spelnia() { byl[0] = min(tab[0], tab[1]); byl[1] = max(tab[0], tab[1]); if (byl[1] == byl[0]) return false; ostByl = 1; for (int i = 2; i < k; i++) if (zastap(tab[i], false)) return false; return true; } int main() { long long wynik = 0; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> tab[i]; ile[tab[i]]++; } int ileRodz = 0, nadwyzka = 0, dobrze = 1; for (int i = 0; i < n; i++) if (ile[i] > 0) ileRodz++; if (ileRodz < k) { cout << -1; return 0; } if (spelnia()) { cout << 0; return 0; } for (int i = 0; i < k; i++) byl[i] = 0; if (tab[0] == tab[1]) { byl[0] = tab[0]; nadwyzka = 1; ostByl = 0; } else { byl[0] = min(tab[0], tab[1]); byl[1] = max(tab[0], tab[1]); dobrze = 2; ostByl = 1; } for (int i = 2; i < n; i++) if (!binary_search(byl, byl + ostByl + 1, tab[i])) { zastap(tab[i], true); wynik += i - dobrze; dobrze++; } cout << wynik; 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 96 97 | #include <iostream> #include <vector> #include <algorithm> #include<string> #include<fstream> using namespace std; const int maxA = 500000; int n, k, tab[maxA], byl[maxA], ostByl = 0, ile[maxA], zaw[maxA]; bool zastap(int x, bool porz) { int L = 0; int R = ostByl; while (L < R) { int sr = (L + R + 1) / 2; if (byl[sr] > x) R = sr - 1; else L = sr; } if (byl[L] == x && !porz) return true; if (L == 0 && x < byl[L]) L = -1; for (int i = ostByl; i > L; i--) byl[i + 1] = byl[i]; ostByl++; byl[L + 1] = x; return false; } bool spelnia() { byl[0] = min(tab[0], tab[1]); byl[1] = max(tab[0], tab[1]); if (byl[1] == byl[0]) return false; ostByl = 1; for (int i = 2; i < k; i++) if (zastap(tab[i], false)) return false; return true; } int main() { long long wynik = 0; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> tab[i]; ile[tab[i]]++; } int ileRodz = 0, nadwyzka = 0, dobrze = 1; for (int i = 0; i < n; i++) if (ile[i] > 0) ileRodz++; if (ileRodz < k) { cout << -1; return 0; } if (spelnia()) { cout << 0; return 0; } for (int i = 0; i < k; i++) byl[i] = 0; if (tab[0] == tab[1]) { byl[0] = tab[0]; nadwyzka = 1; ostByl = 0; } else { byl[0] = min(tab[0], tab[1]); byl[1] = max(tab[0], tab[1]); dobrze = 2; ostByl = 1; } for (int i = 2; i < n; i++) if (!binary_search(byl, byl + ostByl + 1, tab[i])) { zastap(tab[i], true); wynik += i - dobrze; dobrze++; } cout << wynik; return 0; } |