前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >洛谷P1607 [USACO09FEB]庙会班车Fair Shuttle

洛谷P1607 [USACO09FEB]庙会班车Fair Shuttle

作者头像
attack
发布2018-04-12 10:34:54
6110
发布2018-04-12 10:34:54
举报

题目描述

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,彼

此用空格隔开。

输出格式:

【输出】

第一行:可以坐班车的奶牛的最大头数。

输入输出样例

输入样例#1:

代码语言:javascript
复制
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:

代码语言:javascript
复制
10

说明

【样例说明】

班车可以把2头奶牛从1送到5,3头奶牛从5送到8,2头奶牛从8送到14,1头

奶牛从9送到12,1头奶牛从13送到14,1头奶牛从14送到15。

zhw讲的做法是线段树

但是我想问,,

为什么暴力能过的题需要用线段树???????

思路:按照结束点排序,对于每一个线段,查询一下他在覆盖的区间里面的最小值,

   然后根据这条线段的权值进行分类讨论就好

代码语言:javascript
复制
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;
const int MAXN=100001;
inline void read(int &n)
{
	char c=getchar();bool flag=0;n=0;
	while(c<'0'||c>'9')	c=='-'?flag=1,c=getchar():c=getchar();
	while(c>='0'&&c<='9')	n=n*10+c-48,c=getchar();flag==1?n=-n:n=n;
}
struct node
{
	int bg,ed,num;
}cow[MAXN];
int comp(const node &a,const node &b)
{
	if(a.ed!=b.ed)	return a.ed<b.ed;
	else return a.bg<b.bg;
}
int have[MAXN];// 在i这个位置,车上已经装了多少奶牛 
int main()
{
	int k,n,m;
	read(k);read(n);read(m);
	for(int i=1;i<=k;i++)
		read(cow[i].bg),read(cow[i].ed),read(cow[i].num);
	sort(cow+1,cow+k+1,comp);
	int ans=0;
	for(int i=1;i<=k;i++)
	{
		if(have[cow[i].bg]>=m) continue;
		int now=0x7fffff;
		for(int j=cow[i].bg;j<=cow[i].ed;j++)
		{
			now=min(now,m-have[j]);
			if(now==0) break;
		}	
		if(now!=0)
		{
			if(now>=cow[i].num)
			{
				for(int j=cow[i].bg;j<cow[i].ed;j++)	
					have[j]+=cow[i].num;
				ans+=cow[i].num;
			}		
			else 
			{
				for(int j=cow[i].bg;j<cow[i].ed;j++)	
					have[j]+=now;
				ans+=now;
			}
		}
	}
	printf("%d",ans);
	return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-10-19 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目描述
  • 输入输出格式
  • 输入输出样例
  • 说明
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档