#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int n,k,t[1000001],check,odj[1000001],r=1,zap;
long long res;
bool vis[1000001];
int main()
{
cin>>n>>k;
for(int i=1; i<=n; i++){
cin>>t[i];
if(check<k){
if(vis[t[i]]){
odj[r]=i;
r++;
}
else
check++;
}
if(check==k){
zap=i;
check++;
}
vis[t[i]]=true;
}
if(zap==0){
cout<<"-1";
return 0;
}
for(int i=1; i<r; i++){
res+=zap-odj[i];
zap--;
}
cout<<res;
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 | #include <iostream> #include<bits/stdc++.h> using namespace std; int n,k,t[1000001],check,odj[1000001],r=1,zap; long long res; bool vis[1000001]; int main() { cin>>n>>k; for(int i=1; i<=n; i++){ cin>>t[i]; if(check<k){ if(vis[t[i]]){ odj[r]=i; r++; } else check++; } if(check==k){ zap=i; check++; } vis[t[i]]=true; } if(zap==0){ cout<<"-1"; return 0; } for(int i=1; i<r; i++){ res+=zap-odj[i]; zap--; } cout<<res; return 0; } |
English