LA 4254 这个为什么会超时?

#include<iostream>
#include<vector>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
struct Line
{
    int a,b,w;
};
vector <Line> line;
vector <Line> templine;
int n;
int workline[20010];
bool ok(int s)
{
    memset(workline,0,sizeof(workline));
    templine.resize(line.size());
    memcpy(&templine[0],&line[0],line.size()*sizeof(Line));

    for(int i=0;i<n;i++)
    {
    //    cout<<templine[i].a<<" "<<templine[i].b<<endl;
        for(int j=templine[i].a;j<templine[i].b;j++)
        {
            if(workline[j]<s)
            {
                if(templine[i].w>(s-workline[j]))
                {
                    int temp=s-workline[j];
                    workline[j]=s;
                    templine[i].w-=temp;
                }
                else
                {
                    workline[j]+=templine[i].w;
                    templine[i].w=0;
                }
            }
        }
        if(templine[i].w>0)
            return false;
    }
    return true;
}
bool cmp(Line aa,Line bb)
{
    return aa.b<bb.b;
}
int main()
{
    int T;
    //cin>>T;
    scanf("%d",&T);
    while(T--)
    {
        int t1,t2,t3;
        Line aa;
        //cin>>n;
        scanf("%d",&n);
        line.clear();
        for(int i=0;i<n;i++)
        {
            //cin>>t1>>t2>>t3;
            scanf("%d%d%d",&t1,&t2,&t3);
            aa.a=t1;
            aa.b=t2;
            aa.w=t3;
            line.push_back(aa);
        }
        sort(line.begin(),line.end(),cmp);

        int L=1,R=1000000,M;
        while(L<R)
        {
            if(R-L==1) break;
            M=(R+L)/2;
            if(ok(M)) R=M;
            else L=M;
            //cout<<R<<"*"<<endl;
        }
        cout<<R<<endl;
    }
    return 0;
}
View Code

 

posted @ 2014-10-26 21:05  名牌果果  阅读(184)  评论(0编辑  收藏  举报