1422 - Processor
Time limit: 3.000 seconds
An ``early adopter" Mr. Kim bought one of the latest notebooks which has a speed-controlled processor. The processor is able to operate at variable speed. But the higher the speed, the higher the power consumption is. So, to execute a set of programs, adjusting the speed of the processor dynamically results in energy-efficient schedules. We are concerned in a schedule to minimize the maximum speed of the processor.
The processor shall execute a set of programs and each program Pi is given having a starting time ri , a deadline di , and workwi . When the processor executes the programs, for each programPi , the work wi should be done on the processor within the interval[ri,di] to completePi . Also, the processor does not have to execute a program in a contiguous interval, that is, it can interrupt the currently running program and later resume it at the interrupted point. It is assumed thatri ,di , andwi are given positive integers. Recall that the processor can execute the programs at variable speed. If the processor runs the programPi with work wi at a constant speeds ,then it takes time to completePi . We also assume that the available speeds are positive integers, that is, the processor operates only at integer points of speed. The speed is unbounded and the processor may operate at sufficiently large speeds to complete all the programs. The processor should complete all the given programs and the goal is to find a schedule minimizing the maximum of the speeds at which the processor operates.
For example, there are five programs Pi with the interval[ri,di] and workwi ,i = 1,..., 5 , where[r1,d1] = [1, 4] ,[r2,d2] = [3, 6] ,[r3,d3] = [4, 5] ,[r4,d4] = [4, 7] ,[r5,d5] = [5, 8] andw1 = 2 ,w2 = 3 ,w3 = 2 ,w4 = 2 ,w5 = 1 . Then the Figure 1 represents a schedule which minimizes the maximum speed at which the processor operates. The maximum speed is 2 in this example.
![\epsfbox{p4254.eps}](https://i-blog.csdnimg.cn/blog_migrate/92891cc81c8f33ea45ec5eec6e664b9e.png)
Input
Your program is to read from standard input. The input consists of T test cases. The number of test cases T(1T
20) is given on the first line of the input. The first line of each test case contains an integern(1
n
10, 000) , the number of given programs which the processor shall execute. In the nextn lines of each test case, thei -th line contain three integer numbers ri ,di , andwi , representing the starting time, the deadline, and the work of the programPi , respectively, where1
ri <di
20, 000 ,1
wi
1, 000 .
Output
Your program is to write to standard output. Print exactly one line for each test case. The line contains the maximum speed of a schedule minimizing the maximum speed at which the processor operates to complete all the given programs.
Sample Input
3 5 1 4 2 3 6 3 4 5 2 4 7 2 5 8 1 6 1 7 25 4 8 10 7 10 5 8 11 5 10 13 10 11 13 5 8 15 18 10 20 24 16 8 15 33 11 14 14 1 6 16 16 19 12 3 5 12 22 25 10
Sample Output
2 5 7
解题报告: 最小的最大值,用二分。二分处理速度。因为最多有10000个任务,每个任务的任务量最多是1000,那么处理速度最大是10000000(所有任务同时),最小是1。
对于所有的任务,完成顺序应该是:谁离截止时间最近,先完成谁(贪心)。不过因为这样,就会有区间操作了。我们可以细分每个时间段。一秒可以处理的任务量为处理器的速度,初始化整个时间段,每秒有速度spd个任务量,然后根据完成的任务,依次减。如果不够,说明速度不够。
之前用double计算时间来实现,应该是因为精度的原因WA了。改成上面的做法就搞定了。代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <string>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
typedef long long LL;
typedef unsigned long long ULL;
void work();
int main()
{
#ifdef ACM
freopen("in.txt", "r", stdin);
#endif // ACM
work();
}
/***************************************************/
struct Node
{
int l, r, w;
bool operator<(const Node& cmp) const
{
return r==cmp.r ? l<cmp.l : r<cmp.r;
}
} node[11111];
int times[22222];
int n;
bool check(int spd)
{
ff(i, 20000)
times[i] = spd;
ff(i, n)
{
int w = node[i].w;
fff(j, node[i].l, node[i].r) if(times[j])
{
if(w>times[j])
{
w -= times[j];
times[j] = 0;
}
else
{
times[j] -= w;
w = 0;
break;
}
}
if(w > 0) return false;
}
return true;
}
void work()
{
int T;
scanf("%d", &T);
ff(cas, T)
{
scanf("%d", &n);
ff(i, n)
scanf("%d%d%d", &node[i].l, &node[i].r, &node[i].w), node[i].r--;
sort(node, node+n);
int l=1, r=10000000;
while(l<=r)
{
int m=(l+r)/2;
if(check(m))
r=m-1;
else
l=m+1;
}
printf("%d\n", l);
}
}
时间约2.5秒。感觉应该有更快的方法。
参看了其他人的做法。我们可以这么优化。按时间点递推。到达开始时间,把任务加入优先队列。优先队列中,选择离截止时间最短的任务完成。0.4s搞定。
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <string>
using namespace std;
#define ff(i, n) for(int i=0;i<(n);i++)
#define fff(i, n, m) for(int i=(n);i<=(m);i++)
#define dff(i, n, m) for(int i=(n);i>=(m);i--)
#define mem(a) memset((a), 0, sizeof(a))
typedef long long LL;
typedef unsigned long long ULL;
void work();
int main()
{
#ifdef ACM
freopen("in.txt", "r", stdin);
// freopen("in.txt", "w", stdout);
#endif // ACM
work();
}
/*****************************************/
struct Node
{
int l, r, w;
bool operator<(const Node & cmp) const
{
return r > cmp.r;
}
} x, y;
int n;
bool check(int spd, vector<Node> * vec)
{
priority_queue<Node> que;
ff(i, 22222)
{
ff(j, vec[i].size()) que.push(vec[i][j]);
if(que.size() == 0) continue;
if(que.top().r <= i) return false;
int ww = spd;
while(que.size() && ww)
{
x = que.top();
que.pop();
if(x.w <= ww)
{
ww -= x.w;
}
else
{
x.w -= ww;
ww = 0;
que.push(x);
}
}
}
return true;
}
void work()
{
int T;
scanf("%d", &T);
ff(cas, T)
{
vector<Node> vec[22222];
scanf("%d", &n);
ff(i, n)
{
scanf("%d%d%d", &x.l, &x.r, &x.w);
vec[x.l].push_back(x);
}
int l = 1, r = 1000000;
while(l <= r)
{
int m = (l+r)/2;
if(check(m, vec))
r = m - 1;
else
l = m + 1;
}
printf("%d\n", l);
}
}