USACO Milking Cows (模拟)

Milking Cows

Three farmers rise at 5 am each morning and head for the barn to milk three cows. The first farmer begins milking his cow at time 300 (measured in seconds after 5 am) and ends at time 1000. The second farmer begins at time 700 and ends at time 1200. The third farmer begins at time 1500 and ends at time 2100. The longest continuous time during which at least one farmer was milking a cow was 900 seconds (from 300 to 1200). The longest time no milking was done, between the beginning and the ending of all milking, was 300 seconds (1500 minus 1200).

Your job is to write a program that will examine a list of beginning and ending times for N (1 <= N <= 5000) farmers milking N cows and compute (in seconds):

• The longest time interval at least one cow was milked.
• The longest time interval (after milking starts) during which no cows were being milked.

INPUT FORMAT

 Line 1: The single integer, N Lines 2..N+1: Two non-negative integers less than 1,000,000, respectively the starting and ending time in seconds after 0500

SAMPLE INPUT (file milk2.in)

3
300 1000
700 1200
1500 2100



OUTPUT FORMAT

A single line with two integers that represent the longest continuous time of milking and the longest idle time.

900 300

（其实就是进行了优化的搜索而已）

[编辑]怀疑是线段树的变形？

（当然也要考虑被完全包括的情况），马上修改开头与结尾，并删除（可以标记），最后在剩下的线段中统计就可以了。

（好像非常易懂，也很容易实现）

[编辑]标记数组（哈希）

1e6的范围，开一个布尔数组完全可以，有人为TRUE，无人为FALSE，注意边界即可。最后线性扫描即可。

（这时顺便找出总的起点与终点）；

[编辑]分段动规

f[i]表示第i个农民所在的最长连续线段

last_start表示这个线段的起点。

a[i].begin 第i个农民的起点时间，a[i].end 终点时间

f[i]={max{f[i-1],a[i].end} (a[i].begin<=f[i-1]) //加上第i个农民仍然连续
a[i].end             (a[i].begin>f[i-1])  /*加上第i个农民变不连续
在这里开始以此农民的开始时间的新的一条连续线段，
更新last_start=a[i].begin作为新起点，因为有间隔，
所以更新longest_idle_time=max(longest_idle_time,a[i].begin-f[i-1]);*/


/*
ID : ever g1
PROG : milk2
LANG : C++
*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<cstdio>
#define ll long long
#define mset(a,x) memset(a,x,sizeof(a))

using namespace std;
const double PI=acos(-1);
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=100005;
const int mod=1e9+7;
int dir[4][2]={0,1,1,0,0,-1,-1,0};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}
ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}
struct node{
int l,r;
}p[5005];

int cmp(node a,node b)
{
if(a.l==b.l)
return a.r<b.r;

return a.l<b.l;
}

int main()
{
freopen("milk2.in","r",stdin);
freopen("milk2.out","w",stdout);
int n,i,j,k,minn,maxx,visit[5005];
cin>>n;
for(i=0;i<n;i++)
cin>>p[i].l>>p[i].r;

sort(p,p+n,cmp);
int l=p[0].l,r=p[0].r;
if(n==1)
{
cout<<p[0].r-p[0].l<<' '<<"0"<<endl;

goto q;
}

i=1;minn=maxx=0;
while(i<n)
{
while(i<n&&p[i].l<=r)
{
if(p[i].r>r)
r=p[i].r;

i++;
}
if(r-l>maxx)
maxx=r-l;
if(i==n)
break;
if(p[i].l-r>minn)
minn=p[i].l-r;

l=p[i].l;r=p[i].r;
i++;
}
cout<<maxx<<' '<<minn<<endl;
q:	return 0;
}

【USACO】Milking Cows

2016-08-12 20:19:27

洛谷 P1204 [USACO1.2] 挤牛奶Milking Cows

2016-11-15 09:03:24

USACO----Milking Cows

2018-03-19 16:41:38

USACO题目Milking Cows及代码解析

2012年10月23日 16KB 下载

【USACO题库】1.2.1 Milking Cows挤牛奶（模拟）

2016-03-20 19:00:59

Milking Cows 挤牛奶

2015-04-25 22:03:35

bzoj5280 [Usaco2018 Open]Milking Order（二分答案+拓扑排序）

2018-04-21 15:45:50

codeforces 383 A. Milking cows【贪心 + 前缀和】

2018-03-19 22:05:35

CF383A Milking cows

2014-01-21 03:42:46

USACO Milking Cows(模拟)

2015-07-14 19:43:10