题目描述
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 \leq N\leq 20000)N(1≤N≤20000) 个地点(所有地点都以 11 到 NN 之间的一个数字来表示)。现在奶牛们分成 K(1\leq K\leq 50000)K(1≤K≤50000) 个小组,第i 组有 M_i(1\leq M_i\leq N)M i(1≤M i≤N) 头奶牛,他们希望从 S_iS i 跑到 E_i(1\leq S_i< E_i\leq N)E i (1≤S i <E i ≤N)。
由于班车容量有限,可能载不下所有想乘车的奶牛们,此时也允许小里的一部分奶牛分开乘坐班车。约翰经过调查得知班车的容量是 C(1\leq C\leq 100)C(1≤C≤100),请你帮助约翰计划一个尽可能满足更多奶牛愿望的方案。
输入格式
第一行:包括三个整数:K,NK,N 和 CC,彼此用空格隔开。
第二行到 K+1K+1 行:在第 i+1i+1 行,将会告诉你第 ii 组奶牛的信息:S_i,E_iS
i,Ei 和 M_iM i ,彼此用空格隔开。
输出格式
第一行:可以坐班车的奶牛的最大头数。
输入输出样例
输入 #1复制
8 15 3
1 5 2
13 14 1
5 8 3
8 14 2
14 15 1
9 12 1
12 15 2
4 6 1
输出 #1复制
10
说明/提示
班车可以把 22 头奶牛从 11 送到 55,33 头奶牛从 55 送到 88,22 头奶牛从 88 送到 1414,11 头奶牛从 99 送到 1212,11 头奶牛从 1313 送到 1414,11 头奶牛从 1414 送到 1515。
上代码:
#include<cstdio>
#include<algorithm>
#include<cctype>
#define maxn 50007
using namespace std;
int ans,n,m,k,w[maxn];
inline int qread() {
char c=getchar();int num=0,f=1;
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) num=num*10+c-'0';
return num*f;
}
struct node {
int u,v,c;
}e[maxn];
const int inf=0x7fffffff;
inline bool cmp(node a, node b) {
if(a.v!=b.v) return a.v<b.v;
return a.u<b.u;
}
int main() {
k=qread(),n=qread(),m=qread();
for(int i=1;i<=k;++i) e[i].u=qread(),e[i].v=qread(),e[i].c=qread();
sort(e+1,e+1+k,cmp);
for(int i=1;i<=k;++i) {
if(w[e[i].u]>=m) continue;
int minn=inf;
for(int j=e[i].u;j<=e[i].v;++j) {
minn=min(m-w[j],minn);
if(minn<=0) break;
}
if(minn>0) {
if(minn>=e[i].c) {
for(int j=e[i].u;j<e[i].v;++j)
w[j]+=e[i].c;
ans+=e[i].c;
}
else {
for(int j=e[i].u;j<e[i].v;++j)
w[j]+=minn;
ans+=minn;
}
}
}
printf("%d\n",ans);
return 0;
}