@@ -41,35 +41,31 @@ int main() {
41
41
42
42
sort (cows.begin(), cows.end());
43
43
44
- /*
45
- * lying_left[ i] stores the number of cows to the left of cow i
46
- * that must be lying given that Bessie is at the position of cow i.
47
- * /
44
+ // lying_left[ i] stores the number of cows to the left of cow i
45
+ // that must be lying given that Bessie is at the position of cow i.
48
46
vector<int > lying_left(n);
49
47
for (int i = 1; i < n; i++) {
50
- // Add up all the cows that are already lying to the left of our pos .
48
+ // Add up all the cows that are lying to the left of our position .
51
49
lying_left [i ] += lying_left [i - 1 ];
52
50
53
- if (cows [i - 1 ].second == ' L' && cows [i ].first > cows [i - 1 ].first ) {
51
+ if (cows [i - 1 ].second == ' L' && cows [i ].first >= cows [i - 1 ].first ) {
54
52
/*
55
53
* If the cow before says our position is to the left
56
- * but their position is strictly less than our position,
57
- * they're lying.
54
+ * but their position is strictly less than or equal to our
55
+ * position, they're lying.
58
56
*/
59
57
lying_left[i ]++;
60
58
}
61
59
}
62
60
63
- /*
64
- * lying_right stores the same thing, but does it so for the cows
65
- * to the *right* of i.
66
- */
61
+ // lying_right stores the same thing, but does it so for the cows
62
+ // to the *right* of i.
67
63
vector<int> lying_right(n);
68
64
// Fill it up in much the same way.
69
65
for (int i = n - 2; i >= 0; i--) {
70
66
lying_right[i ] += lying_right[i + 1 ];
71
67
72
- if (cows[i + 1 ].second == 'G' && cows[i ].first < cows[i + 1 ].first) {
68
+ if (cows[i + 1 ].second == 'G' && cows[i ].first <= cows[i + 1 ].first) {
73
69
lying_right[i ]++;
74
70
}
75
71
}
@@ -92,14 +88,20 @@ import java.util.*;
92
88
93
89
public class CountingLiars {
94
90
// BeginCodeSnip{Cow Class}
95
- static class Cow {
91
+ static class Cow implements Comparable<Cow> {
96
92
char statement;
97
93
int pos;
98
94
99
95
public Cow(char statement, int pos) {
100
96
this.statement = statement;
101
97
this.pos = pos;
102
98
}
99
+
100
+ @Override
101
+ public int compareTo(Cow c) {
102
+ if (pos != c.pos) { return pos - c.pos; }
103
+ return statement - c.statement;
104
+ }
103
105
}
104
106
// EndCodeSnip
105
107
@@ -116,19 +118,17 @@ public class CountingLiars {
116
118
}
117
119
read.close();
118
120
119
- Arrays.sort(cows, Comparator.comparingInt(c -> c.pos) );
121
+ Arrays.sort(cows);
120
122
121
- /*
122
- * lying_left[i] stores the number of cows to the left of cow i
123
- * that must be lying given that Bessie is at the position of cow i.
124
- */
123
+ // lying_left[i] stores the number of cows to the left of cow i
124
+ // that must be lying given that Bessie is at the position of cow i.
125
125
int[] lying_left = new int[n ];
126
126
for (int i = 1; i < n; i++) {
127
- // Add up all the cows that are already lying to the left of our
128
- // pos.
127
+ // Add up all the cows that are lying to the left of our position.
129
128
lying_left[i ] += lying_left[i - 1 ];
130
129
131
- if (cows[i - 1 ].statement == 'L' && cows[i ].pos > cows[i - 1 ].pos) {
130
+ if (cows[i - 1 ].statement == 'L' &&
131
+ cows[i ].pos >= cows[i - 1 ].pos) {
132
132
/*
133
133
* If the cow before says our position is to the left
134
134
* but their position is strictly less than our position,
@@ -138,16 +138,15 @@ public class CountingLiars {
138
138
}
139
139
}
140
140
141
- /*
142
- * lying_right stores the same thing, but does it so for the cows
143
- * to the *right* of i.
144
- */
141
+ // lying_right stores the same thing, but does it so for the cows
142
+ // to the *right* of i.
145
143
int[] lying_right = new int[n ];
146
144
// Fill it up in much the same way.
147
145
for (int i = n - 2; i >= 0; i--) {
148
146
lying_right[i ] += lying_right[i + 1 ];
149
147
150
- if (cows[i + 1 ].statement == 'G' && cows[i ].pos < cows[i + 1 ].pos) {
148
+ if (cows[i + 1 ].statement == 'G' &&
149
+ cows[i ].pos <= cows[i + 1 ].pos) {
151
150
lying_right[i ]++;
152
151
}
153
152
}
@@ -180,35 +179,28 @@ for _ in range(n):
180
179
statement , pos = input ().split ()
181
180
cows.append(Cow(int (pos ), statement))
182
181
183
- cows.sort(key = lambda c : c .pos )
182
+ cows.sort(key = lambda c : ( c .pos , c . statement ) )
184
183
185
- " " "
186
- lying_left[i] stores the number of cows to the left of cow i
187
- that must be lying given that Bessie is at the position of cow i.
188
- " " "
184
+ # lying_left[i ] stores the number of cows to the left of cow i
185
+ # that must be lying given that Bessie is at the position of cow i.
189
186
lying_left = [0 for _ in range (n )]
190
187
for i in range(1, n):
191
- # Add up all the cows that are already lying to the left of our pos .
188
+ # Add up all the cows that are lying to the left of our position .
192
189
lying_left [i ] += lying_left [i - 1 ]
193
190
194
- if cows [i - 1 ].statement == " L" and cows [i ].pos > cows [i - 1 ].pos :
195
- " " "
196
- If the cow before says our position is to the left
197
- but their position is strictly less than our position ,
198
- they' re lying.
199
- " " "
191
+ if cows [i - 1 ].statement == " L" and cows [i ].pos >= cows [i - 1 ].pos :
192
+ # If the cow before says our position is to the left
193
+ # but their position is strictly less than or equal to our position , they' re lying.
200
194
lying_left[i] += 1
201
195
202
- " " "
203
- lying_right stores the same thing, but does it so for the cows
204
- to the *right* of i.
205
- " " "
196
+ # lying_right stores the same thing, but does it so for the cows
197
+ # to the *right* of i.
206
198
lying_right = [0 for _ in range(n)]
207
199
# Fill it up in much the same way.
208
200
for i in range(n - 2, -1, -1):
209
201
lying_right [i ] += lying_right [i + 1 ]
210
202
211
- if cows [i + 1 ].statement == " G" and cows [i ].pos < cows [i + 1 ].pos :
203
+ if cows [i + 1 ].statement == " G" and cows [i ].pos <= cows [i + 1 ].pos :
212
204
lying_right [i ] += 1
213
205
214
206
min_liars = n
0 commit comments