二分

Processor

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=90648#problem/B

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#define RPE(i,n) for(int i=0;i<(n);i++)
using namespace std;
const int maxn=1e4+10;
int n;
struct node
{
   int s,e,w;
   bool operator < (const node & another) const
   {
       return e>another.e;
   }
}a[maxn];
priority_queue<struct node> q;

bool cmp(node aa,node bb)
{
 return aa.s<bb.s;
}

bool solve(int mid)
{
    int i=0,j=0;
    while(!q.empty()) q.pop();
    while(1)
    {
    while(i<n&&a[i].s<=j) q.push(a[i++]);
    int now=mid;
    while(now!=0&&!q.empty())
    {
        node cc=q.top();
        q.pop();
        int m=min(now,cc.w);  //如果M时间比当前任务要求时间小,那就不对
        now-=m;               //now就会为0
        cc.w-=m;
        if(cc.w!=0) 
        q.push(cc);     //如果解决了,一定不会进队列
    }
    j++;
    if(!q.empty()&&q.top().e<=j)  //当前任务结束时间在当前时间
        return false;            //之前(j是计时器)M太小不够用
    if(q.empty()&&i==n)          //所有进程都能解决
        return true;             //M大的足够用
    }
}

int main()
{
   int T;
   cin>>T;
   while(T--)
   {
      cin>>n;
      int sum=0;
      RPE(i,n) {cin>>a[i].s>>a[i].e>>a[i].w;sum+=a[i].w;}
      sort(a,a+n,cmp);
      int l=1,r=sum,ans;
      while(l<=r)
      {
         int M=(l+r)/2;
         if(solve(M)) {ans=M;r=M-1;}  //solve:检测M秒是否够用
         else         l=M+1;   //返回1说明M大了(够用),返回0说明小了
      }                         //(不够用)
      cout<<ans<<endl;
   }
   return 0;
}
View Code
原文地址:https://www.cnblogs.com/zsyacm666666/p/4805587.html