Usaco 轻轨 贪心

来源:互联网 发布:淘宝店铺怎么注销关闭 编辑:程序博客网 时间:2024/04/17 03:21

传送门
  有N(1<=N<=20,000)个站点的轻轨站,有一个容量为C(1<=C<=100)的列车起点在1号站点,终点在N号站点,有K(K<=50,000)组牛群,每组数量为M_i(1<=M_i<=N),行程起点和终点分别为S_i和E_i(1<=S_i < E_i<=N)请 计算最多有多少头牛可以搭乘轻轨。对于每一批牛,可以只上一部分,全上,或者全不上

你难以想象如果最后一句没翻译对我造成了多大打击

只需要大胆YY
对于相同的起点的两批牛,当然让他们中先下去的下去
对于任意一个时刻,如果车在此时已经满了,那么我们可以进行回退
即将将在他下车站以后才会下车而现在在车上的牛T下去
显然这回更优,因为可以给后面的牛腾地方

可以用维护一个will单调的队列,每次从后面开始抉择,但事实上市只需要写一个优化的暴力
调试信息懒得删……

代码如下

#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#define N 50000+5using namespace std;inline int read(){    int x = 0, f = 1; char ch = getchar();    while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); }    while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }    return x * f;}int will[N],n,k,c,now,ans,MAX;struct group{    int from ,to ,num;    bool operator < (const group &z )const    {        return from^z.from?from<z.from:to<z.to;    }    void out(int i)    {        printf("group %d from %d to %d  %dcows\n",i,from,to,num);    }    void R()    {        from=read(),to=read(),num=read();    }}g[N];void solve(int i){    if(now+g[i].num<=c)    {        now+=g[i].num;        will[g[i].to]+=g[i].num;        MAX=max(MAX,g[i].to);        //printf("%d cows have come into the train ,they will get off at %d\n",g[i].num,g[i].to);        return ;    }    else    {        int kk=now+g[i].num-c;        for(int j=MAX;j>g[i].to&&kk>0; j--)                 if(will[j])            {                int ll=will[j];                will[j]=max(0,will[j]-kk);                MAX=j;                ll-=will[j];                //printf("%d cows have get off the train at the stop %d  beacuse of %d .\n",ll,j,g[i].from);                kk-=ll;            }        will[g[i].to]+=g[i].num-kk;        //printf("now=%d  %d cows have get on the bus ,they will get off at stop %d\n",now,c-now,g[i].to);        now=c;        MAX=max(MAX,g[i].to);    }}int main(){    //freopen("0.in","r",stdin);    //freopen("01.out","w",stdout);    cin>>k>>n>>c;    for(int i=1;i<=k;++i)g[i].R();    sort(g+1,g+k+1);    //for(int i=1;i<=k;++i)g[i].out(i);    int train=1;    for(int i=1;i<=n;++i)    {        if(will[i])        {            ans+=will[i],now-=will[i];            //printf("%d cows succeed at the stop %d.\n",will[i],i);            will[i]=0;        }        while(g[train].from == i)        {            solve(train);            train++;        }    }    cout<<ans;}

这里写图片描述

1 0