2022.5.4 AcWing每日一题

枚举 + 贪心

枚举使用优惠卷的礼物,买下,然后每次贪心地选取总花费最小的礼物,知道买不了。

为什么用优惠券一定是对的?
因为 如果有一次买东西的时候,不用优惠卷,那用优惠卷一定不会比这个结果更差;所以,每次只需要枚举使用优惠卷,也即每次枚举一个物品,并用优惠券买下的情况,再贪心。

#include <bits/stdc++.h>
using namespace std;

const int N = 1100;

struct Node {
	int p, s, tot;
} p[N];
int n, b, ans;

bool cmp(Node a, Node b) {
	return a.tot < b.tot;
}

int main() {
	cin >> n >> b;
	for (int i = 0; i < n; i++) {
		cin >> p[i].p >> p[i].s;
		p[i].tot = p[i].p + p[i].s;
	}

	sort(p, p + n, cmp);

	for (int i = 0; i < n; i++) {
		int tmp = b, cnt = 0;
		if (tmp >= p[i].p / 2 + p[i].s) {
			tmp -= p[i].p / 2 + p[i].s;
			cnt++;
		}
		for (int j = 0; j < n; j++) {
			if (j != i && tmp >= p[j].tot) {
				tmp -= p[j].tot;
				cnt++;
			}
		}
		ans = max(ans, cnt);
	}
	cout << ans << endl;
	return 0;
}

posted @ 2022-05-04 09:34  superPG  阅读(11)  评论(0编辑  收藏  举报