# 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）请　计算最多有多少头牛可以搭乘轻轨。对于每一批牛，可以只上一部分，全上，或者全不上

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


#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#define N 50000+5
using namespace std;
{
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()
{
}
}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;
}

• 本文已收录于以下专栏：

举报原因： 您举报文章：Usaco 轻轨 贪心 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)