#include <iostream> #include <unordered_set> namespace { using std::cin; using std::cout; using std::unordered_set; } int main() { size_t n, k; unordered_set<size_t> brand_set; size_t next_bottle = 0; size_t next_brand = 0; size_t read_bottle; size_t seconds = 0; cin >> n; cin >> k; do { cin >> read_bottle; if (brand_set.insert(read_bottle).second) { seconds += next_bottle - next_brand; ++next_brand; } ++next_bottle; } while (next_brand < k && next_bottle < n); if (next_bottle >= n && next_brand < k) cout << -1; else cout << seconds; 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 35 36 37 38 39 | #include <iostream> #include <unordered_set> namespace { using std::cin; using std::cout; using std::unordered_set; } int main() { size_t n, k; unordered_set<size_t> brand_set; size_t next_bottle = 0; size_t next_brand = 0; size_t read_bottle; size_t seconds = 0; cin >> n; cin >> k; do { cin >> read_bottle; if (brand_set.insert(read_bottle).second) { seconds += next_bottle - next_brand; ++next_brand; } ++next_bottle; } while (next_brand < k && next_bottle < n); if (next_bottle >= n && next_brand < k) cout << -1; else cout << seconds; return 0; } |