题意:有n个任务,每个任务必须在在时刻[r, d]之内执行w的工作量(三个变量都是整数)。处理器执行的速度可以变化,当速度为s时,一个工作量为w的任务需要 执行的时间为w/s个单位时间。另外不一定要连续执行,可以分成若干块。求处理器在执行过程中最大速度的最小值。处理器速度为任意的整数值。
思路:起初的思路是整体贪心,就是把某个区间的任务量累加,然后对于当前区间能否满足限制速度,但是这样难以实现。第二个思路就是,对于某个时刻处理最快结束的(即右端点最小的),这显然是当前最优的选择。
代码:
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=1e4+10;
struct node
{
int l,r,w;
bool operator <(const node&aa)const
{
return r>aa.r;
}
}a[maxn];
int n;
bool cmp(node aa,node b)
{
return aa.l<b.l;
}
bool ok(int m)
{
priority_queue<node> q;
while(!q.empty())q.pop();
int t=1,pos=0;
while(!q.empty()||pos<n)
{
while(pos<n&&a[pos].l<t)q.push(a[pos++]);
int speed=m;
while(!q.empty()&&speed)
{
node now=q.top();q.pop();
if(now.r<t)return false;
if(speed>=now.w)speed-=now.w;
else
{
now.w-=speed;
q.push(now);
break;
}
}
t++;
}
return q.empty();
}
int main()
{
int t;
cin>>t;
while(t--)
{
cin>>n;
for(int i=0;i<n;i++)cin>>a[i].l>>a[i].r>>a[i].w;
sort(a,a+n,cmp);
int l=1,r=100000;
while(r>l)
{
int m=(l+r)/2;
if(ok(m))r=m;
else l=m+1;
//cout<<l<<" "<<r<<endl;
}
cout<<l<<endl;
}
system("pause");
return 0;
}
本文探讨了在给定任务集的情况下,如何通过优化处理器速度来确保所有任务在指定时间内完成的问题。采用了一种基于贪心策略的算法,通过优先处理最紧迫任务的方法,实现了对处理器最大速度最小值的有效求解。
1110

被折叠的 条评论
为什么被折叠?



