Welcome to FutureAppLaboratory

v=(*^ワ^*)=v

Analysis of PROB Milking Cows

| Comments

Description

(From train.usaco.org) 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.

1
2
3
4
5
SAMPLE INPUT (file milk2.in)
3
300 1000
700 1200
1500 2100
1
2
SAMPLE OUTPUT (file milk2.out)
900 300

Analysis

A straight-forward, brute-force solution is to keep a large array $k$, with each element $k_i$ is a boolean value indicates whether at least one cow is being milked at time $i$. Then travel from the start to the end to get the answer.

Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
ID: viennak1
PROB: milk2
LANG: C++
*/

// Section 1.2 PROB Milking Cows

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cassert>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <numeric>
#include <fstream>
#include <iostream>
#include <utility>
#include <iomanip>
#include <stack>
using namespace std;

int N;
int s[5000],t[5000];
bool k[1000005];
int res1,res2;
int main(){
    ofstream fout ("milk2.out");
    ifstream fin ("milk2.in");
    fin>>N;
    for(int i=0;i<N;i++) fin>>s[i]>>t[i];
    for(int i=N-1;i>0;i--) for(int j=0;j<i;j++){
        if(s[j]>s[j+1]) swap(s[j],s[j+1]),swap(t[j],t[j+1]);
        else if(s[j]==s[j+1] && t[j]<t[j+1]) swap(t[j],t[j+1]);
    }
    int last=0;
    for(int i=0;i<N;i++){
        if(s[i]<=last){
            if(t[i]<=last) continue;
            for(int j=last;j<t[i];j++) k[j]=true;
            last=t[i];
        }
        else{
            for(int j=s[i];j<t[i];j++) k[j]=true;
            last=t[i];
        }
    }
    bool mark=false;
    int end=0; for(int i=0;i<N;i++) end=max(end,t[i]);
    int start=0;
    if(s[0]>500) start=last=500, res2=s[0]-500;
    else start=last=s[0],mark=true;
    for(int i=start;i<=end+1;i++){
        if(mark==k[i]) continue;
        if(!mark) res2=max(res2,i-last), last=i, mark=true;
        else res1=max(res1,i-last), last=i, mark=false;
    }
    fout<<res1<<" "<<res2<<endl;
    //for(int i=0;i<21;i++) cout<<k[i]<<" ";
    return 0;
}

Comments