#include <bits/stdc++.h> #define f first #define s second #define mp make_pair #define pb push_back #define pii pair<int, int> #define pll pair<long long, long long> #define ll long long #define vi vector<int> #define vl vector<ll> #define rep(i,p,k) for(int i=p; i<k; i++) #define per(i,p,k) for (int i=k-1;i>=p;i--) ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} using namespace std; int tab[1000005]; set<int>s; int main() { int n, k; cin >> n >> k; rep(i, 0, n) cin >> tab[i]; int found = 0; ll moves = 0; rep(i, 0, n) { if (found == k) break; if (s.find(tab[i]) == s.end()) { s.insert(tab[i]); moves += i-found; found++; } } if (found == k) cout << moves; else cout << -1; }
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 | #include <bits/stdc++.h> #define f first #define s second #define mp make_pair #define pb push_back #define pii pair<int, int> #define pll pair<long long, long long> #define ll long long #define vi vector<int> #define vl vector<ll> #define rep(i,p,k) for(int i=p; i<k; i++) #define per(i,p,k) for (int i=k-1;i>=p;i--) ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;} using namespace std; int tab[1000005]; set<int>s; int main() { int n, k; cin >> n >> k; rep(i, 0, n) cin >> tab[i]; int found = 0; ll moves = 0; rep(i, 0, n) { if (found == k) break; if (s.find(tab[i]) == s.end()) { s.insert(tab[i]); moves += i-found; found++; } } if (found == k) cout << moves; else cout << -1; } |