#include <bits/stdc++.h> using namespace std; typedef intmax_t I; constexpr I inf = 1e18; typedef pair<I, I> II; typedef double F; #define debug_printable(template_thingies, type, start, printer, stop) \ template <template_thingies> \ ostream &operator<<(ostream &o, const type &v) { \ for (auto i = (o << start, v.begin()); i != v.end(); ++i) \ (i != v.begin() && (o << ", ")), o << printer; \ return o << stop; \ } #define comma , debug_printable(typename T, vector<T>, "[", *i, "]"); debug_printable(typename T comma size_t N, array<T comma N>, "<", *i, ">"); debug_printable(typename T, set<T>, "{", *i, "}"); debug_printable(typename T comma typename U, map<T comma U>, "{", i->first << ": " << i->second, "}"); template <typename T, typename U> ostream &operator<<(ostream &o, pair<T, U> p) { return o << "(" << p.first << ", " << p.second << ")"; } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); I n, k; cin >> n >> k; vector<I> v(n); for (I i = 0; i < n; ++i) { cin >> v[i]; } I r = 0, j = 0; vector<bool> d(n); for (I i = 0; i < n; ++i) { if (d[v[i]]) continue; d[v[i]] = true; r += i - j; ++j; if (j == k) break; } cout << (j == k ? r : -1) << "\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 38 39 40 41 42 43 44 45 46 47 | #include <bits/stdc++.h> using namespace std; typedef intmax_t I; constexpr I inf = 1e18; typedef pair<I, I> II; typedef double F; #define debug_printable(template_thingies, type, start, printer, stop) \ template <template_thingies> \ ostream &operator<<(ostream &o, const type &v) { \ for (auto i = (o << start, v.begin()); i != v.end(); ++i) \ (i != v.begin() && (o << ", ")), o << printer; \ return o << stop; \ } #define comma , debug_printable(typename T, vector<T>, "[", *i, "]"); debug_printable(typename T comma size_t N, array<T comma N>, "<", *i, ">"); debug_printable(typename T, set<T>, "{", *i, "}"); debug_printable(typename T comma typename U, map<T comma U>, "{", i->first << ": " << i->second, "}"); template <typename T, typename U> ostream &operator<<(ostream &o, pair<T, U> p) { return o << "(" << p.first << ", " << p.second << ")"; } int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr); I n, k; cin >> n >> k; vector<I> v(n); for (I i = 0; i < n; ++i) { cin >> v[i]; } I r = 0, j = 0; vector<bool> d(n); for (I i = 0; i < n; ++i) { if (d[v[i]]) continue; d[v[i]] = true; r += i - j; ++j; if (j == k) break; } cout << (j == k ? r : -1) << "\n"; } |