Usaco 1.2.1 Milking Cows
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Usaco 1.2.1 Milking Cows相关的知识,希望对你有一定的参考价值。
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 2100OUTPUT 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的主要内容,如果未能解决你的问题,请参考以下文章
USACO SECTION1 2.1 Milking Cows 简单贪心
洛谷P1204 [USACO1.2]挤牛奶Milking Cows