#include <bits/stdc++.h> using std::cout; using std::cin; using std::ios_base; using std::string; using std::sort; using std::max; using std::min; using std::vector; using std::priority_queue; using std::stack; using std::endl; using std::fixed; #define be_faster_please ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define ll long long struct data { int marka, poz; }; bool cmp1(const data & a, const data & b) { if(a.marka != b.marka) return a.marka < b.marka; else return a.poz < b.poz; } data t[500010]; int pom[500010]; int main() { be_faster_please; int n, k; cin >> n >> k; for(int i = 0; i < n; i++) { int x; cin >> x; t[i].marka = x; t[i].poz = i; } sort(t, t + n, cmp1); int licz=1; for(int i = 1; i < n; i++) { if (t[i].marka != t[i - 1].marka) licz++; } if(licz < k) cout << -1; else { int licz = 1; pom[0] = t[0].poz; for(int i = 1; i < n; i++) { if (t[i].marka != t[i - 1].marka) { pom[licz] = t[i].poz; licz++; } } sort(pom, pom + licz); ll ans = 0; for(int i = 0; i < k; i++) { ans += pom[i] - i; } cout << ans; } 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 | #include <bits/stdc++.h> using std::cout; using std::cin; using std::ios_base; using std::string; using std::sort; using std::max; using std::min; using std::vector; using std::priority_queue; using std::stack; using std::endl; using std::fixed; #define be_faster_please ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); #define ll long long struct data { int marka, poz; }; bool cmp1(const data & a, const data & b) { if(a.marka != b.marka) return a.marka < b.marka; else return a.poz < b.poz; } data t[500010]; int pom[500010]; int main() { be_faster_please; int n, k; cin >> n >> k; for(int i = 0; i < n; i++) { int x; cin >> x; t[i].marka = x; t[i].poz = i; } sort(t, t + n, cmp1); int licz=1; for(int i = 1; i < n; i++) { if (t[i].marka != t[i - 1].marka) licz++; } if(licz < k) cout << -1; else { int licz = 1; pom[0] = t[0].poz; for(int i = 1; i < n; i++) { if (t[i].marka != t[i - 1].marka) { pom[licz] = t[i].poz; licz++; } } sort(pom, pom + licz); ll ans = 0; for(int i = 0; i < k; i++) { ans += pom[i] - i; } cout << ans; } return 0; } |