#include <bits/stdc++.h> using namespace std; vector <int> tab; set<int> s1; set<int> s2; //map<int,int> kub; int main() { int n,k; cin >> n >> k; for (int i=0;i<n;++i) { int pom; cin >> pom; tab.push_back(pom); if(i<k) { //kub[pom]++; s1.insert(pom); } else { } } int w=0; int j=k; for (int i=0;i<k;++i) { if (s2.find(tab[i]) != s2.end()) { while(s1.find(tab[j]) != s1.end()) { j++; if (j>=n) { cout << "-1"; return 0; } } //kub[tab[j]] ++; w+=j-i; s1.insert(tab[j]); } s2.insert(tab[i]); } cout << w; } /* 5 3 3 3 3 1 2 */
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 | #include <bits/stdc++.h> using namespace std; vector <int> tab; set<int> s1; set<int> s2; //map<int,int> kub; int main() { int n,k; cin >> n >> k; for (int i=0;i<n;++i) { int pom; cin >> pom; tab.push_back(pom); if(i<k) { //kub[pom]++; s1.insert(pom); } else { } } int w=0; int j=k; for (int i=0;i<k;++i) { if (s2.find(tab[i]) != s2.end()) { while(s1.find(tab[j]) != s1.end()) { j++; if (j>=n) { cout << "-1"; return 0; } } //kub[tab[j]] ++; w+=j-i; s1.insert(tab[j]); } s2.insert(tab[i]); } cout << w; } /* 5 3 3 3 3 1 2 */ |