NOIP模拟 NYG的背包 [高山算法]

终于我的贪心过了,方法与题解不同,我每次记录历史最高值h,若当前x<=h则可以,然后判定几个特殊条件,AC了。

高山算法是什么?自行百度,本人无厘头命名,貌似是时间空间最优的方法。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100005;
struct data{
    long long x,y;
    bool operator < (const data &rhs)const{
        return x<rhs.x||(x==rhs.x&&(y-x)>(rhs.y-rhs.x));
    }
}a[N];
inline void read(long long &res){
    static char ch;long long flag=1;
    while((ch=getchar())<'0'||ch>'9')if(ch=='-')flag=-1;res=ch-48;
    while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-48;res*=flag;
}
long long t,n,w,h,flag,b;
int main(){
    freopen("backpack.in","r",stdin);
    freopen("backpack.out","w",stdout);
    read(t);
    while(t--){
        read(n),read(w);h=w;
        for(register int i=1;i<=n;i++)read(a[i].x),read(a[i].y);
        sort(a+1,a+1+n),flag=1;b=0x3f3f3f3f;
        for(register int i=1;i<=n;i++){
            if(h<a[i].x){cout<<"No"<<endl;flag=0;break;}
            w-=a[i].x;w+=a[i].y;
            h=max(h,w);b=min(b,a[i].y);
        }
        if(flag){
            if(w<b)cout<<"No"<<endl;
            else cout<<"Yes"<<endl;
        }
    }
    return 0;
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值