#include <bits/stdc++.h>
using namespace std;
int tab[1000000];
bool uz[1000000];
int main() {
ios_base::sync_with_stdio(0);
int n,k,w=0;
queue<int> ez;
cin>>n>>k;
for(int i=0;i<n;i++) cin>>tab[i];
for(int i=0;i<k;i++)
{
if(!uz[tab[i]]) uz[tab[i]]=1;
else ez.push(i);
}
int i=k;
while(!ez.empty())
{
if(!uz[tab[i]])
{
w+=i-ez.front();
uz[tab[i]]=1;
ez.pop();
}
i++;
if(i==n+1)
{
cout<<-1;
exit(0);
}
}
cout<<w;
}
//(a+n-1)/n
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 | #include <bits/stdc++.h> using namespace std; int tab[1000000]; bool uz[1000000]; int main() { ios_base::sync_with_stdio(0); int n,k,w=0; queue<int> ez; cin>>n>>k; for(int i=0;i<n;i++) cin>>tab[i]; for(int i=0;i<k;i++) { if(!uz[tab[i]]) uz[tab[i]]=1; else ez.push(i); } int i=k; while(!ez.empty()) { if(!uz[tab[i]]) { w+=i-ez.front(); uz[tab[i]]=1; ez.pop(); } i++; if(i==n+1) { cout<<-1; exit(0); } } cout<<w; } //(a+n-1)/n |
English