#include <iostream> #include <vector> #include <set> using namespace std; vector<int> pos[500005]; set<pair<int,int>> bottles; int main(){ int n, k; cin >> n >> k; for (int i = 0; i < n; i++){ int a; cin >> a; bottles.insert(make_pair(i, a)); pos[a].push_back(i); } long long res = 0; int sat = 0; for (int i = 0; i < k; i++){ if (bottles.empty()){ cout << "-1" << "\n"; return 0; } pair<int,int> c = *bottles.begin(); res += c.first - i; for (int j = 0; j < pos[c.second].size(); j++){ bottles.erase(make_pair(pos[c.second][j], c.second)); } } cout << res << "\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 | #include <iostream> #include <vector> #include <set> using namespace std; vector<int> pos[500005]; set<pair<int,int>> bottles; int main(){ int n, k; cin >> n >> k; for (int i = 0; i < n; i++){ int a; cin >> a; bottles.insert(make_pair(i, a)); pos[a].push_back(i); } long long res = 0; int sat = 0; for (int i = 0; i < k; i++){ if (bottles.empty()){ cout << "-1" << "\n"; return 0; } pair<int,int> c = *bottles.begin(); res += c.first - i; for (int j = 0; j < pos[c.second].size(); j++){ bottles.erase(make_pair(pos[c.second][j], c.second)); } } cout << res << "\n"; } |