Skip to content

Commit 05041ca

Browse files
Merge pull request #4141 from SansPapyrus683/master
fix counting liars
2 parents fdc47a4 + 73b3448 commit 05041ca

File tree

1 file changed

+36
-44
lines changed

1 file changed

+36
-44
lines changed

solutions/bronze/usaco-1228.mdx

+36-44
Original file line numberDiff line numberDiff line change
@@ -41,35 +41,31 @@ int main() {
4141

4242
sort(cows.begin(), cows.end());
4343

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.
4846
vector<int> lying_left(n);
4947
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.
5149
lying_left[i] += lying_left[i - 1];
5250

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) {
5452
/*
5553
* 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.
5856
*/
5957
lying_left[i]++;
6058
}
6159
}
6260

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.
6763
vector<int> lying_right(n);
6864
// Fill it up in much the same way.
6965
for (int i = n - 2; i >= 0; i--) {
7066
lying_right[i] += lying_right[i + 1];
7167

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) {
7369
lying_right[i]++;
7470
}
7571
}
@@ -92,14 +88,20 @@ import java.util.*;
9288

9389
public class CountingLiars {
9490
// BeginCodeSnip{Cow Class}
95-
static class Cow {
91+
static class Cow implements Comparable<Cow> {
9692
char statement;
9793
int pos;
9894

9995
public Cow(char statement, int pos) {
10096
this.statement = statement;
10197
this.pos = pos;
10298
}
99+
100+
@Override
101+
public int compareTo(Cow c) {
102+
if (pos != c.pos) { return pos - c.pos; }
103+
return statement - c.statement;
104+
}
103105
}
104106
// EndCodeSnip
105107

@@ -116,19 +118,17 @@ public class CountingLiars {
116118
}
117119
read.close();
118120

119-
Arrays.sort(cows, Comparator.comparingInt(c -> c.pos));
121+
Arrays.sort(cows);
120122

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.
125125
int[] lying_left = new int[n];
126126
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.
129128
lying_left[i] += lying_left[i - 1];
130129

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) {
132132
/*
133133
* If the cow before says our position is to the left
134134
* but their position is strictly less than our position,
@@ -138,16 +138,15 @@ public class CountingLiars {
138138
}
139139
}
140140

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.
145143
int[] lying_right = new int[n];
146144
// Fill it up in much the same way.
147145
for (int i = n - 2; i >= 0; i--) {
148146
lying_right[i] += lying_right[i + 1];
149147

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) {
151150
lying_right[i]++;
152151
}
153152
}
@@ -180,35 +179,28 @@ for _ in range(n):
180179
statement, pos = input().split()
181180
cows.append(Cow(int(pos), statement))
182181

183-
cows.sort(key=lambda c: c.pos)
182+
cows.sort(key=lambda c: (c.pos, c.statement))
184183

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.
189186
lying_left = [0 for _ in range(n)]
190187
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.
192189
lying_left[i] += lying_left[i - 1]
193190

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.
200194
lying_left[i] += 1
201195

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.
206198
lying_right = [0 for _ in range(n)]
207199
# Fill it up in much the same way.
208200
for i in range(n - 2, -1, -1):
209201
lying_right[i] += lying_right[i + 1]
210202

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:
212204
lying_right[i] += 1
213205

214206
min_liars = n

0 commit comments

Comments
 (0)