Through a 1423: [example 2] trees explanations

YBT 1423 solution to a problem trees

The original title

时间限制: 1000 ms         内存限制: 65536 KB
提交数: 2297     通过数: 853 

Description [title]

现在我们国家开展新农村建设,农村的住房建设纳入了统一规划,统一建设,政府要求每一住户门口种些树。门
口路边的地区被分割成块,并被编号成1..N。每个部分为一个单位尺寸大小并最多可种一棵树。每个居民房子门
前被指定了三个号码B,E,T。这三个数表示该居民想在B和E之间最少种T棵树。当然,B≤E,居民必须记住指定
区不能种多于区域地块数的树,所以T≤E-B+l。居民们想种树的各自区域可以交叉。你的任务是求出能满足所有
要求的最少的树的数量,尽量较少政府的支出。

[Enter]

第一行包含数据N,M,区域的个数(0<N≤30000),房子的数目(0<m≤5000);

下面的m行描述居民们的需要:B E T,0<B≤E≤30000,T≤E-B+1。

[Output]

输出一个数,为满足所有居民的要求,所需要种树的最少数量。

[Sample input]

9 4
3 5 2
1 4 2
4 6 2
8 9 2

[Sample Output]

5

This question is just to see, really want to curse loudly: cancer problem ah!
Beginning with a For loop also delusional, who knows, triple loop not afford to hurt ah!
A direct jump: Time Limit 60%
suddenly tears ~ ~ ~ ~
later reference books a bit, but also used a Struct structure was out of: 100% correct answers
first definition of the structure:

struct Tree{
	int s;
	int e;
	int v;
}; 
Tree a[5005],mid;

After re-engage the input and preliminary processing section:

void Init(){
	cin>>n>>m;
	for(int i=1;i<=m;i++)
		cin>>a[i].s>>a[i].e>>a[i].v;
	sort(a+1,a+m+1,CMP);
}

Finally, to get the processing section and an output:

void Solve(){
	int i,j,k,ans=0;
	for(i=1;i<=m;i++){
		k=0;
		for(j=a[i].s;j<=a[i].e;j++)
			if(Use[j]) k++;
		if(k>=a[i].v) continue;
		for(j=a[i].e;j>=a[i].s;j--){
			if(!Use[j]){
				Use[j]=1;
				k++;
				ans++;
				if(k==a[i].v) break;
			}
		}
	}
	cout<<ans; 
}

The complete code is as follows:

#include<iostream>
#include<algorithm>
using namespace std;
struct Tree{
	int s;
	int e;
	int v;
}; 
Tree a[5005],mid;
int n,m;
bool Use[30005]={0};
bool CMP(const Tree &x,const Tree &y){
	return x.e<y.e;
}
void Init(){
	cin>>n>>m;
	for(int i=1;i<=m;i++)
		cin>>a[i].s>>a[i].e>>a[i].v;
	sort(a+1,a+m+1,CMP);
}
void Solve(){
	int i,j,k,ans=0;
	for(i=1;i<=m;i++){
		k=0;
		for(j=a[i].s;j<=a[i].e;j++)
			if(Use[j]) k++;
		if(k>=a[i].v) continue;
		for(j=a[i].e;j>=a[i].s;j--){
			if(!Use[j]){
				Use[j]=1;
				k++;
				ans++;
				if(k==a[i].v) break;
			}
		}
	}
	cout<<ans; 
} 
int main(){
	Init();
	Solve();
	return 0;
}

The original site: 1423: example [2] trees

Published 14 original articles · won praise 8 · views 1164

Guess you like

Origin blog.csdn.net/Horse_Lake/article/details/103864855