zoukankan      html  css  js  c++  java
  • USACO1.2.1Milking 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
    题解:离散化和线段树都不会,所以只能用最暴力的方法。。。。开一个数组,有人标记为1,没人标记为0。但要注意边界问题,结束那个点不用标记。最后扫描一遍即可。时间复杂度O(N)。
    View Code
     1 /*
     2 ID:spcjv51
     3 PROG:milk2
     4 LANG:C
     5 */
     6 #include<stdio.h>
     7 int main(void)
     8 {
     9     FILE *fin=fopen("milk2.in","r");
    10     FILE *fout=fopen("milk2.out","w");
    11     short f[1000005];
    12     long i,l,r,n,ll,maxcontinuous,maxidle,maxn,minn;
    13     fscanf(fin,"%ld",&n);
    14     memset(f,0,sizeof(f));
    15     maxn=0;
    16     minn=100000;
    17     maxidle=0;
    18     while(n--)
    19     {
    20           fscanf(fin,"%ld%ld",&l,&r);
    21           for(i=l;i<r;i++)
    22             if (!f[i]) f[i]=1;
    23           if(l<minn) minn=l;
    24           if(r>maxn) maxn=r;
    25 
    26     }
    27     i=minn;
    28     ll=0;
    29     while(i<maxn&&f[i]==1)
    30     {
    31         ll++;
    32         i++;
    33     }
    34      maxcontinuous=ll;
    35      while(i<maxn)
    36      {
    37          ll=0;
    38          while(i<maxn&&f[i]==0)
    39          {
    40              ll++;
    41              i++;
    42          }
    43          if(ll>maxidle) maxidle=ll;
    44          ll=0;
    45          while(i<maxn&&f[i]==1)
    46          {
    47              ll++;
    48              i++;
    49          }
    50          if(ll>maxcontinuous)maxcontinuous=ll;
    51      }
    52     fprintf(fout,"%ld %ld\n",maxcontinuous,maxidle);
    53     fclose(fin);
    54     fclose(fout);
    55     return 0;
    56 }


  • 相关阅读:
    PHP7 快速编译安装
    php访问url的四种方式
    php获取文件 return array数组的值
    thinkphp3.2自定义success及error跳转页面
    think php 访问时
    vtk点云数据的显示[转]
    strlen() 和 sizeof() 在字符串中的使用
    函数 MultiByteToWideChar() 详解
    函数WideCharToMultiByte() 详解
    wchar_t 和 char 之间转换
  • 原文地址:https://www.cnblogs.com/zjbztianya/p/2856655.html
Copyright © 2011-2022 走看看