-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFairPhotography.java
More file actions
85 lines (83 loc) · 1.88 KB
/
FairPhotography.java
File metadata and controls
85 lines (83 loc) · 1.88 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.io.*;
import java.util.*;
public class FairPhotography {
static int[] pW;
static int[] pS;
static Cow[] cows;
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader br = new BufferedReader(new FileReader("swap.in"));
int N = Integer.parseInt(br.readLine());
pS = new int[N]; pW = new int[N]; cows = new Cow[N];
String[] temp;
for (int i = 0; i < N; i++)
{
temp = br.readLine().split(" ");
cows[i] = new Cow(Integer.parseInt(temp[0]), temp[1].charAt(0));
}
Comparator<Cow> c = new Comparator<Cow>()
{
public int compare(Cow o1, Cow o2)
{
return o1.position-o2.position; //ascending by distance
}
};
Arrays.sort(cows, c);
if (cows[0].type == 'S')
pS[0]++;
else
pW[0]++;
for (int i = 1; i < cows.length; i++)
{
if (cows[i].type == 'S')
pS[i] = pS[i-1] + 1;
else
pW[i] = pW[i-1] + 1;
}
int start = 0;
int end = cows[N-1].position-cows[0].position;
int med = 0;
while (start != end)
{
med = (start + end+1) / 2;
if (pass(med))
start = med;
else
end = med-1;
}
br.close();
//PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("homework.out")));
//pw.close();
}
static class Cow
{
public int position;
public char type;
public Cow(int position, char type)
{
this.position = position;
this.type = type;
}
public String toString() {
return position + " " + type;
}
}
public static boolean pass(int med)
{
int j = 0;
for (int i = 0; i < cows.length; i++)
{
while (j < cows.length && cows[j].position - cows[i].position <= med)
j++;
if (j == cows.length && cows[j].position - cows[i].position < med)
return false;
j--;
if ((j-i)%2 == 0)
continue;
if (pW[j] - pW[i] >= pS[j] - pS[i])
return true;
}
return false;
}
}