#include <bits/stdc++.h>
using namespace std;
bool zaj[500009];
int tab[500009];
bool help[500009];
int main()
{ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n,m;
cin>>n>>m;
int rozne=0;
int ostatni=0;
for(int i=1;i<=n;i++)
{
int x;
cin>>x;
tab[i]=x;
if(zaj[x]==0)
{
rozne++;
if(rozne<=m)
{
ostatni=i;
help[i]=1;
zaj[x]=1;
}
}
}
if(rozne<m){cout<<-1<<endl;return 0;}
int h=0;
long long wynik=0;
for(int i=ostatni;i>=1;i--)
{
if(help[i]==1){h++;}
else{wynik+=h;}
}
cout<<wynik<<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 | #include <bits/stdc++.h> using namespace std; bool zaj[500009]; int tab[500009]; bool help[500009]; int main() {ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; cin>>n>>m; int rozne=0; int ostatni=0; for(int i=1;i<=n;i++) { int x; cin>>x; tab[i]=x; if(zaj[x]==0) { rozne++; if(rozne<=m) { ostatni=i; help[i]=1; zaj[x]=1; } } } if(rozne<m){cout<<-1<<endl;return 0;} int h=0; long long wynik=0; for(int i=ostatni;i>=1;i--) { if(help[i]==1){h++;} else{wynik+=h;} } cout<<wynik<<endl; return 0; } |
English