Usaco 輕軌 貪心

傳送門
  有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+5
using 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;
}

這裏寫圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章