#include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 500005; int n, k, tab[N], is[N], rem[N], i, j; LL res; int main() { scanf("%d %d", &n, &k); for (i=0;i<n;i++) scanf("%d", &tab[i]); for (i=0;i<n && k > 0;i++) { if (!is[tab[i]]) { is[tab[i]] = 1; k--; } else rem[i] = 1; } if (k > 0) { printf("-1\n"); return 0; } res = 0; i--; for (j = i - 1;j>=0;j--) if (rem[j]) { res += (i - j); i--; } 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 32 33 34 | #include <bits/stdc++.h> using namespace std; typedef vector<int> VI; typedef pair <int,int> ii; typedef long long LL; #define pb push_back const int INF = 2147483647; const int N = 500005; int n, k, tab[N], is[N], rem[N], i, j; LL res; int main() { scanf("%d %d", &n, &k); for (i=0;i<n;i++) scanf("%d", &tab[i]); for (i=0;i<n && k > 0;i++) { if (!is[tab[i]]) { is[tab[i]] = 1; k--; } else rem[i] = 1; } if (k > 0) { printf("-1\n"); return 0; } res = 0; i--; for (j = i - 1;j>=0;j--) if (rem[j]) { res += (i - j); i--; } printf("%lld\n", res); return 0; } |