[USACO09FEB]Fair Shuttle G
题目描述
Although Farmer John has no problems walking around the fair to collect prizes or see the shows, his cows are not in such good shape; a full day of walking around the fair leaves them exhausted. To help them enjoy the fair, FJ has arranged for a shuttle truck to take the cows from place to place in the fairgrounds.
FJ couldn’t afford a really great shuttle, so the shuttle he rented traverses its route only once (!) and makes N (1 <= N <= 20,000) stops (conveniently numbered 1…N) along its path. A total of K (1 <= K <= 50,000) groups of cows conveniently numbered 1…K wish to use the shuttle, each of the M_i (1 <= M_i <= N) cows in group i wanting to ride from one stop S_i (1 <= S_i < E_i) to another stop E_i (S_i < E_i <= N) farther along the route.
The shuttle might not be able to pick up an entire group of cows (since it has limited capacity) but can pick up partial groups as appropriate.
Given the capacity C (1 <= C <= 100) of the shuttle truck and the descriptions of the groups of cows that want to visit various sites at the fair, determine the maximum number of cows that can ride the shuttle during the fair.
逛逛集市,兑兑奖品,看看节目对农夫约翰来说不算什么,可是他的奶牛们非常缺乏锻炼——如果要逛完一整天的集市,他们一定会筋疲力尽的。所以为了让奶牛们也能愉快地逛集市,约翰准备让奶牛们在集市上以车代步。但是,约翰木有钱,他租来的班车只能在集市上沿直线跑一次,而且只能停靠N(1 ≤N≤20000)个地点(所有地点都以1到N之间的一个数字来表示)。现在奶牛们分成K(1≤K≤50000)个小组,第i 组有Mi(1 ≤Mi≤N)头奶牛,他们希望从Si跑到Ti(1 ≤Si<Ti≤N)。
由于班车容量有限,可能载不下所有想乘车的奶牛们,此时也允许小里的一部分奶牛分开乘坐班车。约翰经过调查得知班车的容量是C(1≤C≤100),请你帮助约翰计划一个尽可能满足更多奶牛愿望的方案。
输入格式
【输入】
第一行:包括三个整数:K,N和C,彼此用空格隔开。
第二行到K+1行:在第i+1行,将会告诉你第i组奶牛的信息:Si,Ei和Mi,彼
此用空格隔开。
输出格式
【输出】
第一行:可以坐班车的奶牛的最大头数。
首先要想到以右边界把区间进行排序,右边界小的排在前面;
然后贪心的根据当前区间 [l,r] 的最大人数上人,只要保证总人数不超过限制,然后区间 [l,r-1] 加上上的人数,继续维护最大值;
这个区间最大值,区间修改显然线段树是一个很棒的方式;
代码:
#include<bits/stdc++.h>
#define LL long long
#define pa pair<int,LL>
#define ls k<<1
#define rs k<<1|1
#define inf 0x3f3f3f3f
using namespace std;
const int N=50100;
const int M=400100;
const int mod=1e9+7;
int k,n,c;
struct Nod{
int s,e,m;
}d[N];
bool cmp(Nod p,Nod q){
if(p.e==q.e) return p.s<q.s;
return p.e<q.e;
}
struct Node{
int l,r,mx,lt;
}tr[N*4];
void build(int l,int r,int k){
tr[k].l=l,tr[k].r=r;
if(l==r) return;
int d=(l+r)>>1;
build(l,d,ls);
build(d+1,r,rs);
}
void pd(int k){
if(tr[k].lt){
tr[ls].lt+=tr[k].lt,tr[rs].lt+=tr[k].lt;
tr[ls].mx+=tr[k].lt,tr[rs].mx+=tr[k].lt;
}
tr[k].lt=0;
}
int query(int l,int r,int k){
if(tr[k].l>=l&&tr[k].r<=r) return tr[k].mx;
pd(k);
int d=(tr[k].l+tr[k].r)>>1;
int mmax=0;
if(l<=d) mmax=max(mmax,query(l,r,ls));
if(r>d) mmax=max(mmax,query(l,r,rs));
return mmax;
}
void add(int l,int r,int w,int k){
if(tr[k].l>=l&&tr[k].r<=r){
tr[k].mx+=w;
tr[k].lt+=w;
return;
}
pd(k);
int d=(tr[k].l+tr[k].r)>>1;
if(l<=d) add(l,r,w,ls);
if(r>d) add(l,r,w,rs);
tr[k].mx=max(tr[ls].mx,tr[rs].mx);
}
int main(){
scanf("%d%d%d",&k,&n,&c);
build(1,n,1);
for(int i=1;i<=k;i++) scanf("%d%d%d",&d[i].s,&d[i].e,&d[i].m);
sort(d+1,d+k+1,cmp);
int ans=0;
for(int i=1;i<=k;i++){
int mmax=query(d[i].s,d[i].e,1);
if(mmax+d[i].m>c) add(d[i].s,d[i].e-1,c-mmax,1),ans+=c-mmax;
else add(d[i].s,d[i].e-1,d[i].m,1),ans+=d[i].m;
}
printf("%d\n",ans);
return 0;
}