贪心+排序,但是怎样贪呢?
设两个石头x y
从3个方面考虑:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e5+9;
typedef long long LL;
struct stone
{
int a;
int b;
}s[maxn];
bool cmp(const stone &a1,const stone &a2)
{
int x = a1.b-a1.a;
int y = a2.b-a2.a;
if(x>=0&&y>=0) return a1.a<a2.a;
if(x>=0&&y<0) return true;
if(x<0&&y>=0) return false;
if(x<0&&y<0) return a1.b>a2.b;
}
int main()
{
std::ios::sync_with_stdio(false);
int t;
cin>>t;
while(t--)
{
LL n,m;
cin>>n>>m;
memset(s,0,sizeof(s));
for(int i=0;i<n;i++)
{
cin>>s[i].a>>s[i].b;
}
sort(s,s+n,cmp);
int flag = 1;
for(int i=0;i<n;i++)
{
m-=s[i].a;
if(m<0)
{
printf("No\n");
flag = 0;
break;
}
m+=s[i].b;
}
if(flag) printf("Yes\n");
}
return 0;
}
本文地址:https://blog.csdn.net/qq_45769627/article/details/107201505
如对本文有疑问, 点击进行留言回复!!
Error running app: Default Activity Not Found
如何用HMS Nearby Service给自己的App添加近距离数据传输功能
BlockChain:2020年7月10日世界人工智能大会WAIC《链智未来 赋能产业区块链主题论坛》(四)
网友评论