#include<iostream> #include<vector> #include<set> using namespace std; int main(){ long long n,k; cin >> n >> k; set<long long>mo; vector<long long>w(n); for (long long i=0;i<n;i++){ cin >> w[i]; mo.insert(w[i]); } if(mo.size()<k){ cout << "-1" << endl; return 0; } set<long long>o; //vector<bool>s(k,false); long long s=0; //long long ss=0; int dz=k; vector<long long>z; long long wy=0; for (long long i=0;i<n;i++){ while(i<k){ if(o.find(w[i])==o.end()){ o.insert(w[i]); dz--; } else{ z.push_back(i); //cout << i << " "; } i++; } if(z.size()<=s){ //cout << "tu"<<endl; break; } if(o.find(w[i])==o.end()){ o.insert(w[i]); wy+=i-z[s]; s++; } /*if(o.find(w[i])==o.end()){ o.insert(w[i]); if(ss>k){ wy+=i; wy-=ss; } s[ss]=true; while(s[ss]==true && ss<k){ss++;} }*/ } cout << wy << endl; 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 | #include<iostream> #include<vector> #include<set> using namespace std; int main(){ long long n,k; cin >> n >> k; set<long long>mo; vector<long long>w(n); for (long long i=0;i<n;i++){ cin >> w[i]; mo.insert(w[i]); } if(mo.size()<k){ cout << "-1" << endl; return 0; } set<long long>o; //vector<bool>s(k,false); long long s=0; //long long ss=0; int dz=k; vector<long long>z; long long wy=0; for (long long i=0;i<n;i++){ while(i<k){ if(o.find(w[i])==o.end()){ o.insert(w[i]); dz--; } else{ z.push_back(i); //cout << i << " "; } i++; } if(z.size()<=s){ //cout << "tu"<<endl; break; } if(o.find(w[i])==o.end()){ o.insert(w[i]); wy+=i-z[s]; s++; } /*if(o.find(w[i])==o.end()){ o.insert(w[i]); if(ss>k){ wy+=i; wy-=ss; } s[ss]=true; while(s[ss]==true && ss<k){ss++;} }*/ } cout << wy << endl; return 0; } |