题目连接:
http://ace.delos.com/usacoprob2?a=kfu9VlwKrpV&S=milk2
题目:
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 |
Lines 2..N+1: | Two non-negative integers less than 1000000, 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
题目翻译:
描述
三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶。第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒。第二个农民在700秒开始,在 1200秒结束。第三个农民在1500秒开始2100秒结束。期间最长的至少有一个农民在挤奶的连续时间为900秒(从300秒到1200秒),而最长的无人挤奶的连续时间(从挤奶开始一直到挤奶结束)为300秒(从1200秒到1500秒)。
你的任务是编一个程序,读入一个有N个农民(1 <= N <= 5000)挤N头牛的工作时间列表,计算以下两点(均以秒为单位):
- 最长至少有一人在挤奶的时间段。
- 最长的无人挤奶的时间段。(从有人挤奶开始算起)
[编辑] 格式
文件名: milk2
输入:
(file milk2.in)
Line 1:
一个整数N。
Lines 2..N+1:
每行两个小于1000000的非负整数,表示一个农民的开始时刻与结束时刻。
输出:
(file milk2.out)
一行,两个整数,即题目所要求的两个答案。
[编辑] SAMPLE INPUT
3 300 1000 700 1200 1500 2100
[编辑] SAMPLE OUTPUT
900 300
/*
ID:smilecl1
LANG:C
TASK:milk2
*/
/*
* TIME: 2012/11/1
* MODIFY: 2012/11/9
* EMAIL: SmileCloud201@gmail.com
* NAME: LiYun
*/
/*
* 思路:
* 先要对每一组数据按照peasant[i].begin的大小从小到大的顺序来排序。这里用了快排序
* 再按照情况进行分类。1、两段时间没有交叉,ans2的值要进行修改。
* 2、两段时间有交叉,ans2的值保持不变,此时其实又可以分两种情况,一种是一个时间段完全包含另一个时间段;另一种
* 是仅仅交叉。这时要修改temp.end的值
*/
#include <stdio.h>
#include <stdlib.h>
#define max_number 5001
typedef struct peasant Peasant;
struct peasant
{
int begin;
int end;
};
int max (int i, int j)
{
int n=i;
int m=j;
return m >= n ? m : n;
}
int campare(const void *va, const void *vb)
{
Peasant *a, *b;
a = (Peasant*)va;
b = (Peasant*)vb;
if(a->begin > b->begin)
return 1;
if(a->begin < b->begin)
return -1;
return 0;
}
int main()
{
FILE *fin;
FILE *fout;
Peasant peasant[max_number];
Peasant temp;
int number = 0;
int i = 0;
int ans1 = 0;
int ans2 = 0;
fin = fopen("milk2.in", "r");
fout = fopen("milk2.out", "w");
fscanf(fin, "%d", &number);
for (i = 0; i != number; ++i)
fscanf(fin, "%d%d", &peasant[i].begin, &peasant[i].end);
/* 快排序 */
qsort(peasant, number, sizeof(Peasant), campare);
temp.begin = peasant[0].begin;
temp.end = peasant[0].end;
ans1 = peasant[0].end - peasant[0].begin;
for (i = 1; i != number; ++i)
{
/* 两段时间没有交叉,ans2的值要进行修改,注意不能取“=” */
if (peasant[i].begin > temp.end)
{
ans1 = max( ans1, peasant[i].end - peasant[i].begin );
ans2 = max(ans2, peasant[i].begin - temp.end);
temp.begin = peasant[i].begin;
temp.end = peasant[i].end;
}
else
{
/* 两段时间有交叉 */
if (peasant[i].end >= temp.end)
{
temp.end = peasant[i].end;
}
}
ans1 = max (ans1 , temp.end - temp.begin);
}
fprintf(fout, "%d %d\n",ans1, ans2);
return 0;
}