#include <cstdio> #include <iostream> #include <algorithm> #include <string> #include <unordered_set> #include <queue> using namespace std; int a[500000+1]; int main(){ long long int k = 0; long long int n = 0; unordered_set<int> oren; cin >> n; cin >> k; for(int i = 0; i < n; ++i){ cin >> a[i]; } long long int wynik = 0; long long int place=0; for(int i = 0; i < n && place < k; ++i){ if(oren.find(a[i]) == oren.end()){ oren.insert(a[i]); wynik+=i-place; place++; } } if(oren.size() != k){ cout << -1 << "\n"; } else { cout << wynik << "\n"; } 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 <cstdio> #include <iostream> #include <algorithm> #include <string> #include <unordered_set> #include <queue> using namespace std; int a[500000+1]; int main(){ long long int k = 0; long long int n = 0; unordered_set<int> oren; cin >> n; cin >> k; for(int i = 0; i < n; ++i){ cin >> a[i]; } long long int wynik = 0; long long int place=0; for(int i = 0; i < n && place < k; ++i){ if(oren.find(a[i]) == oren.end()){ oren.insert(a[i]); wynik+=i-place; place++; } } if(oren.size() != k){ cout << -1 << "\n"; } else { cout << wynik << "\n"; } return 0; } |