51nod 1351 吃点心(贪心)
2017-12-14 21:07
127 查看
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1351
题意:
思路:
要么先选low值大的,要么先选high值大的,分两种情况讨论。
每次只要选了的low值和>=x或者c-未选的high值和>=x就肯定满足了。
题意:
思路:
要么先选low值大的,要么先选high值大的,分两种情况讨论。
每次只要选了的low值和>=x或者c-未选的high值和>=x就肯定满足了。
#include<iostream> #include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; const int maxn = 50+5; int n,c,x,lowtot,hightot; struct node { int low,high; bool operator< (const node& rhs) const { return low > rhs.low; } }a[maxn]; bool cmp(node a, node b) { return a.high>b.high; } int main() { //freopen("in.txt","r",stdin); int T; scanf("%d",&T); while(T--) { lowtot = hightot = 0; scanf("%d%d%d",&n,&c,&x); for(int i=1;i<=n;i++) { scanf("%d%d",&a[i].low,&a[i].high); lowtot += a[i].low; hightot += a[i].high; } int sum = 0; int ans = 0; sort(a+1,a+n+1); int tmp = hightot; for(int i=1;i<=n;i++) { sum += a[i].low; ans++; hightot -= a[i].high; if(sum>=x || c-hightot>=x) break; } sum = 0; int ans2 = 0; sort(a+1,a+n+1,cmp); hightot = tmp; for(int i=1;i<=n;i++) { sum += a[i].low; ans2++; hightot -= a[i].high; if(sum>=x || c-hightot>=x) break; } printf("%d\n",min(ans,ans2)); } return 0; }
相关文章推荐
- 51NOD 1351 吃点心 贪心
- 51Nod-1351-吃点心
- 51nod oj 1390 游戏得分 1351 吃点心【贪心】
- 51nod-1351:吃点心
- 51nod-1351 吃点心
- 51nod 1449 砝码称重(进制思想+贪心)
- 51Nod:独木舟问题(贪心)
- 51nod--贪心入门
- 51nod 1672 贪心/队列
- [贪心 DP] 51Nod 1241 特殊的排序
- 51nod 1163 贪心+优先队列
- 贪心算法基础之任务执行顺序 51nod 贪心教程
- 51nod 1099 任务执行顺序(贪心)
- 51nod 1128 正整数分组 V 二分+贪心
- 【贪心】51Nod 1476 括号序列的最小代价
- 51nod 1672 区间交(贪心)
- 51nod 1378 夹克老爷的愤怒(树型dp+贪心)
- 51nod 1133【贪心】
- 51nod 1125[交换机器的最小代价]【贪心】
- 51nod 1091 线段的重叠(贪心)