#include <bits/stdc++.h> using namespace std; bool w_k[1000007]; vector <int> jak_daleko; vector <int> do_zmiany; bool poza_k[1000007]; int butelki[1000007]; int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); int n; int k; cin >> n >> k; for (int i = 0; i < n; ++i){ cin >> butelki[i]; if (i >= k){ if (poza_k[butelki[i]] == 0 && w_k[butelki[i]] == 0){ jak_daleko.push_back(i+1-k); poza_k[butelki[i]] = 1; } } else{ if (w_k[butelki[i]] == 1){ do_zmiany.push_back(i); } if (w_k[butelki[i]] == 0){ w_k[butelki[i]] = 1; } } } long long wynik = 0; int iter = 0; for (int i = 0; i < do_zmiany.size(); ++i){ if (iter >= jak_daleko.size()){ cout << "-1" << "\n"; return 0; } wynik += (k-1)-do_zmiany[i] + (jak_daleko[iter]); iter++; } cout << wynik << "\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 | #include <bits/stdc++.h> using namespace std; bool w_k[1000007]; vector <int> jak_daleko; vector <int> do_zmiany; bool poza_k[1000007]; int butelki[1000007]; int main(){ ios_base::sync_with_stdio(0); cin.tie(NULL); int n; int k; cin >> n >> k; for (int i = 0; i < n; ++i){ cin >> butelki[i]; if (i >= k){ if (poza_k[butelki[i]] == 0 && w_k[butelki[i]] == 0){ jak_daleko.push_back(i+1-k); poza_k[butelki[i]] = 1; } } else{ if (w_k[butelki[i]] == 1){ do_zmiany.push_back(i); } if (w_k[butelki[i]] == 0){ w_k[butelki[i]] = 1; } } } long long wynik = 0; int iter = 0; for (int i = 0; i < do_zmiany.size(); ++i){ if (iter >= jak_daleko.size()){ cout << "-1" << "\n"; return 0; } wynik += (k-1)-do_zmiany[i] + (jak_daleko[iter]); iter++; } cout << wynik << "\n"; return 0; } |