#include <cstdio> #include <map> #include <vector> using namespace std; int n, k, p, ans; map<int, bool> v; vector<int> av; int main(){ scanf("%d%d", &n, &k); for(int i = 0; i < n; i++){ scanf("%d", &p); if(!v[p]) av.push_back(i); v[p] = 1; } if(av.size() < k) printf("-1"); else{ for(int i = 0; i < k; i++){ ans += av[i] - i; } printf("%d\n", ans); } }
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 | #include <cstdio> #include <map> #include <vector> using namespace std; int n, k, p, ans; map<int, bool> v; vector<int> av; int main(){ scanf("%d%d", &n, &k); for(int i = 0; i < n; i++){ scanf("%d", &p); if(!v[p]) av.push_back(i); v[p] = 1; } if(av.size() < k) printf("-1"); else{ for(int i = 0; i < k; i++){ ans += av[i] - i; } printf("%d\n", ans); } } |