#include <cstdio> #include <iostream> #include <vector> #include <unordered_set> #include <algorithm> using namespace std; int main() { bool found = false; unordered_set<int> s; int n, k ,a; int result = 0; scanf("%d%d",&n,&k); for (int i=0; i<n; i++) { scanf("%d",&a); if (s.find(a) == s.end()) { result += i-s.size(); s.insert(a); } if (s.size() == k) { found = true; break; } } if (found) printf("%d",result); else printf("-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 | #include <cstdio> #include <iostream> #include <vector> #include <unordered_set> #include <algorithm> using namespace std; int main() { bool found = false; unordered_set<int> s; int n, k ,a; int result = 0; scanf("%d%d",&n,&k); for (int i=0; i<n; i++) { scanf("%d",&a); if (s.find(a) == s.end()) { result += i-s.size(); s.insert(a); } if (s.size() == k) { found = true; break; } } if (found) printf("%d",result); else printf("-1"); } |