#include <bits/stdc++.h>
using namespace std;
int n,k,marka[500007];
long long result;
set <int> ustawione;
int main()
{
ios_base::sync_with_stdio(false);
cin >> n >> k;
for(int i=0; i<n; i++)
{
cin >> marka[i];
}
int pos = 0;
while(ustawione.size()<k && pos < n)
{
if(ustawione.count(marka[pos]) == 0)
{
result += (pos - ustawione.size());
ustawione.insert(marka[pos]);
}
pos++;
}
if (ustawione.size() < k) cout << -1 << endl;
else cout << result << endl;
}
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 | #include <bits/stdc++.h> using namespace std; int n,k,marka[500007]; long long result; set <int> ustawione; int main() { ios_base::sync_with_stdio(false); cin >> n >> k; for(int i=0; i<n; i++) { cin >> marka[i]; } int pos = 0; while(ustawione.size()<k && pos < n) { if(ustawione.count(marka[pos]) == 0) { result += (pos - ustawione.size()); ustawione.insert(marka[pos]); } pos++; } if (ustawione.size() < k) cout << -1 << endl; else cout << result << endl; } |
English