Codeforces Hello 2018 - D - Too Easy Problems

说点小感想:自己嘛,确实没有很好的水平。9月开始做代码题以来,也大大小小做了千余题,却不能飘飘然。12月初开始打codeforces,也确实表明了我div2仅能做出5/8的水平。特别是期末考,求最长上升子序列的题都能wa..对于这样的我只能给自己一句话:2018也要付出满满的努力呀!
依然贪心…感觉我看什么都是贪心(・_・;)
①以时间排序,选择耗时最少的题目
②在①的基础上用优先队列维护答案,对于不满足老师要求的题目(ai < ans )我们剔除其中花费时间最多的,到能再次满足要求为止。
③在①和②的基础上,如果剩下的时间不足以完成接下来哪怕一道题了,就不必再作选择,这就是最优解。
注意贪心的顺序。

#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
struct prob{
    int least,sec,id;
}a[200005];
bool cmp(prob x,prob y){
    return x.sec<y.sec;
}
struct comp{
    bool operator()(const prob &x,const prob &y){
        if(x.least!=y.least)return x.least>y.least;return x.sec<y.sec;
    }
};
priority_queue<prob,vector<prob>,comp> an;
int main(){
    int n,ans=0,top=0,t;
    scanf("%d%d",&n,&t);
    for(int i=0;i<n;i++){
        scanf("%d%d",&a[i].least,&a[i].sec);
        a[i].id=i+1;
    }
    sort(a,a+n,cmp);
    for(int i=0;i<n;i++){
        if(t<a[i].sec)break;//策略③
        if(a[i].least>ans){//策略①
            t-=a[i].sec;
            an.push(a[i]);
            ans++;
            //printf("%d",ans);
        }
        while(!an.empty()&&an.top().least<ans){//策略②
            ans--;
            t+=an.top().sec;
            an.pop();
        }
    }
    printf("%d\n%d\n",ans,ans);
    while(!an.empty())printf("%d ",an.top().id),an.pop();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值