Title Description
Three farmers get up 5 o'clock in the morning every day and then go to the barn to milk 3 cows. The first farmer was milking his cows for 300 seconds (starting from 5 o'clock), until 1000 seconds. The second farmer starts at 700 seconds and ends in 1200 seconds. The third farmer ends at 2,100 seconds in 1500 seconds. During the longest period of time, at least one farmer has been milking for 900 seconds (from 300 seconds to 1200 seconds), while the longest continuous time of unmanned milking (from milking to milking ends) is 300 seconds (from 1200 seconds to 1500 seconds).
Your task is to compile a program that reads a list of working times for n Cows (1 <= n <= 5000) and calculates the following two points (in seconds):
A maximum of at least one person is in the milking time period.
The longest period of time for unmanned milking. (starting from the milking of people)
Input/output format
Input format:
Line 1:
An integer n.
Lines 2..n+1:
Each row of two non-negative integers less than 1000000 represents the beginning and end of a farmer's time.
Output format:
One line, two integers, which is the two answers required by the topic.
Input and Output Sample input example # #:
3300 1000700 12001500 2100
Sample # # of output:
900 300
Description
The title translation comes from Nocow.
Usaco Training Section 1.2
Ideas:
Offline practices
The segment tree covers all the intervals
Then convert to linear to find the maximum interval
Come on, on the code:
#include <cstdio>#include<string>#include<cstring>#include<iostream>using namespacestd;classT_tree { Public: intL,r,mid; BOOLFlag,dis; voidFlag_ () {flag=true; } voidMid_ () {Mid= (l+r) >>1; } BOOLif_ () {if(L==R)return true; Else return false; }};classT_tree tree[1000001*4];intn,maxn,do_l[5001],do_r[5001],cnt=0;CharCget;BOOLresult[1000001];inlinevoidRead_int (int&Now_) {Cget=GetChar (); while(cget>'9'|| cget<'0') cget=GetChar (); while(cget<='9'&&cget>='0') {Now_=now_*Ten+cget-'0'; Cget=GetChar (); }}voidTree_build (intNowintLintR) {TREE[NOW].L=l,tree[now].r=R; if(l==r) {Tree[now].dis=true; return ; } tree[now].mid_ (); Tree_build ( now<<1, L,tree[now].mid); Tree_build ( now<<1|1, tree[now].mid+1, R);} InlinevoidTree_down (intNow ) { if(TREE[NOW].L==TREE[NOW].R)return ; Tree[now<<1].flag_ (); Tree[now<<1].dis=false; Tree[now<<1|1].flag_ (); Tree[now<<1|1].dis=false;}voidTree_change (intNowintLintR) { if(tree[now].l==l&&tree[now].r==r) {Tree[now].dis=false; Tree[now].flag_ (); return ; } if(Tree[now].flag) Tree_down (now); if(R<=tree[now].mid) Tree_change (now<<1, L,r); Else if(L>tree[now].mid) Tree_change (now<<1|1, L,r); Else{Tree_change ( now<<1, L,tree[now].mid); Tree_change ( now<<1|1, tree[now].mid+1, R); }}voidTree_result (intNow ) { if(Tree[now].if_ ()) {result[++cnt]=Tree[now].dis; return ; } if(Tree[now].flag) Tree_down (now); Tree_result ( now<<1); Tree_result ( now<<1|1);}intMain () {read_int (n); for(intI=1; i<=n;i++) {read_int (do_l[i]); Read_int (Do_r[i]); MAXN=Max (maxn,do_r[i]); } tree_build (1,1, MAXN); for(intI=1; i<=n;i++) {Tree_change (1, do_l[i]+1, Do_r[i]); } tree_result (1); intmax_t=0, max_f=0, Max_tt; for(intI=1; i<=cnt;i++) { if(!Result[i]) {Max_tt=1; for(intj=i+1; j<=cnt;j++) { if(result[j]==result[j-1]) max_tt++; Else { if(result[j-1]) max_t=Max (MAX_T,MAX_TT); Elsemax_f=Max (MAX_F,MAX_TT); Max_tt=1; } } if(result[cnt]) max_t=Max (MAX_T,MAX_TT); Elsemax_f=Max (MAX_F,MAX_TT); Break; }} printf ("%d%d\n", max_f,max_t); return 0;}
AC Diary-Milking Valley P1204