#include <bits/stdc++.h> using namespace std; #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define rep(i, a, n) for (int i = a; i <= n; i++) #define repV(i, a, n) for (int i = n; i >= a; --i) #define st first #define nd second #define pb push_back #define mp make_pair #define all(a) a.begin(), a.end() typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair<int, int> pii; constexpr int M = 5e5 + 5; int t[M], n, k, pop = 1, ans, idx = 1; bool check, odw[M]; void solve() { cin >> n >> k; rep(i, 1, n) { cin >> t[i]; } while (idx <= k) { check = 0; rep(i, pop, n) { if (!odw[t[i]]) { check = 1; pop = i; odw[t[i]] = 1; ans += (i - idx); idx++; } } if (!check) { cout << "-1\n"; return; } } cout << ans << "\n"; } int main() { IOS solve(); }
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 47 48 49 50 51 52 53 54 55 56 | #include <bits/stdc++.h> using namespace std; #define IOS \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); #define rep(i, a, n) for (int i = a; i <= n; i++) #define repV(i, a, n) for (int i = n; i >= a; --i) #define st first #define nd second #define pb push_back #define mp make_pair #define all(a) a.begin(), a.end() typedef long long ll; typedef long double ld; typedef unsigned long long ull; typedef pair<int, int> pii; constexpr int M = 5e5 + 5; int t[M], n, k, pop = 1, ans, idx = 1; bool check, odw[M]; void solve() { cin >> n >> k; rep(i, 1, n) { cin >> t[i]; } while (idx <= k) { check = 0; rep(i, pop, n) { if (!odw[t[i]]) { check = 1; pop = i; odw[t[i]] = 1; ans += (i - idx); idx++; } } if (!check) { cout << "-1\n"; return; } } cout << ans << "\n"; } int main() { IOS solve(); } |