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;
}
本文作者:superPG
本文链接:https://www.cnblogs.com/superPG/p/16220393.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步