#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define SZ(a) ((int)(a).size())
#define ALL(a) (a).begin(), (a).end()
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
constexpr int kInf = 1000 * 1000 * 1000 + 7;
constexpr long long kInfLL = 1e18;
// #include <atcoder/fenwicktree>
// using namespace atcoder;
// using mint = modint998244353;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll result = 0;
int n, k;
cin >> n >> k;
vi a(n);
REP(i, n) cin >> a[i];
set<int> s;
REP(i, n) {
int x = SZ(s);
s.insert(a[i]);
if (x < k && x < SZ(s)) {
result += i - x;
}
}
if (SZ(s) < k) {
cout << -1 << endl;
return 0;
}
cout << result << endl;
}
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 40 41 42 43 44 45 46 | #include <bits/stdc++.h> using namespace std; #define REP(i, n) for (int i = 0; i < (n); ++i) #define SZ(a) ((int)(a).size()) #define ALL(a) (a).begin(), (a).end() using ll = long long; using vi = vector<int>; using vvi = vector<vi>; constexpr int kInf = 1000 * 1000 * 1000 + 7; constexpr long long kInfLL = 1e18; // #include <atcoder/fenwicktree> // using namespace atcoder; // using mint = modint998244353; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); ll result = 0; int n, k; cin >> n >> k; vi a(n); REP(i, n) cin >> a[i]; set<int> s; REP(i, n) { int x = SZ(s); s.insert(a[i]); if (x < k && x < SZ(s)) { result += i - x; } } if (SZ(s) < k) { cout << -1 << endl; return 0; } cout << result << endl; } |
English