#include <bits/stdc++.h> #define REP(i,n) for (int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for (int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) std::cerr << "TRACE(" #x ")" << std::endl; #define DEBUG(x) std::cerr << #x << " = " << (x) << std::endl; void init_io() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } int main() { init_io(); int n, want; std::cin >> n >> want; std::vector<bool> seen(n); std::int64_t cost = 0; int have = 0; REP(i, n) { if (have == want) break; int brand; std::cin >> brand; --brand; if (!seen[brand]) { seen[brand] = true; cost += i - have; ++have; } } if (have < want) cost = -1; std::cout << cost << '\n'; }
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 | #include <bits/stdc++.h> #define REP(i,n) for (int _n=(n), i=0;i<_n;++i) #define FOR(i,a,b) for (int i=(a),_b=(b);i<=_b;++i) #define FORD(i,a,b) for (int i=(a),_b=(b);i>=_b;--i) #define TRACE(x) std::cerr << "TRACE(" #x ")" << std::endl; #define DEBUG(x) std::cerr << #x << " = " << (x) << std::endl; void init_io() { std::cin.tie(nullptr); std::ios::sync_with_stdio(false); } int main() { init_io(); int n, want; std::cin >> n >> want; std::vector<bool> seen(n); std::int64_t cost = 0; int have = 0; REP(i, n) { if (have == want) break; int brand; std::cin >> brand; --brand; if (!seen[brand]) { seen[brand] = true; cost += i - have; ++have; } } if (have < want) cost = -1; std::cout << cost << '\n'; } |