闯关|贪心
2026-04-05 10:37:17
发布于:河北
3阅读
0回复
0点赞
废话少说,上代码
#include<iostream>
#include<algorithm>
using namespace std;
// 定义龙的结构体,包含力量x和奖励y
struct dragon{
int x;
int y;
}a[1001];
// 比较函数,按力量x升序排序
bool cmp(dragon a,dragon b){
return a.x<b.x;
}
int main(){
// 优化输入输出速度
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t=0,s=0,n=0; // t:测试用例数 s:初始力量值 n:龙的数量
cin>>t;
bool can_win=true; // 标记是否能获胜
while(t--){ // 处理每个测试用例
cin>>s>>n; // 输入初始力量和龙数量
// 读入每条龙的信息(力量和奖励)
for(int i=0;i<n;++i)
cin>>a[i].x>>a[i].y;
// 按龙的力量升序排序
sort(a,a+n,cmp);
// 遍历排序后的龙
for(int i=0;i<n;++i){
if(a[i].x>=s){ // 如果当前龙的力量大于等于自己的力量,无法战胜
can_win=false;
break;
}else s+=a[i].y; // 否则战胜龙,获得奖励y,更新力量值
}
// 输出结果
cout<<(can_win?"YES\n":"NO\n");
can_win=true; // 重置标记
}
return 0;
}
这里空空如也








有帮助,赞一个