java solution milk the cow

Solutions on MaxInterview for java solution milk the cow by the best coders in the world

showing results for - "java solution milk the cow"
Brian
29 May 2018
1import java.util.*;
2import java.io.*;
3/*
4ID: coconut
5LANG: JAVA
6TASK: milk2
7*/
8public class milk2 {
9    static farmer[] farmers;
10    public static void main(String[] args) throws Exception  {
11        Scanner sc = new Scanner(new File("milk2.in"));
12        farmers = new farmer[sc.nextInt()];    
13        for (int i = 0; i < farmers.length; i++){
14            int temp = sc.nextInt();
15            int temp2 = sc.nextInt();
16            farmers[i] = new farmer(temp, temp2);
17        }
18            
19        sc.close();
20        //
21        int longestMilk = longestMilk();
22        int longestNoMilk = longestNoMilk();
23        //
24        PrintWriter out = new PrintWriter(new File("milk2.out"));
25        out.print(longestMilk + " ");
26        out.println(longestNoMilk + "");
27        out.close();
28    }
29    static int longestMilk() {
30        mergeSort(farmers);
31         int[] allInterval = new int[farmers.length];
32         int lastMAX = -1;
33         int lastIndex = 0;
34         for(int i = 0;i < farmers.length; i++) { 
35             
36             if(lastMAX >= farmers[i].start) {
37                 if( farmers[i].end > lastMAX){
38                     allInterval[lastIndex] += farmers[i].end - lastMAX;
39                     lastMAX = farmers[i].end;
40                 }
41                     
42                
43             } else {
44                 //create a new interval
45                 allInterval[i] = farmers[i].end - farmers[i].start;
46                 lastMAX = farmers[i].end;
47                 lastIndex = i;
48             }
49            
50         }
51         lastMAX = -1;
52        
53         for(int i : allInterval){
54             if(i > lastMAX)
55                lastMAX = i;
56         }
57         return lastMAX;
58            
59    }
60    static int longestNoMilk() {
61        int[] aMI = new int[farmers.length * 2];
62        Arrays.fill(aMI, -1);
63        int point = 0;
64        for(farmer i : farmers){
65            boolean newIn = true;
66            for(int j = 0; j < point; j+=2){
67                 
68                 if(aMI[j] == i.start){
69                    if(aMI[j+1] < i.end)
70                         aMI[j+1] = i.end;
71                    newIn = false;
72                    break; 
73                    
74                        
75                 } else if (i.start > aMI[j] && i.start < aMI[j+1]) {
76                     if(i.end > aMI[j+1])
77                         aMI[j+1] = i.end;
78                    newIn = false;
79                     break;
80                 } else if (i.start < aMI[j] && i.end > aMI[j]){
81                     if(i.end > aMI[j+1])
82                        aMI[j+1] = i.end;
83                     newIn = false;
84                     break;
85                 }
86            }
87            if (newIn){
88                aMI[point++] = i.start;
89                aMI[point++] = i.end;
90            }
91             
92        }
93        //point -> smol
94        point = -1;
95        int biggestNoCow = 0;
96        for(int i = 1; i < aMI.length; i++){ 
97            if(point == -1)
98                point = aMI[i];
99            else {
100                int temp = Math.max(0, aMI[i] - point);
101                if(temp >biggestNoCow)
102                    biggestNoCow = temp;
103                point = -1;
104            }
105        }
106        return biggestNoCow;
107    }
108    static void mergeSort(farmer[] list){
109        if(list.length > 1) {
110			farmer[] firstHalf = new farmer[list.length/2];
111			farmer[] secondHalf = new farmer[list.length - firstHalf.length];
112			System.arraycopy(list, 0, firstHalf, 0, firstHalf.length);
113			
114			System.arraycopy(list, firstHalf.length, secondHalf,0 , secondHalf.length);
115			mergeSort(firstHalf);
116			mergeSort(secondHalf);
117			merge0(firstHalf, secondHalf, list);
118		}
119    }
120    static void merge0(farmer[] f, farmer[] s , farmer[] source ) {
121		int c1 = 0,c2=0,c3=0;
122		while(c1 < f.length && c2 < s.length) {
123			if (f[c1].start < s[c2].start) {
124				source[c3] = f[c1];	
125				c1++;
126			} else {
127				source[c3] = s[c2];
128				c2++;
129			}
130			c3++;
131		}
132		while (c1 < f.length) {
133			source[c3] = f[c1];
134			c1++;
135			c3++;
136		}
137		while (c2 < s.length) {
138			source[c3] = s[c2];
139			c2++;
140			c3++;
141		}
142	}
143}
144class farmer{
145    public int start = 0;
146    public int end = 0;
147    public farmer(int start, int end){
148        this.start = start;
149        this.end = end;
150    }
151}
152
153
154
155