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.

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

   题意:

   给你N个区间,N个区间可重叠,全部重叠后,找出重叠后的最长区间距离。相互不重叠的区间则找出相隔最长区间的距离。输出两个距离长度,第一个为最长区间长度,第二个为相隔最长区间长度。

   思路:

   用数组来表示,将起点位置+1,终点为位置-1,将后一项+=前一项,则会获得一组0,1数表示的数组区间。最长区间长度则是数组中从1到0最长的距离,最长相隔区间长度为从0到1最长的距离,比较即可得出。

   

   First and wrong:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
	int N,from,to,i,max=0,min;
	int time[100005];
	memset(time,0,sizeof(time));
	scanf("%d",&N);
	while(N--)
	{
		scanf("%d%d",&from,&to);
		time[from-1]++;
		time[to]--;
		if(to>max) max=to;
	}
	to=max; //忘记重新赋值,导致后面死循环了
	
//	printf("\n%d\n",to); //测试上一步错误
//	system("pause");
	
	for(i=2;i<=to;i++)   //is max not to
	  time[i]+=time[i-1];
	 
//  for(i=1;i<=to;i++)    //输出看看0,1状态
//	{
//	 printf("%d ",time[i]);
//     if(!(i%10)) printf("\n");
//	}
//    system("pause");
    
	i=1;
    while(!time[i]) i++;
	from=i;
	min=0;
	max=0;   //忘记初始化
	
//printf("to=%d\nfrom=%d\nmin=%d\n",to,from,min);
//system("pause");

	while(i<=to)  //not i!=to  to and max
	{
	   if(i>to)  break;
	   while(time[i]&&i<=to) i++;
	   
	   printf("\ni=%d\n",i);
	   system("pause");
	  
	   if(max<i-from) max=i-from;  
	   
//	   printf("max=%d\n",max);  //死循环了,一开始的时候
//	   system("pause");
	   
	   from=i;
	   
	   if(i>to) break;
	   while(!time[i]&&i<=to) i++;
	   
//	   printf("i=%d\n",i);    //死循环了,一开始的时候
//	   system("pause");
	   
	   if(min<i-from) min=i-from-1; 
	   
//	   printf("min=%d\n",min);   //死循环了,一开始的时候
//	   system("pause");
	   
	   from=i;
	}
	printf("%d %d\n",max,min);
	return 0;
}

   AC:

#include<stdio.h>
#include<string.h>
int time[1000005];
int main()
{
	int N,from,to,max=0,i,min;
	memset(time,0,sizeof(time));
	scanf("%d",&N);
	if(N==1)
	{
		scanf("%d%d",&from,&to);
		printf("%d %d\n",to-from,0);
	}
	else
	{
	while(N--)
	{
	  scanf("%d%d",&from,&to);
	  if(max<to) max=to;
	  time[from+1]++;   
                     //这里必须+1,为什么?
	  time[to+1]--;     //这里也必须+1
	}
	to=max;
	for(i=2;i<=to;i++)
	  time[i]+=time[i-1];
	
//	for(i=1;i<=to;i++)
//	{
//		printf("%d ",time[i]);
//		if(!(i%10)) printf("\n");
//	}
	
	i=1;
	while(!time[i]) i++;   //移动到第一个不是0的数上 
        from=i;     //标记从这里开始
	max=0;
	min=0;      //两个都要初始化 
	while(i<=to)
	{
		if(i>to) break;            //每次循环前都判断一次是否已经大于最大数
		while(time[i]&&i<=to) i++; //移动到第一个不是1的数上,即为第一个0
		if(max<i-from) max=i-from;
		if(i>to) break;            //每次循环前都判断一次是否已经大于最大数
		from=i;
		while(!time[i]&&i<=to) i++; //移动到第一个不是0的数上,即为第一个非0数
		if(min<i-from) min=i-from;
		from=i; 
	} 
    printf("%d %d\n",max,min);
    }
	return 0;
}

   Test:

Here are the respective outputs:
        ----- our output ---------
        19_0
        ---- your output ---------
        19_1
        --------------------------
        ------ Data for Run 3 [length=54 bytes] ------
        10 
        2 3 
        4 5 
        6 7 
        8 9 
        10 11 
        12 13 
        14 15 
        16 17 
        18 19 
        1 20 
        ----------------------------

Here are the respective outputs:
        ----- our output ---------
        100_1
        ---- your output ---------
        404_0
        --------------------------

        ------ Data for Run 2 [length=34 bytes] ------
        4 
        100 200 
        201 301 
        302 402 
        403 503 
        ----------------------------
//若上面数字前不加1的话,该测试会出现错,为什么?

   总结:

   我决定AC完后一天早上才写总结,马上写的话当时可能记住了,过一天写可能记得比较牢固点。数组太大应该开在函数外,一开始一直以为是哪里错了。这道题跟之前校赛有道题很类似,所以很快就想到了是怎么做的,但是却又把时间浪费在Debug上,静下心来然后细心寻找这很重要,不过比之前好多了。进步得比较慢,不过起码还是在进步,继续努力。

   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值