#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
int main(){
int licz_b, licz_od_l, licz_s=0, it=0, temp = 1, gites = 0;
map <int, bool> pom;
cin>>licz_b>>licz_od_l;
int tab[licz_b], temp_tab[licz_b];
for(int i = 0; i<licz_b; ++i){
cin>>tab[i];
temp_tab[i] = tab[i];
}
sort(&temp_tab[0], &temp_tab[licz_b]);
for(int i=0; i<licz_b-1; ++i){
if(temp_tab[i] != temp_tab[i+1]){
temp++;
}
}
if(temp < licz_od_l){
cout<<-1;
}else{
temp = 0;
for (int i=0; i< licz_b; ++i){
pom[tab[i]] = false;
}
for (int i=0; i< licz_b; ++i){
if(!pom[tab[i]]){
pom[tab[i]] = true;
licz_s += (i-temp);
temp++;
}
if(temp == licz_od_l){
cout<<licz_s;
break;
}
}
}
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 | #include<iostream> #include<algorithm> #include<map> using namespace std; int main(){ int licz_b, licz_od_l, licz_s=0, it=0, temp = 1, gites = 0; map <int, bool> pom; cin>>licz_b>>licz_od_l; int tab[licz_b], temp_tab[licz_b]; for(int i = 0; i<licz_b; ++i){ cin>>tab[i]; temp_tab[i] = tab[i]; } sort(&temp_tab[0], &temp_tab[licz_b]); for(int i=0; i<licz_b-1; ++i){ if(temp_tab[i] != temp_tab[i+1]){ temp++; } } if(temp < licz_od_l){ cout<<-1; }else{ temp = 0; for (int i=0; i< licz_b; ++i){ pom[tab[i]] = false; } for (int i=0; i< licz_b; ++i){ if(!pom[tab[i]]){ pom[tab[i]] = true; licz_s += (i-temp); temp++; } if(temp == licz_od_l){ cout<<licz_s; break; } } } return 0; } |
English