贪心 HDU3177

C++编程竞赛策略实现
本文展示了一个基于C++的编程竞赛题目解决方案,通过结构体排序及贪心算法判断是否能在给定条件下完成任务。利用标准库函数进行高效输入输出,并自定义比较函数对任务按特定条件排序。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cmath>
 5 
 6 using namespace std;
 7 
 8 struct node
 9 {
10     int a,b;
11     bool operator <(node n) const
12     {
13         return b-a<n.b-n.a;
14     }
15 }bn[1100];
16 
17 int main()
18 {
19     int T;
20     scanf("%d",&T);
21     while(T--)
22     {
23         int v,n;
24         scanf("%d%d",&v,&n);
25         for(int i=0;i<n;i++)
26         {
27             scanf("%d%d",&bn[i].a,&bn[i].b);
28         }
29         sort(bn,bn+n);
30         int flag=-1;
31         for(int i=n-1;i>=0;i--)
32         {
33             if(v>=bn[i].b)
34             {
35                 v-=bn[i].a;
36             }
37             else
38             {
39                 flag=0;
40                 break;
41             }
42         }
43         if(flag)
44             cout<<"Yes"<<endl;
45         else
46             cout<<"No"<<endl;
47     }
48     return 0;
49 }
View Code

 

转载于:https://www.cnblogs.com/wsruning/p/4752612.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值