您的位置:首页 > 其它

LA4254 Processor (贪心+二分)

2016-09-15 20:31 417 查看
https://acm.bnu.edu.cn/v3/external/42/4254.pdf

题解:

这道题首先肯定是个二分,但是怎么判断这个速度是否合法我想了很久。

我发现不管怎么贪心都是有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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: