#include <cstdio> #include <unordered_set> using namespace std; int nextInt() { int n; scanf("%d", &n); return n; } long long solve() { int n = nextInt(); int k = nextInt(); unordered_set<int> data; long long res = 0 - k; res *= k - 1; res /= 2; for (int i = 0; i < n; ++i) { int x = nextInt(); if (data.count(x) > 0) continue; res += i; data.insert(x); if (data.size() == k) return res; } return -1; } int main() { long long res = solve(); printf("%lld\n", res); 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 | #include <cstdio> #include <unordered_set> using namespace std; int nextInt() { int n; scanf("%d", &n); return n; } long long solve() { int n = nextInt(); int k = nextInt(); unordered_set<int> data; long long res = 0 - k; res *= k - 1; res /= 2; for (int i = 0; i < n; ++i) { int x = nextInt(); if (data.count(x) > 0) continue; res += i; data.insert(x); if (data.size() == k) return res; } return -1; } int main() { long long res = solve(); printf("%lld\n", res); return 0; } |