#include<bits/stdc++.h>
using namespace std;
int tab[1000005];
bool odw[1000005];
void chujowka(int a,int b)
{
set<int> s;
for(int x=1;x<=a;x++)
s.insert(tab[x]);
if(s.size() < b)
{
cout<<-1;
exit(0);
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int a,b;
cin>>a>>b;
for(int x=1;x<=a;x++)
cin>>tab[x];
chujowka(a,b);
vector<int> v1;
vector<int> v2;
for(int x=1;x<=b;x++)
{
if(!odw[tab[x]])
odw[tab[x]] = true;
else
v1.push_back(x);
}
for(int x=b+1;x<=a;x++)
{
if(v2.size() == v1.size())
break;
if(!odw[tab[x]])
{
odw[tab[x]] = true;
v2.push_back(x);
}
}
long long ans = 0;
for(int x=0;x<v1.size();x++)
ans += v2[x] - v1[x];
cout<<ans;
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 | #include<bits/stdc++.h> using namespace std; int tab[1000005]; bool odw[1000005]; void chujowka(int a,int b) { set<int> s; for(int x=1;x<=a;x++) s.insert(tab[x]); if(s.size() < b) { cout<<-1; exit(0); } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int a,b; cin>>a>>b; for(int x=1;x<=a;x++) cin>>tab[x]; chujowka(a,b); vector<int> v1; vector<int> v2; for(int x=1;x<=b;x++) { if(!odw[tab[x]]) odw[tab[x]] = true; else v1.push_back(x); } for(int x=b+1;x<=a;x++) { if(v2.size() == v1.size()) break; if(!odw[tab[x]]) { odw[tab[x]] = true; v2.push_back(x); } } long long ans = 0; for(int x=0;x<v1.size();x++) ans += v2[x] - v1[x]; cout<<ans; return 0; } |
English