跳到正文

题解

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

SafariMo约 2 分钟
原文
文章信息与版本

文章信息

发布
2024年11月9日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 2 分钟
题目
P11268

版本

  1. v1当前

不是这个题真搞笑吧。

先按 aia_i 排序。

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

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

这个题做完了。

C++
#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;  
}
原文