您的位置:首页 > 其它

UVaLive 4254 Processor (二分+优先队列)

2017-03-11 10:50 417 查看
题意:有n个任务,每个任务有三个参数,r,d,w,表示该任务必须在[r,d]之间执行,工作量是w,处理器执行速度可以变化,当执行速度是s的时候,

一个工作量是w的任务需要需要的执行时间是w/s个工作单位,另外,任务不一定要连续的执行,可以分成若干块,求出处理器执行过程中最大速度的最小值,

速度必须是整数。

析:首先是二分这个速度,然后去判断是不是成立,判断的时候要用到优先队列,把把所有的任务按开始时间排序,然后去枚举每个时间点,

用优先队列去维护一个结束时间早的优先,在每个时间点判断是不是能完成任务,完不成就是不是成立,完成就继续向下枚举。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#define debug() puts("++++");
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e4 + 5;
const int mod = 2000;
const int dr[] = {-1, 1, 0, 0};
const int dc[] = {0, 0, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Node{
int l, r, w;
bool operator < (const Node &p) const{
return r > p.r;
}
};
Node a[maxn];
inline int cmp(const Node &lhs, const Node &rhs){  return lhs.l < rhs.l;  }

bool judge(int mid){
priority_queue<Node> pq;
int cnt = 0;
for(int i = 2; i <= m; ++i){
while(cnt < n && a[cnt].l < i) pq.push(a[cnt++]);
int tmp = mid;
while(tmp && pq.size()){
Node u = pq.top();  pq.pop();
if(u.r < i)  return false;
if(tmp >= u.w)  tmp -= u.w;
else{
u.w -= tmp;
pq.push(u);
break;
}
}
}
return cnt == n && pq.empty();
}

int solve(){
int l = 1, r = 10000;
while(l < r){
int mid = (l + r) >> 1;
if(judge(mid))  r = mid;
else l = mid + 1;
}
return l;
}

int main(){
int T;  cin >> T;
while(T--){
scanf("%d", &n);
m = 1;
for(int i = 0; i < n; ++i){
scanf("%d %d %d", &a[i].l, &a[i].r, &a[i].w);
m = max(m, a[i].r);
}
sort(a, a+n, cmp);
printf("%d\n", solve());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: