Milking Cows 挤牛奶 USACO 排序 模拟

1005: 1.2.1 Milking Cows 挤牛奶

时间限制: 1 Sec  内存限制: 128 MB
提交: 15  解决: 9
[提交] [状态] [讨论版] [命题人:外部导入]
题目描述

1.2.1 Milking Cows 挤牛奶    

(milk2.pas/c/cpp) 

  三个农民每天清晨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

 

提示

 

来源/分类

 

 

我的天哪 这道题难道有那么难吗

有n(<=5000)个区间

这n个区间在0-100 0000之间

p.s.个人认为就是个简单的排序+模拟。。。

我想的是先把这些区间按照起始时间排一个序(快排比较简单吧nlogn)

So?设一个指针从0-时间的最大值    跑一遍  再+100 0000    那肯定超时啊。。。。

 

 

看来这么整不行  呜呜呜~~~~

 

嗷嗷嗷好像明白了   在1-100 0000之间其实还是有很多点没有扫到

而且既然直接从1-100 0000扫会超时  只要把n个区间的起始点排好序以后 把这n个区间从前往后扫一遍不就OK了吗    +5000肯定不超时啦!!!(暗暗地说:对呀!!天哪我刚才咋么没想到!)

 

那在扫的时候怎么模拟呢?

emmmm。。。

沉思片刻 再看看题吧

 

 

最长至少有一人在挤奶的时间段。

最长的无人挤奶的时间段。(从有人挤奶开始算起

 

 

第一个只要打一个擂台就行了 在从前往后枚举区间的时候看看有没有接起来或者重叠    其实也就是是否相连

再简单一点就是判断当前区间的起点是否小于等于tmpend 然后tmpend每次和当前区间打擂台取一个最大值

 

 

第二个。。天哪咋办。。原地懵圈

停停停再想想O(∩_∩)O哈哈~

如果当前区间的begin比tmpend大

那就相当于不相连了  而且可以保证这之间没有别的区间进来掺和了

为甚么呢》

其实也很简单  tmpend其实存的也就是到当前区间为止最大最靠后的区间的end值

那么最靠后的在tmpend  那tmpend~当前枚举区间的begin之间当然就没有别的区间啦

6666

敲一敲试试吧

 AC了!

我的代码变量名可能很通俗易懂 O(∩_∩)O哈哈~

 

 

还有一个非常重要的问题

以后打擂台的时候假如要寻找最大值

初始的maxn一定要设成0 别设成-1 比如这一个题的最长的无人时间 可能每时每刻都有人啊  那你要是一开始的时候设得-1就输出了-1  其实应该是 0 !

下一次不要再犯错误了(~ ̄▽ ̄)~

#include<bits/stdc++.h>
using namespace std;
struct Node{
    int Begin,End;//结构体大法好 
}milk[5005];
int cmp(Node a,Node b){
    return a.Begin<b.Begin;//其实那个End不用考虑啊 
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d%d",&milk[i].Begin,&milk[i].End);
    sort(milk+1,milk+n+1,cmp);//sort排序一下 再加一个cmp
    int tmpend=milk[1].End   ,   max_at_least_1=milk[1].End-milk[1].Begin   ,   max_nobody=0;
    int at_least_1_tot=milk[1].End-milk[1].Begin;
    for(int i=2;i<=n;i++){
        if(milk[i].Begin<=tmpend){//区间相连
            if(milk[i].End>=tmpend)
                at_least_1_tot+=milk[i].End-tmpend;
            tmpend=max(tmpend,milk[i].End);
        }
        else{//有空隙 
            max_at_least_1=max(max_at_least_1,at_least_1_tot);//取最大值 
            at_least_1_tot=milk[i].End-milk[i].Begin;//还原重新累计 
            max_nobody=max(max_nobody,milk[i].Begin-tmpend);
            tmpend=max(tmpend,milk[i].End);
        }
    } 
    printf("%d %d", max_at_least_1 , max_nobody );
     
    return 0;
}

 

转载于:https://www.cnblogs.com/Tidoblogs/p/11291670.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、付费专栏及课程。

余额充值