USACO1.2.1 Milking Cows (milk2)
2015-01-26 19:16
399 查看
按照开始时间升序排序,然后从左到右扫一遍
所谓从左到右扫一遍,就是记录一个当前区间,[a_begin ,a_end]
如果下一组数据的begin比a_end的小(或相等),则是连接起来的,检查这组数据的end,取max{end , tmp_end}。
如果下一组数据的begin比a_end的大,则是相互断开的,整理本区间,ans1取max{a_end - a_begin ,ans1}。ans2取max{begin - a_end ,ans2}
所谓从左到右扫一遍,就是记录一个当前区间,[a_begin ,a_end]
如果下一组数据的begin比a_end的小(或相等),则是连接起来的,检查这组数据的end,取max{end , tmp_end}。
如果下一组数据的begin比a_end的大,则是相互断开的,整理本区间,ans1取max{a_end - a_begin ,ans1}。ans2取max{begin - a_end ,ans2}
/* ID: xsy97051 LANG: C++ TASK: milk2 */ #include <algorithm> #include <cstdio> #include <iostream> using namespace std; struct node { int bgn, end; bool operator<(const node &rhs) const { return bgn<rhs.bgn; } }a[5000]; int main() { freopen("milk2.in", "r", stdin); freopen("milk2.out", "w", stdout); int bgn,end,n,ans1=0,ans2=0; cin>>n; for(int i=0;i<n;++i) cin>>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>ans1) ans1=right-left; if(i==n) break; if(a[i].bgn-right>ans2) ans2=a[i].bgn-right; left=a[i].bgn; right=a[i].end; } cout<<ans1<<" "<<ans2<<endl; }
相关文章推荐
- USACO1.2.1 Milking Cows (milk2)
- USACO 1.2.1 Milking Cows(milk2)
- usaco 1.2.1 Milking Cows
- USACO 1.2.1 Milking Cows
- Usaco 1.2.1 挤牛奶(Milking Cows)
- USACO 1.2.1 Milking Cows
- [USACO 1.2.1] Milking Cows
- usaco 1.2.1 Milking Cows 题解
- 【区间计数】Milking Cows 挤牛奶 (milk2) Usaco_Training 1.2
- usaco 1.2.1 Milking Cows 挤牛奶
- USACO 1.2.1 Milking Cows
- USACO之Section1.2.1 Milking Cows [其他]
- USACO1.2.1 Milking Cows(挤牛奶)
- USACO Section 1.2.1 Milking Cows
- USACO1.2.1 Milking Cows 给奶牛挤奶
- USACO 1.2.1 Milking Cows
- 【区间计数】Milking Cows 挤牛奶 (milk2) Usaco_Training 1.2
- USACO1.2.1--Milking Cows
- USACO 1.2.1 - MIlking Cows(模拟)
- usaco1.2.1 Milking Cows