USACO 1.2.2 Milking Cows

题目链接:http://cerberus.delos.com:790/usacoprob2?a=usTFRRcEHRQ&S=milk2

翻译的话nocow上面就有,我就不翻译了。

 

我的思路是先按照每个农民的开始工作的时间排序,然后判断哪段时间最长。其实这道题目并不难,但是错了好久。总是有一些地方没有想到,思维不够紧密。下面直接上代码。

/*
ID: sunexio2
LANG: C++
PROG: milk2

*/
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <algorithm>
using namespace std;

ofstream fout("milk2.out");
ifstream fin ("milk2.in");

struct tt{
	int st, et;
}a[5500];


bool cmp(tt x, tt y)
{
	if(x.st != y.st)
		return x.st < y.st;
	return x.st < y.st;
}

int main(){
	int n;
	while(fin >> n)
	{
		for(int i = 0; i < n; ++i)
			fin >> a[i].st >> a[i].et ;
		sort(a, a + n, cmp);
		int ans1 = 0,  ans2 = 0, ttmp, cmc = a[0].et;
		ttmp = ans1 = a[0].et - a[0].st;
		ans2 = 0;
		for(int i = 1; i < n; ++i)
		{
			cmc = cmc > a[i - 1].et? cmc : a[i - 1].et;
			if(a[i].st <= cmc ){
				if(a[i].et - cmc > 0)
					ttmp += a[i].et - cmc;
			}
			else
					ttmp = a[i].et - a[i].st;
			ans1 = ttmp > ans1? ttmp : ans1;
			if(a[i].st - cmc > ans2)
				ans2 = a[i].st - cmc;
		}
		fout << ans1 << ' ' << ans2 << endl;
	}
	return 0;
}


 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值