您的位置:首页 > 其它

LA 4254 Processor (单调队列 + 二分)

2016-04-25 21:21 393 查看
题意:有n个任务,每个任务有三个参数,ri,di,wi,表示在ri di之内需要执行完工作量为wi的任务,任务可以分块执行,求执行过程中的最大速度的最小值。速度需为整数。

分析:二分判断,判断有些复杂,要用到单调队列,写的挺烂的,但是一遍过了。

#include <queue>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <iostream>
#define MAXN 10002
using namespace std;
int n,T;
struct thing
{
double r,d,w;
} test[MAXN];
bool camp(thing a,thing b)
{
if(a.r == b.r) return a.d < b.d;
return a.r < b.r;
}
struct Camp
{
bool operator() (int a,int b)
{
return test[a].d > test[b].d;
}
};
priority_queue <int,vector<int>,Camp> q;
bool jud(double v,const thing test[])
{
thing Test[MAXN];
for(int i = 1;i <= n;i++) Test[i] = test[i];
while(!q.empty()) q.pop();
int now = 1,num = 0;
q.push(1);
double pos = Test[1].r;
while(!q.empty())
{
thing &u = Test[q.top()];
if(u.w/v > u.d - pos) return false;
double dis = now == n ? u.w/v : min(u.w/v,Test[now+1].r - pos);
pos += dis;
u.w -= dis*v;
if(pos == Test[now+1].r)
{
now++;
q.push(now);
}
if(u.w < 0.000001)
{
q.pop();
num++;
}
if(q.empty() && num != n)
{
now++;
pos = Test[now].r;
q.push(now);
}
}
return true;
}
int main()
{
cin.sync_with_stdio(false);
cin>>T;
while(T--)
{
cin>>n;
for(int i = 1;i <= n;i++)
cin>>test[i].r>>test[i].d>>test[i].w;
sort(test+1,test+1+n,camp);
int s = 1,t = 10001;
while(s != t)
{
int mid = (s + t) / 2;
if(jud(mid,test)) t = mid;
else s = mid + 1;
}
cout<<s<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: