USACO 1.2.2 Milking Cows

来源:互联网 发布:山下智久石原里美 知乎 编辑:程序博客网 时间:2024/06/05 14:37

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

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

 

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

/*ID: sunexio2LANG: 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;}elsettmp = 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