【SSL1088 &洛谷P 1204】[USACO]挤牛奶【离散化】

USACO 1.2 挤牛奶

Time Limit:1000MS Memory Limit:65536K

Description

三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶。第一个农民在300时刻(从5点开始计时,秒为单位)给他的牛挤奶,一直到1000时刻。第二个农民在700时刻开始,在 1200时刻结束。第三个农民在1500时刻开始2100时刻结束。期间最长的至少有一个农民在挤奶的连续时间为900秒(从300时刻到1200时刻),而最长的无人挤奶的连续时间(从挤奶开始一直到挤奶结束)为300秒(从1200时刻到1500时刻)。
  你的任务是编一个程序,读入一个有N个农民(1 <= N <= 5000)挤N头牛的工作时间列表,计算以下两点(均以秒为单位):
• 最长至少有一人在挤奶的时间段。
• 最长的无人挤奶的时间段。

Input

Line 1: 一个整数N。
Lines 2…N+1: 每行两个小于1000000的非负整数,表示一个农民的开始时刻与结束时刻。

Output

一行,两个整数,即题目所要求的两个答案。

Sample Input

3
300 1000
700 1200
1500 2100

Sample Output

900 300

分析:

离散化 再取最大值 bool标记
再在未标记的区域离散化最大值
即是我们要求的两个结果

CODE:

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct peo{
	int x,y;
}a[5001];
int n,jew[10001],ans,s,s2,ans2;
int main(){
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		scanf("%d%d",&a[i].x,&a[i].y);
		jew[i*2-1]=a[i].x;
		jew[i*2]=a[i].y;  //初始化
	}
	sort(jew+1,jew+2*n+1);
	for(int i=2;i<=2*n;i++)
	{
		bool really=0;
		for(int j=1;j<=n;j++)
		{
			if(jew[i]>a[j].x&&jew[i]<=a[j].y)
			{
				s+=jew[i]-jew[i-1];  //离散化
				ans=max(ans,s);   //取最大值
				s2=0;
				really=true;   //标记
				break;
			}
		}
		if(!really){  //未标记
			s=0;
			s2+=jew[i]-jew[i-1];  //离散化
			ans2=max(ans2,s2);   //取最大值
		}
	}
	cout<<ans<<" "<<ans2;
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/dgssl_xhy/article/details/107503719
今日推荐