UVA 1422 - Processor (二分+贪心+优先队列)
来源:互联网 发布:社交推荐算法 编辑:程序博客网 时间:2024/05/02 00:58
先对开始时间进行排序,在利用优先队列是结束时间早点先出队;
因为时间只有20000,我们可以去枚举每个单位时间,看要给分配给那个任务,
如果某个时间队列中还有结束时间大于枚举的时间,就跳出判断是在mid的右边。
#include<stdio.h>
#include<iostream>#include<string.h>
#include<algorithm>
#include<stdlib.h>
#include<math.h>
#include<queue>
using namespace std;
int t,n;
struct node
{
double r,d,w;
bool operator<(const node&tt) const{
return d>tt.d;
}
}work[10005];
//bool cmp(node a,node b)
//{
//return a.r<b,r;
//}
bool cmp(node a, node b) {
return a.r<b.r;
}
void init()
{
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%lf%lf%lf",&work[i].r,&work[i].d,&work[i].w);
sort(work,work+n,cmp);
}
int judge(int mid)
{
priority_queue<node> q;
int wn=0;
for(int i=1;i<=20000;i++)
{
int sum=mid;
while(work[wn].r<i&&wn!=n)//开始时间小于i&&任务没全判断完,入队
q.push(work[wn++]);
while(sum!=0&&!q.empty())
{
node save=q.top();q.pop();
if(i>save.d) return false;
if(save.w>sum)//任务时间大于给的速率
{
save.w-=sum;
sum=0;
q.push(save);
}
else
sum-=save.w;
}
if(wn==n&&q.empty()) break;
}
if(wn==n&&q.empty())
return true;
return false;
}
int solve()
{
int l=0,r=10000000,mid;
while(l<r)
{
if(r - l == 1) break;
mid=(l+r)>>1;
if(!judge(mid)) l=mid;
else
r=mid;
}
return r;
}
int main()
{
scanf("%d",&t);
while(t--)
{
init();
printf("%d\n",solve());
}
return 0;
}
0 0
- UVA 1422 - Processor (二分+贪心+优先队列)
- UVA 1422 - Processor (二分+贪心+优先队列)
- UVA 1422 Processor(优先队列+贪心+二分)
- (intermediate) UVA 贪心+二分+优先队列 1422Processor
- UVa 1422 - Processor(二分 + 贪心 + 优先队列)
- UVA 1422 - Processor(二分+优先队列+贪心)
- uva 1422 - Processor(二分+优先队列)
- LA 4254 Processor(二分+贪心+优先队列)
- UVA 1422 Processor(二分极大值极小化+优先队列)
- UVA-1422(贪心+二分+优先队列)
- uva1422 - Processor 二分+优先队列
- Processor(二分搜索,优先队列)
- UVALive - 4254 Processor 二分+优先队列
- UVA-10954 贪心+优先队列
- LA4254 Processor (贪心+二分)
- UVa 1422 Processor 解题报告(二分)
- UVa 10954 - Add All 贪心+优先队列
- UVA 11134 - Fabled Rooks(贪心+优先队列)
- OAuth的学习
- POJ 1062 昂贵的聘礼
- Qt 的学习
- 有一字符串,包含数字与字母,编程去除数字。
- linux--管线命令
- UVA 1422 - Processor (二分+贪心+优先队列)
- 首字母变大写(杭电2026)
- HDU-#1269 迷宫城堡(Tarjan+强连通)
- poj3070Fibonacci(矩阵快速幂)
- java中的环境变量设置---理解
- 数据库基本概念
- PSA: 从数组移除对象
- HDU 1754 I Hate It (线段树)
- 【第三回】使用OCCT做类似AutoCAD的拉伸功能(Extrude)