码迷,mamicode.com
首页 > 其他好文 > 详细

Usaco 1.2.1 Milking Cows

时间:2017-10-25 11:41:23      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:set   write   sizeof   src   idle   while   done   get   can   

emmmm,重温usaco,然后这个智障模拟题的模拟做法,我WA了10发才对,这就很尴尬了,主要是判断条件出了很多锅,一会这个点WA,一会那个点的,要gg的节奏。

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.
  • INPUT FORMAT

    Line 1: The single integer, N
    Lines 2..N+1: Two non-negative integers less than 1,000,000, respectively 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

题目大意就是一群农民挤牛奶,每个人在一个时间段挤牛奶,求最长至少有一人在挤奶的时间长度和最长的无人挤奶的时间长度。数据范围小到炸,所以模拟手玩就可以了。

 

技术分享
#include<cstdio>
#include<algorithm>
using namespace std;
inline int read(){
    int k=0;
    char c=getchar();
    while(c<0||c>9)
        c=getchar();
    while(c>=0&&c<=9)
    {
        k=k*10+c-0;
        c=getchar();
    }
    return k;
} 

struct node
{
    int x,y;
};

int cmp(const node&a,const node&b){
    return a.x<b.x;
}

int main()
{
    freopen("milk2.in","r",stdin);
    freopen("milk2.out","w",stdout);
    int n=read();
    node flo[n+7];
    for(int i=0;i<n;++i){
      flo[i].x=read();
      flo[i].y=read();
    }
    sort(flo,flo+n,cmp);
    int ans=flo[0].y-flo[0].x;
    int ans1=0;
    for(int i=0;i<n-1;++i){
        int dou=flo[i].y;
        int j=1;
        while(dou>=flo[i+j].x&&i+j<n-1)
        {
            dou=dou>flo[i+j].y?dou:flo[i+j].y;
            ++j;
        }
        dou-=flo[i].x;
        i=i+j-1;
        ans=(ans<dou?dou:ans);
    }
    int left=flo[0].y;
    for(int i=1;i<n;++i) {
        while(flo[i].x<=left&&i<n) {
            left=left>flo[i].y?left:flo[i].y;
            ++i;
        }
        if(flo[i].x>left&&i<n) {
            ans1=ans1>(flo[i].x-left)?ans1:(flo[i].x-left);
            left=flo[i].y;
        }
    }
    printf("%d ",ans);
    printf("%d\n",ans1);
    return 0;
}
排序手玩
技术分享
#include <cstdio>
#include <vector>
#include <string.h>
using namespace std;
 
int N;
int WorkTime[1000001];
int Work1,Work2;
int i,j;
int maxWork,maxFree;
int ans1,ans2;
 
int main(){
    freopen("milk2.in","r",stdin);
    freopen("milk2.out","w",stdout);
    memset(WorkTime,0,sizeof(WorkTime));
    scanf("%d",&N);
    int max=0;
    int min=1000001;
    for(i=1;i<=N;i++) {
        scanf("%d %d",&Work1,&Work2);
        for(j=Work1+1;j<=Work2;j++){
        WorkTime[j]=1;
        if(Work1<min) min=Work1;
        if(Work2>max) max=Work2;
        }
    }
    maxWork=0;
    for(i=min;i<=max+1;i++){
       if(WorkTime[i]==1){
                ans1++;
       }else{
        if(ans1>maxWork) maxWork=ans1;
        ans1=0;
    }
}
    maxFree=0;
    for(i=min+1;i<=max;i++){
       if(WorkTime[i]==0) ans2++;
       else{
            if(ans2>maxFree) maxFree=ans2;
            ans2=0;
       }
    }
    printf("%d %d\n",maxWork,maxFree);
}
统计手玩

 

Usaco 1.2.1 Milking Cows

标签:set   write   sizeof   src   idle   while   done   get   can   

原文地址:http://www.cnblogs.com/wykjx1314/p/7727044.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!