#include <iostream> #include <vector> void solve_dataset(); int main() { std::ios_base::sync_with_stdio(false); solve_dataset(); return 0; } void solve_dataset() { long long bottle_count, desired_distinct_count; std::cin >> bottle_count >> desired_distinct_count; std::vector<bool> already_present(bottle_count + 1, false); long long next_position_to_fill = 0; long long seconds = 0; for(long long position = 0; position < bottle_count; position++) { long long brand; std::cin >> brand; if(already_present[brand]) continue; seconds += position - next_position_to_fill; if(++next_position_to_fill >= desired_distinct_count) break; already_present[brand] = true; } const long long answer = next_position_to_fill < desired_distinct_count ? -1 : seconds; std::cout << answer << std::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 | #include <iostream> #include <vector> void solve_dataset(); int main() { std::ios_base::sync_with_stdio(false); solve_dataset(); return 0; } void solve_dataset() { long long bottle_count, desired_distinct_count; std::cin >> bottle_count >> desired_distinct_count; std::vector<bool> already_present(bottle_count + 1, false); long long next_position_to_fill = 0; long long seconds = 0; for(long long position = 0; position < bottle_count; position++) { long long brand; std::cin >> brand; if(already_present[brand]) continue; seconds += position - next_position_to_fill; if(++next_position_to_fill >= desired_distinct_count) break; already_present[brand] = true; } const long long answer = next_position_to_fill < desired_distinct_count ? -1 : seconds; std::cout << answer << std::endl; } |