您的位置:首页 > 其它

Codeforces Hello 2018 - D - Too Easy Problems

2018-01-09 01:23 363 查看
说点小感想:自己嘛,确实没有很好的水平。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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: