zoukankan      html  css  js  c++  java
  • 洛谷P1204 [USACO1.2]挤牛奶Milking Cows

    题目描述

    三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶。第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒。第二个农民在700秒开始,在 1200秒结束。第三个农民在1500秒开始2100秒结束。期间最长的至少有一个农民在挤奶的连续时间为900秒(从300秒到1200秒),而最长的无人挤奶的连续时间(从挤奶开始一直到挤奶结束)为300秒(从1200秒到1500秒)。

    你的任务是编一个程序,读入一个有N个农民(1 <= N <= 5000)挤N头牛的工作时间列表,计算以下两点(均以秒为单位):

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

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

    输入输出格式

    输入格式:

     

    Line 1:

    一个整数N。

    Lines 2..N+1:

    每行两个小于1000000的非负整数,表示一个农民的开始时刻与结束时刻。

     

    输出格式:

     

    一行,两个整数,即题目所要求的两个答案。

     

    输入输出样例

    输入样例#1:
    3
    300 1000
    700 1200
    1500 2100
    
    输出样例#1:
    900 300
    

    说明

    题目翻译来自NOCOW。

    USACO Training Section 1.2

    模拟。这么水的题意外地WA了两次,发上来作纪念。

    首先要有人挤奶以后才能开始算无人挤奶的时间。

    /*by SilverN*/
    #include<algorithm>
    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    using namespace std;
    const int mxn=5100;
    int n;
    int s[mxn],t[mxn];
    int a[1000100];
    int ans1=0,ans2=0;
    inline int max(int a,int b){
        if(a>b)return a;
        return b;
    }
    int main(){
        scanf("%d",&n);
        int i,j;
        int ed=0;
        for(i=1;i<=n;i++){
            scanf("%d%d",&s[i],&t[i]);
            ed=max(ed,t[i]);
            a[s[i]]++;a[t[i]+1]--;
        }
        int last=1;
        bool flag_st=0,flag_non=0;
        bool flag_first=0;
        for(i=0;i<=ed;i++){
            a[i]=a[i-1]+a[i];
            if(a[i]>0){
                flag_first=1;
                flag_non=0;
                if(!flag_st){
                    flag_st=1;
                    last=i;
                }
                ans1=max(ans1,i-last);
            }
            else{
                if(!flag_first)continue;
                flag_st=0;
                if(!flag_non){
                    flag_non=1;
                    last=i;
                }
                ans2=max(i+1+1-last,ans2);
            }
        }
        printf("%d %d
    ",ans1,ans2);
        return 0;
    }
  • 相关阅读:
    centos 安装netstat
    du 常见的命令
    CentOS7 安装Python3.6.8
    Alpine安装telnet
    TypeError: 'NoneType' object is not callable
    docker中删除dead状态的容器
    监控进程,线程shell脚本
    pyinstaller打包py成exe后音乐文件播放异常pygame.error failed to execute script
    lambda expressions
    Domain logic approaches
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5882387.html
Copyright © 2011-2022 走看看