#include <iostream> #include <map> #include <vector> #include <algorithm> #define rep(a,b) for(int a=0; a<(b); ++a) #define nl "\n" using std::cin; using std::cout; using std::map; using std::vector; using std::sort; using std::pair; using std::clog; long long n,k,res=0,count=0; map<long long,long long> pos; int main() { cin>>n>>k; rep(i,n){ long long buf; cin>>buf; if(!pos.count(buf)){ pos[buf]=i; ++count; } } //clog<<count<<nl; if(count<k){ cout<<-1<<nl; return 0; } long long used=0; vector<long long> sorted; for(auto it:pos){ sorted.push_back(it.second); } sort(sorted.begin(),sorted.end()); /*for(auto it:sorted){ clog<<it<<nl; }*/ while(used<k){ res+=sorted[used]-used; ++used; } cout<<res<<nl; 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 41 42 43 44 45 46 47 48 49 50 | #include <iostream> #include <map> #include <vector> #include <algorithm> #define rep(a,b) for(int a=0; a<(b); ++a) #define nl "\n" using std::cin; using std::cout; using std::map; using std::vector; using std::sort; using std::pair; using std::clog; long long n,k,res=0,count=0; map<long long,long long> pos; int main() { cin>>n>>k; rep(i,n){ long long buf; cin>>buf; if(!pos.count(buf)){ pos[buf]=i; ++count; } } //clog<<count<<nl; if(count<k){ cout<<-1<<nl; return 0; } long long used=0; vector<long long> sorted; for(auto it:pos){ sorted.push_back(it.second); } sort(sorted.begin(),sorted.end()); /*for(auto it:sorted){ clog<<it<<nl; }*/ while(used<k){ res+=sorted[used]-used; ++used; } cout<<res<<nl; return 0; } |