-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxMed.java
More file actions
55 lines (53 loc) · 1.26 KB
/
MaxMed.java
File metadata and controls
55 lines (53 loc) · 1.26 KB
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
import java.io.*;
import java.util.*;
public class MaxMed {
static int[] array;
static int N, K;
public static void main(String[] args) throws IOException, InterruptedException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] temp = br.readLine().split(" ");
N = Integer.parseInt(temp[0]);
K = Integer.parseInt(temp[1]);
array = new int[N];
temp = br.readLine().split(" ");
for (int i = 0; i < temp.length; i++)
array[i] = Integer.parseInt(temp[i]);
Arrays.sort(array);
long start = 0;
long end = 1500000000;
long mid;
boolean check;
while (start != end)
{
mid = (start + end + 1)/2;
if (pass(mid))
start = mid;
else
end = mid - 1;
System.out.println(start + " " + end);
}
if (N == 1)
start = K + array[0];
System.out.println(start);
}
private static boolean pass(long median)
{
int i = array.length - 1;
while (i > -1 && array[i] >= median)
i--;
int temp = K;
//if (median - array[i] > K) // if the first one less cannot be filled by K
// return false;
while (i > -1 && temp >= 0)
{
System.out.println(":) " + temp);
temp -= median - array[i];
i--;
}
i++; //first one cannot fill
if (i < N/2) //surpasses the median
return true;
return false;
}
}