#include <iostream>
using namespace std;
int n,k,t[500007],licz;
bool ta[500007],tab[500007];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin>>n>>k;
for(int i=0;i<n;i++)
{
cin>>t[i];
if(ta[t[i]]==0)
{
ta[t[i]]=1;
licz++;
}
}
if(licz<k)
{
cout<<-1;
return 0;
}
int x=0,y=0;
licz=0;
while(k>0)
{
if(tab[t[x]]==0)
{
licz+=x-y;
k--;
y++;
tab[t[x]]=1;
}
x++;
}
cout<<licz;
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 | #include <iostream> using namespace std; int n,k,t[500007],licz; bool ta[500007],tab[500007]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin>>n>>k; for(int i=0;i<n;i++) { cin>>t[i]; if(ta[t[i]]==0) { ta[t[i]]=1; licz++; } } if(licz<k) { cout<<-1; return 0; } int x=0,y=0; licz=0; while(k>0) { if(tab[t[x]]==0) { licz+=x-y; k--; y++; tab[t[x]]=1; } x++; } cout<<licz; return 0; } |
English