Milking Cows

USACO 上的一道题

原题目:

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

 思路 :大意是要找最大的有奶牛挤奶的区间和没有奶牛要挤奶的时间区间;可以把所有区间按照起始时间排序,然后遍历,不断判断区间的包含关系,同时记录下最大的挤奶区间best_milked和不挤奶的区间大小best_n0_milked。区间之间的关系有以下三种情况:

|___________________|  (start,end)

        |____________|     (start1,end1)

        |__________________|   (start2,end2)

                                                 |________________|   (start3,end3)

第一种 :考察是否更新更大的best_milked = end -start ; start 、end 不用更新,继续向后考察下一个区间;

第二种: (start2<=end && end2>=end) 考察是否更新更大的best_milked = end2 - start; start 不用更新,end 更新为end2,继续向下考察下一个区间;

第三种: (start3>end)  更新best_milked为end-start和end3-start3和原来的值的最大值,同时可以更新best_no_milked为原来的值和start3-end之间的最大值,start更新为start3,end更新为end3,继续考察下一个区间;

源代码如下:

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int start;
    int end;
};
int cmp(const void *a ,const  void *b)//注意返回值当作是bool值,必须保证交换a,b后真假发生变化
{
    if(((struct node *)a)->start -  ((struct node *)b)->start <0 ) return -1;
    else if(((struct node *)a)->start -  ((struct node *)b)->start > 0 ) return 1;
    else return 0;
}
int main () 
{
    FILE *fin  = fopen ("milk2.in", "r");
    FILE *fout = fopen ("milk2.out", "w");
    int n;
    fscanf (fin, "%d", &n);    
    struct node *info;
    info=new struct node[n];
    int i=0;
    for(i=0;i<n;i++)
    {
        fscanf(fin,"%d%d",&info[i].start,&info[i].end);
    }

    qsort(info,n,sizeof(struct node),cmp);
    int start,end;
    start=info[0].start;
    end=info[0].end;
    int best_milked=end-start;
    int best_no_milked=0;
    int temp;
    for(i=1;i<n;i++)
    {
        if(info[i].end <=end)
        {
            temp= end - start;
            if(temp>best_milked) best_milked=temp;
        }
        else if(info[i].start<=end && info[i].end >=end)
        {
            temp= info[i].end - start;
            if(temp>best_milked) best_milked=temp;
            end = info[i].end;
        }
        else if(info[i].start>end)
        {
            if((info[i].end -info[i].start) > (end -start) )temp =info[i].end -info[i].start;
            else temp = end -start;
            if(temp>best_milked) best_milked=temp;            
            temp = info[i].start -end;
            if(temp>best_no_milked) best_no_milked =temp;
            start=info[i].start;
            end = info[i].end;
        }
    }
    fprintf (fout, "%d %d\n",best_milked, best_no_milked);
    return 0;
}

 

转载于:https://www.cnblogs.com/easyFancy/p/3330266.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Based on the following story, continue the story by writing two paragraphs, paragraph 1 beginning with "A few weeks later, I went to the farm again. " and paragraph 2 beginning with "I was just about to leave when the hummingbird appeared."respectively with 150 words. I was invited to a cookout on an old friend's farm in western Washington. I parked my car outside the farm and walked past a milking house which had apparently not been used in many years.A noise at a window caught my attention,so I entered it. It was a hummingbird,desperately trying to escape. She was covered in spider-webs and was barely able to move her wings. She ceased her struggle the instant I picked her up. With the bird in my cupped hand, I looked around to see how she had gotten in. The broken window glass was the likely answer. I stuffed a piece of cloth into the hole and took her outside,closing the door securely behind me. When I opened my hand, the bird did not fly away; she sat looking at me with her bright eyes.I removed the sticky spider-webs that covered her head and wings. Still, she made no attempt to fly.Perhaps she had been struggling against the window too long and was too tired? Or too thirsty? As I carried her up the blackberry-lined path toward my car where I kept a water bottle, she began to move. I stopped, and she soon took wing but did not immediately fly away. Hovering,she approached within six inches of my face. For a very long moment,this tiny creature looked into my eyes, turning her head from side to side. Then she flew quickly out of sight. During the cookout, I told my hosts about the hummingbird incident. They promised to fix the window. As I was departing, my friends walked me to my car. I was standing by the car when a hummingbird flew to the center of our group and began hovering. She turned from person to person until she came to me. She again looked directly into my eyes, then let out a squeaking call and was gone. For a moment, all were speechless. Then someone said, “She must have come to say good-bye.”
02-12

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值