USACO--1.2 Milking Cows

                                     USACO--1.2 Milking Cows

描述

三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶。第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒。第二个农民在700秒开始,在 1200秒结束。第三个农民在1500秒开始2100秒结束。期间最长的至少有一个农民在挤奶的连续时间为900秒(从300秒到1200秒),而最长的无人挤奶的连续时间(从挤奶开始一直到挤奶结束)为300秒(从1200秒到1500秒)。

你的任务是编一个程序,读入一个有N个农民(1 <= N <= 5000)挤N头牛的工作时间列表,计算以下两点(均以秒为单位):

  • 最长至少有一人在挤奶的时间段。
  • 最长的无人挤奶的时间段。(从有人挤奶开始算起)

格式

PROGRAM NAME: milk2

INPUT FORMAT:

(file milk2.in)

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

OUTPUT FORMAT:

(file milk2.out)

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

 SAMPLE INPUT

3
300 1000
700 1200
1500 2100

SAMPLE OUTPUT

900 300



这一题可以算是一题模拟题,比较简单,不过我Wa了两次。
Wa1:没有考虑n=1的情况,wa了第一次。
Wa2:没有考虑排序后第一个数据的区间可以包含其他数据,Wa了一次,幸亏是最后一次~~~~
代码比较繁杂,不建议看~~~~(>_<)~~~~ 
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
using namespace std;
const long MAX=1000005;
int n;
long ans1[MAX];
long ans2[MAX];
struct q
{
	long L;
	long R;
}f[MAX];
void debug(int tot, int tott)
{
	for (int i=1; i<=tot; i++)
	cout<<ans1[i]<<" ";
	cout<<endl;
	for (int i=1; i<=tott; i++)
	cout<<ans2[i]<<" ";
	cout<<endl;
}
void solve()
{
	bool flag=true;
	int x=f[1].L,y=f[1].R;
	long maxx=0;
	int tot=0;
	int tott=0;
	if (n==1) cout<<y-x<<" "<<0<<endl;//第一次wa的地方
	else 
	{
	for (int i=2; i<=n; i++)
	{
	   if (f[i].L<=y && y<=f[i].R) y=f[i].R;
	   else if(f[i].L<=y && y>f[i].R) {
	   ans1[++tot]=y-x;
}//第二次wa的地方!!
	   else {
	   	ans1[++tot]=y-x;
		ans2[++tott]=f[i].L-y;
	   	x=f[i].L;
	   	y=f[i].R;
	   	flag=false;
	   }
	   //debug(tot,tott);	
	}
	for (int i=1; i<=tot; i++)
	{
		if (ans1[i]>maxx) maxx=ans1[i];
	}
     cout<<maxx<<" ";
     maxx=0;
	for (int i=1; i<=tott; i++)
	{
		if (ans2[i]>maxx) maxx=ans2[i];
	}
	cout<<maxx<<endl;
}
}
bool cmp(q x, q y)
{
	return x.L<y.L;
}

int main()
{
	scanf("%d",&n);
	for (int i=1; i<=n; i++)
	scanf("%ld %ld",&f[i].L, &f[i].R);
	sort(f+1,f+n+1,cmp);
	solve();
	return 0;
}
据说是c++最快的程序~~

#include <algorithm>
#include <cstdio>
using namespace std;
 
struct node
{
	int bgn, end;
	bool operator<(const node &rhs) const
	{
		return bgn < rhs.bgn;
	}
}a[5000];
 
int main()
{
	
	int bgn, end, n, res1 = 0, res2 = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
		scanf("%d%d", &a[i].bgn, &a[i].end);
	sort(a, a+n);
	int left = a[0].bgn, right = a[0].end;
	for (int i = 1; ; ++i)
	{
		for (; i < n && a[i].bgn <= right; ++i)
			if (a[i].end > right)
				right = a[i].end;
		if (right-left > res1) res1 = right-left;
		if (i == n) break;
		if (a[i].bgn-right > res2) res2 = a[i].bgn-right;
		left = a[i].bgn; right = a[i].end;
	}
	printf("%d %d\n", res1, res2);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值