#include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; int n,k; vector <int> v; set<int> used; int main(){ scanf("%d%d",&n,&k); for (int i =0; i < n; i++){ int x; scanf("%d", &x); v.push_back(x); } int score = 0; int size = 0; for (int i = 0; i < n; i++){ int x = v[i]; if ( used.find(x) == used.end()){ score+= i-size; used.insert(x); size++; } // printf("i = %d, scoer = %d\n", i, score); if ( size == k ){ printf("%d\n", score); return 0; } } printf("%d\n", -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 | #include <iostream> #include <vector> #include <algorithm> #include <set> using namespace std; int n,k; vector <int> v; set<int> used; int main(){ scanf("%d%d",&n,&k); for (int i =0; i < n; i++){ int x; scanf("%d", &x); v.push_back(x); } int score = 0; int size = 0; for (int i = 0; i < n; i++){ int x = v[i]; if ( used.find(x) == used.end()){ score+= i-size; used.insert(x); size++; } // printf("i = %d, scoer = %d\n", i, score); if ( size == k ){ printf("%d\n", score); return 0; } } printf("%d\n", -1); } |