#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define st first #define nd second #define pb push_back #define rep(i,a,b) for(ll i=a;i<=b;i++) #define all(a) a.begin(),a.end() const int mxN = 5e5+7; int a[mxN]; int ms[mxN]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,k; cin >> n >> k; rep(i,1,n) cin >> a[i]; set<int> ile_r; rep(i,1,n) ile_r.insert(a[i]); if(ile_r.size() < k){ cout << "-1\n"; return 0; } ll res = 0; int akt = 1; set<int> secik; for(int i=1;i<=n && akt <= k;i++){ if(secik.find(a[i]) == secik.end()){ res += i-akt; akt++; secik.insert(a[i]); } } cout << res << "\n"; }
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 | #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; #define st first #define nd second #define pb push_back #define rep(i,a,b) for(ll i=a;i<=b;i++) #define all(a) a.begin(),a.end() const int mxN = 5e5+7; int a[mxN]; int ms[mxN]; int main(){ ios_base::sync_with_stdio(0); cin.tie(0); int n,k; cin >> n >> k; rep(i,1,n) cin >> a[i]; set<int> ile_r; rep(i,1,n) ile_r.insert(a[i]); if(ile_r.size() < k){ cout << "-1\n"; return 0; } ll res = 0; int akt = 1; set<int> secik; for(int i=1;i<=n && akt <= k;i++){ if(secik.find(a[i]) == secik.end()){ res += i-akt; akt++; secik.insert(a[i]); } } cout << res << "\n"; } |