题解:P11268 【MX-S5-T2】买东西题

· · 题解

不是这个题真搞笑吧。

先按 a_i 排序。

维护每个可用的优惠劵,每次用优惠力度最大的。

当然,如果使用优惠劵,考虑反悔,即为有 a_i - b_i 的价值。

这个题做完了。

#include<bits/stdc++.h>
#define int long long
using namespace std;
string S;
const int N = 1e6 + 66;
pair<int , int> a[N] , b[N];
const int P = 1e9 + 7; 
signed main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
//  freopen("buy6.in" , "r" , stdin);   
//  freopen("buy.out", "w" , stdout);
    int n , m; 
    cin >> n >> m;
    int q = 0;
    for(int i = 1; i <= n; ++ i){
        cin >> a[i] . first >> a[i] . second;
        q += a[i] . first;
        a[i] . second = a[i] . first - a[i] . second; 
    }
    sort(a + 1 , a + n + 1);
    for(int i = 1; i <= m; ++ i){
        cin >> b[i] . first >> b[i] . second; 
    }
    sort(b + 1, b + m + 1);
    int t = 0;
    priority_queue<int> s;
    for(int i = 1; i <= n; ++ i){
        while(t < m && b[t + 1] . first <= a[i] . first){
            s . push(b[t + 1] . second); 
            ++ t;
        }
        if(! s . size() || a[i] . second > s . top()){
            q -= a[i] . second;
        }
        else{ 
            q -= s . top();
            s . pop();
            s . push(a[i] . second); 
        }
    }
    cout << q << '\n';
    return 0;  
}