滴滴餐桌求解
做了多关键字排序,然后先分配做人少的桌子,只ac 50%,不知道漏了哪些情况。
#include <iostream> #include <vector> #include <algorithm> using namespace std; bool cmp(pair<int, int> a, pair<int, int> b) { return (a.first < b.first) || (a.first == b.first && a.second > b.second); } int main() { int n, m; while (cin >> n >> m) { // input data vector<int> tc(n); for (int i = 0; i < n; ++i) cin >> tc[i]; vector<pair<int, int>> guest; for (int i = 0; i < m; ++i) { int t1, t2; cin >> t1 >> t2; guest.push_back(make_pair(t1, t2)); } // sort sort(guest.begin(), guest.end(), cmp); sort(tc.begin(), tc.end()); int sum = 0; for (int i = 0; i < tc.size(); ++i) { int k = tc[i]; auto start = guest.begin(); auto tmp = start; int flag = 0; while (start != guest.end() && (*start).first <= k) { if ((*start).second > (*tmp).second) tmp = start; start++; flag = 1; } if (flag) { sum += (*tmp).second; (*tmp).second = 0; } } cout << sum << endl; } }