洛谷P1607 [USACO09FEB]廟會班車Fair Shuttle

自為風月馬前卒發表於2017-10-19

題目描述

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:
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

說明

【樣例說明】

班車可以把2頭奶牛從1送到5,3頭奶牛從5送到8,2頭奶牛從8送到14,1頭

奶牛從9送到12,1頭奶牛從13送到14,1頭奶牛從14送到15。

 

 

zhw講的做法是線段樹

但是我想問,,

為什麼暴力能過的題需要用線段樹???????

思路:按照結束點排序,對於每一個線段,查詢一下他在覆蓋的區間裡面的最小值,

   然後根據這條線段的權值進行分類討論就好

 

#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;
}

  

 

相關文章