#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,k;
cin>>n>>k;
vector<int> bottles(n);
for(int i = 0; i < n; i++)
cin>>bottles[i];
vector<bool> czy(n+1);
czy[bottles[0]] = true;
int how_many = 1;
int result = 0;
for(int i = 1; i < bottles.size() && how_many < k; i++){
if(!czy[bottles[i]]){
result += abs(i - (int)how_many);
czy[bottles[i]] = true;
how_many++;
}
}
if(how_many == k)
cout<<result<<endl;
else
cout<<-1<<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 | #include<bits/stdc++.h> using namespace std; int main() { int n,k; cin>>n>>k; vector<int> bottles(n); for(int i = 0; i < n; i++) cin>>bottles[i]; vector<bool> czy(n+1); czy[bottles[0]] = true; int how_many = 1; int result = 0; for(int i = 1; i < bottles.size() && how_many < k; i++){ if(!czy[bottles[i]]){ result += abs(i - (int)how_many); czy[bottles[i]] = true; how_many++; } } if(how_many == k) cout<<result<<endl; else cout<<-1<<endl; return 0; } |
English