USACO Section 1.2.1 Milking Cows
2013-07-23 09:34
363 查看
首先还是排序,比较重要先按开始时间排,如果相同,再按结束时间排序
用两个变量分别记录最长的连续时间的起始时间,满足条件就更新这两个值
用两个变量分别记录最长的连续时间的起始时间,满足条件就更新这两个值
/* ID: zhuihun1 PROG: milk2 LANG: C++ */ #include <iostream> #include <fstream> #include <string.h> #include <stdio.h> #include<algorithm> using namespace std; struct Farmer { int s_time; int e_time; }; bool comp(Farmer a,Farmer b) { return a.s_time<b.s_time||(a.s_time==b.s_time&&a.e_time<b.e_time); } Farmer a[5005];int n; int max(int g,int h) { return g>h?g:h; } int main() { ofstream fout ("milk2.out"); ifstream fin ("milk2.in"); int i; int max_t=0,min_t=0; fin>>n; for(i=0;i<n;i++) fin>>a[i].s_time>>a[i].e_time; sort(a,a+n,comp); int nBegin=a[0].s_time, nEnd=a[0].e_time; max_t=a[0].e_time-a[0].s_time; for(i=0;i<n;i++) { if(a[i].s_time<=nEnd && a[i].e_time>=nEnd){ nEnd = a[i].e_time; } else if(a[i].s_time>nEnd) { max_t = max(nEnd-nBegin, max_t); min_t = max(a[i].s_time-nEnd, min_t); nBegin = a[i].s_time; nEnd = a[i].e_time; } } fout<<max_t<<" "<<min_t<<endl; return 0; }
相关文章推荐
- USACO之Section1.2.1 Milking Cows [其他]
- USACO1.2.1 Milking Cows(挤牛奶)
- USACO 1.2.1 Milking Cows
- USACO-Section1.3 Milking Cows (区间问题)
- USACO1.2.1--Milking Cows
- USACO Section 1.2 Milking Cows
- USACO1.2.1 Milking Cows (milk2)
- USACO-Section 1.2 Milking Cows【贪心算法】
- USACO-Section1.2 Milking Cows [线段树]
- USACO1.2.1 Milking Cows (milk2)
- USACO 1.2.1 Milking Cows
- [USACO 1.2.1] Milking Cows
- usaco 1.2.1 Milking Cows 题解
- usaco1.2.1 Milking Cows
- USACO - Chapter1 Section 1.2 - Milking Cows
- Usaco 1.2.1 挤牛奶(Milking Cows)
- usaco 1.2.1 Milking Cows 挤牛奶
- USACO-Section1.2 Milking Cows
- USACO Section 1.2 Milking Cows 解题报告
- usaco 1.2.1 Milking Cows