LA4254 Processor (贪心+二分)
2016-09-15 20:31
417 查看
https://acm.bnu.edu.cn/v3/external/42/4254.pdf
题解:
这道题首先肯定是个二分,但是怎么判断这个速度是否合法我想了很久。
我发现不管怎么贪心都是有bug的,后来看了题解了我才知道,对于这道题,正确的贪心思路应当是对于每个时刻,贪心的选择最早结束的任务进行处理。那么这道题就很好解决了。
这道题的贪心难以想到的地方就是对每个时刻贪心把,整体贪心就错了,但是每个时刻,我贪心选择最早结束的,也不会有其他方式比它更优。
代码中还有一个技巧,这道题本该使用浮点数的,但是经过代码中的处理避免了浮点数的运算。代码中的temp本该是速度,但是我们是枚举的每一秒的任务,所以可以直接减,为0了说明就是做完了。
题解:
这道题首先肯定是个二分,但是怎么判断这个速度是否合法我想了很久。
我发现不管怎么贪心都是有bug的,后来看了题解了我才知道,对于这道题,正确的贪心思路应当是对于每个时刻,贪心的选择最早结束的任务进行处理。那么这道题就很好解决了。
这道题的贪心难以想到的地方就是对每个时刻贪心把,整体贪心就错了,但是每个时刻,我贪心选择最早结束的,也不会有其他方式比它更优。
代码中还有一个技巧,这道题本该使用浮点数的,但是经过代码中的处理避免了浮点数的运算。代码中的temp本该是速度,但是我们是枚举的每一秒的任务,所以可以直接减,为0了说明就是做完了。
#include<bits/stdc++.h> using namespace std; const int maxn=1e5+5; const double inf=1e18; const double eps=1e-4; struct note { int l,r,w; // note(int lNum=0.00,double rNum=0.00,double wNum=0.00):l(lNum),r(rNum),w(wNum) {} bool operator<(const struct note &aa)const { return r>aa.r; } } A[maxn]; int maxL,maxR; bool Check(int maxV,int n) { priority_queue<note> que; while(!que.empty()) { que.pop(); } int pos=0; for(int i=maxL; i<=maxR; ++i) { int temp=maxV; while(pos<n&&(A[pos].l<i)) { que.push(A[pos++]); } while(!que.empty()&&temp) { note now=que.top(); que.pop(); if(now.r<i)return false; if(temp>=now.w) { temp-=now.w; } else { now.w-=temp; que.push(now); break; } } } return que.empty(); } bool Cmp(const struct note&aa,const struct note &bb) { return aa.l<bb.l; } int main() { #ifdef tangge freopen("4254.in","r",stdin); #endif // tangge int T,n; scanf("%d",&T); while(T--) { scanf("%d",&n); maxR=0; maxL=1000000; int sumW=0; for(int i=0; i<n; ++i) { scanf("%d%d%d",&A[i].l,&A[i].r,&A[i].w); maxR=max(maxR,A[i].r); maxL=min(maxL,A[i].l); sumW+=A[i].w; } sort(A,A+n,Cmp); int L=0,R=sumW,Mid,ans=100000000; while(L<=R) { Mid=(L+R)>>1; if(Check(Mid,n)) { R=Mid-1; ans=min(ans,Mid); } else L=Mid+1; } printf("%d\n",ans); } return 0; }
相关文章推荐
- UVA 1422 - Processor (二分+贪心+优先队列)
- UVA 1422 - Processor (二分+贪心+优先队列)
- UVa 1422 - Processor(二分 + 贪心 + 优先队列)
- LA 4254 Processor 贪心+二分 *
- UVA 1422 - Processor(二分+优先队列+贪心)
- LA 4254 Processor 处理器 【二分 贪心 优先队列】
- (intermediate) UVA 贪心+二分+优先队列 1422 Processor
- UVA 1422 Processor(优先队列+贪心+二分)
- LA 4254 Processor (二分+贪心)
- LA 4254 Processor(二分+贪心+优先队列)
- POJ 2976 Dropping tests (二分+贪心)
- NYOJ586. 疯牛(贪心+二分搜索)
- HDU 5090 Game with Pearls【贪心||模拟||二分匹配】
- codeforces 609D D. Gadgets for dollars and pounds(二分+贪心)
- 二分+贪心-uva-1335 - Beijing Guards
- 列车调度(贪心+二分)
- HDU 4343 D - Interval query 二分贪心
- POj-3104 Drying 二分+贪心
- (ZOJ) 3334 二分+贪心(二分double的写法)
- HDU4791->贪心&&二分优化