UVa 1422 - Processor(二分 + 贪心 + 优先队列)
来源:互联网 发布:jsp人才系统源码 编辑:程序博客网 时间:2024/05/02 04:39
题意
有N个任务,每个任务只能在开始时间到结束时间之内完成,有任务量。完成所需时间是w / 处理器速度。
输出处理器速度最大值的最小值。
思路
虽然一看就知道是二分,但是怎么Check想了一整天都没想出来。看了别人的题解。
思路是一秒一秒来算。
建一个优先队列,结束时间越短越优先,因为要最快完成。
一秒内能处理的任务量就是处理器的速度mid。
出队,如果能处理完就减掉,不能处理完就减一下任务量,因为任务还木有完成,所以还得入队。
一直重复,直到全部都出队。
代码
#include <cstdio>
#include <queue>
#include <algorithm>
#define LL long long
#define lowbit(x) ((x) & (-x))
const int MAXN = 1e4 + 5;
const int INF = 0x3f3f3f3f;
using namespace std;
struct POINT
{
int st, ed, w;
bool operator < (const POINT &a) const
{
return ed > a.ed;
}
}pit[MAXN];
int n, vmax;
priority_queue<POINT> qu;
int cmp(const POINT &a, const POINT &b)
{
return a.st < b.st;
}
bool Check(int mid)
{
while (!qu.empty()) qu.pop();
int i, j;
int k = 0;
for (i = 1; i <= 20000; i++)
{
while (pit[k].st < i && k != n) qu.push(pit[k++]);
int curLev = mid;
while (!qu.empty() && curLev != 0)
{
POINT cur = qu.top(); qu.pop();
if (cur.ed < i) return false; //取出了过时未完成的任务。
if (cur.w > curLev)
{
cur.w -= curLev;
curLev = 0;
qu.push(cur);
}
else
curLev -= cur.w;
}
}
if (qu.empty() && k == n)
return true;
return false;
}
int Solve()
{
sort(pit, pit + n, cmp);
int l = 0, r = vmax, ans, mid;
while (l <= r)
{
mid = l + (r - l) / 2;
if (Check(mid))
{
ans = mid;
r = mid - 1;
}
else
l = mid + 1;
}
return ans;
}
int main()
{
//freopen("input.txt", "r", stdin);
int T, i, j;
scanf("%d", &T);
while (T--)
{
vmax = 0;
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%d%d%d", &pit[i].st, &pit[i].ed, &pit[i].w);
vmax += pit[i].w;
}
printf("%d\n", Solve());
}
return 0;
}
0 0
- UVA 1422 - Processor (二分+贪心+优先队列)
- UVA 1422 - Processor (二分+贪心+优先队列)
- UVA 1422 Processor(优先队列+贪心+二分)
- UVa 1422 - Processor(二分 + 贪心 + 优先队列)
- UVA 1422 - Processor(二分+优先队列+贪心)
- uva 1422 - Processor(二分+优先队列)
- (intermediate) UVA 贪心+二分+优先队列 1422Processor
- UVA 1422 Processor(二分极大值极小化+优先队列)
- UVA-1422(贪心+二分+优先队列)
- LA 4254 Processor(二分+贪心+优先队列)
- uva1422 - Processor 二分+优先队列
- Processor(二分搜索,优先队列)
- UVALive - 4254 Processor 二分+优先队列
- UVa 1422 Processor 解题报告(二分)
- UVA-10954 贪心+优先队列
- LA4254 Processor (贪心+二分)
- LA 4254 Processor (单调队列 + 二分)
- UVA 10954 Add All (贪心 + 优先队列)
- 第十二章 12.1.6节练习
- 编译hadoop 2.3的native library
- StrutsPrepareAndExecuteFilter的作用
- Unity3D连接MSSQL以及遇见的一个问题
- Cocosd-x3.1 Menu使用示例
- UVa 1422 - Processor(二分 + 贪心 + 优先队列)
- mysql---主从复制
- 找到第N个丑数
- javascript之数组操作
- Java网络编程从入门到精通(11):使用NetworkInterface类获得网络接口信息
- java命令模式
- 十大编程算法助程序员走上高手之路
- iOS中处理时间的类
- hdu 1520 Anniversary party(树形DP入门)