// Problem: Oranzada [B] // https://sio2.mimuw.edu.pl/c/pa-2021-1/p/ora/ #include <cstdio> #include <cmath> #include <iostream> #include <algorithm> #include <string> #include <cctype> #include <vector> #include <set> #include <map> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define FOREACH(e, v) for(auto const &e: v) #define IN(e, s) (s).find((e)) != (s).end() #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; #define PB push_back #define ST first #define ND second #define ALL(c) (c).begin(), (c).end() #define SZ(x) ((int)(x).size()) typedef long long LL; typedef unsigned long long ULL; typedef unsigned short UST; typedef unsigned int UINT; typedef vector<int> VI; inline void fix_io() { cin.tie(nullptr); ios::sync_with_stdio(false); } int main() { fix_io(); UINT n,k; UINT c=0; cin >> n >> k; set<UINT> uq; vector<UINT> idx; FOR(i,0,n-1) { UINT a; cin >> a; if (c != k) { if (uq.find(a) == uq.end()) { uq.insert(a); idx.PB(i); c++; } } } if (c < k) { cout << "-1\n"; return 0; } UINT pos = 1; ULL ans = 0; FOR(i,1,idx.size()-1) { ans += idx[i] - pos; pos++; } cout << ans << '\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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | // Problem: Oranzada [B] // https://sio2.mimuw.edu.pl/c/pa-2021-1/p/ora/ #include <cstdio> #include <cmath> #include <iostream> #include <algorithm> #include <string> #include <cctype> #include <vector> #include <set> #include <map> using namespace std; #define REP(i,n) for(int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for(int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for(int i=(a),_b=(b);i>=_b;--i) #define FOREACH(e, v) for(auto const &e: v) #define IN(e, s) (s).find((e)) != (s).end() #define TRACE(x) cerr << "TRACE(" #x ")" << endl; #define DEBUG(x) cerr << #x << " = " << (x) << endl; #define PB push_back #define ST first #define ND second #define ALL(c) (c).begin(), (c).end() #define SZ(x) ((int)(x).size()) typedef long long LL; typedef unsigned long long ULL; typedef unsigned short UST; typedef unsigned int UINT; typedef vector<int> VI; inline void fix_io() { cin.tie(nullptr); ios::sync_with_stdio(false); } int main() { fix_io(); UINT n,k; UINT c=0; cin >> n >> k; set<UINT> uq; vector<UINT> idx; FOR(i,0,n-1) { UINT a; cin >> a; if (c != k) { if (uq.find(a) == uq.end()) { uq.insert(a); idx.PB(i); c++; } } } if (c < k) { cout << "-1\n"; return 0; } UINT pos = 1; ULL ans = 0; FOR(i,1,idx.size()-1) { ans += idx[i] - pos; pos++; } cout << ans << '\n'; } |