您的位置:首页 > 其它

USACO 1.2.1 - MIlking Cows(模拟)

2016-09-05 21:10 232 查看
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.

PROGRAM NAME: milk2

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.

SAMPLE OUTPUT (file milk2.out)

900 300

题意:

给出一些工作的时间(有重叠),求出最大的连续工作时长和最大的连续休息时长.

解题思路:

开数组标记工作和非工作,注意的是,工作是从开始时间的下一个单位时间真正开始,所以最后遍历的时候需要注意.

AC代码:

/*
ID:Reckful
LANG:C++
TASK:milk2
*/
#include<stdio.h>
#include<algorithm>
using namespace std;
bool qdu[1000001];
int main()
{
//freopen("milk2.in","r",stdin);
//freopen("milk2.out","w",stdout);
int n;
int res1 = 0;
scanf("%d",&n);
int max_b = 0;
int min_a = 1000001;
while(n--)
{
int a;
int b;
scanf("%d%d",&a,&b);
min_a = min(min_a,a);
max_b = max(max_b,b);
for(int i = a+1;i <= b;i++) qdu[i] = 1;
}
int res2 = 0;
int tmp1 = 0;
int tmp2 = 0;
for(int i = min_a;i <= max_b+1;i++)
{
if(qdu[i])  tmp1++;
else
{
res1 = max(res1,tmp1);
tmp1 = 0;
}
}
for(int i = min_a+1;i <= max_b;i++)
{
if(!qdu[i]) tmp2++;
else
{
res2 = max(res2,tmp2);
tmp2 = 0;
}
}
printf("%d %d\n",res1,res2);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: