From 68edefc41f6f5b446e51df3d3b4f57a3eed67d11 Mon Sep 17 00:00:00 2001 From: Kevin Sheng Date: Wed, 27 Dec 2023 16:50:59 -0800 Subject: [PATCH 1/5] fix counting liars --- solutions/bronze/usaco-1228.mdx | 80 +++++++++++++++------------------ 1 file changed, 36 insertions(+), 44 deletions(-) diff --git a/solutions/bronze/usaco-1228.mdx b/solutions/bronze/usaco-1228.mdx index 65f0cd98b0..a11c4cb9fb 100644 --- a/solutions/bronze/usaco-1228.mdx +++ b/solutions/bronze/usaco-1228.mdx @@ -41,16 +41,14 @@ int main() { sort(cows.begin(), cows.end()); - /* - * lying_left[i] stores the number of cows to the left of cow i - * that must be lying given that Bessie is at the position of cow i. - */ + // lying_left[i] stores the number of cows to the left of cow i + // that must be lying given that Bessie is at the position of cow i. vector lying_left(n); for (int i = 1; i < n; i++) { - // Add up all the cows that are already lying to the left of our pos. + // Add up all the cows that are lying to the left of our pos. lying_left[i] += lying_left[i - 1]; - if (cows[i - 1].second == 'L' && cows[i].first > cows[i - 1].first) { + if (cows[i - 1].second == 'L' && cows[i].first >= cows[i - 1].first) { /* * If the cow before says our position is to the left * but their position is strictly less than our position, @@ -60,16 +58,14 @@ int main() { } } - /* - * lying_right stores the same thing, but does it so for the cows - * to the *right* of i. - */ + // lying_right stores the same thing, but does it so for the cows + // to the *right* of i. vector lying_right(n); // Fill it up in much the same way. for (int i = n - 2; i >= 0; i--) { lying_right[i] += lying_right[i + 1]; - if (cows[i + 1].second == 'G' && cows[i].first < cows[i + 1].first) { + if (cows[i + 1].second == 'G' && cows[i].first <= cows[i + 1].first) { lying_right[i]++; } } @@ -92,7 +88,7 @@ import java.util.*; public class CountingLiars { // BeginCodeSnip{Cow Class} - static class Cow { + static class Cow implements Comparable { char statement; int pos; @@ -100,35 +96,40 @@ public class CountingLiars { this.statement = statement; this.pos = pos; } + + @Override + public int compareTo(Cow c) { + if (pos != c.pos) { + return pos - c.pos; + } + return statement - c.statement; + } } // EndCodeSnip public static void main(String[] args) throws IOException { BufferedReader read = - new BufferedReader(new InputStreamReader(System.in)); + new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(read.readLine()); Cow[] cows = new Cow[n]; for (int i = 0; i < n; i++) { StringTokenizer cow = new StringTokenizer(read.readLine()); cows[i] = new Cow(cow.nextToken().charAt(0), - Integer.parseInt(cow.nextToken())); + Integer.parseInt(cow.nextToken())); } read.close(); - Arrays.sort(cows, Comparator.comparingInt(c -> c.pos)); + Arrays.sort(cows); - /* - * lying_left[i] stores the number of cows to the left of cow i - * that must be lying given that Bessie is at the position of cow i. - */ + // lying_left[i] stores the number of cows to the left of cow i + // that must be lying given that Bessie is at the position of cow i. int[] lying_left = new int[n]; for (int i = 1; i < n; i++) { - // Add up all the cows that are already lying to the left of our - // pos. + // Add up all the cows that are lying to the left of our pos. lying_left[i] += lying_left[i - 1]; - if (cows[i - 1].statement == 'L' && cows[i].pos > cows[i - 1].pos) { + if (cows[i - 1].statement == 'L' && cows[i].pos >= cows[i - 1].pos) { /* * If the cow before says our position is to the left * but their position is strictly less than our position, @@ -138,16 +139,14 @@ public class CountingLiars { } } - /* - * lying_right stores the same thing, but does it so for the cows - * to the *right* of i. - */ + // lying_right stores the same thing, but does it so for the cows + // to the *right* of i. int[] lying_right = new int[n]; // Fill it up in much the same way. for (int i = n - 2; i >= 0; i--) { lying_right[i] += lying_right[i + 1]; - if (cows[i + 1].statement == 'G' && cows[i].pos < cows[i + 1].pos) { + if (cows[i + 1].statement == 'G' && cows[i].pos <= cows[i + 1].pos) { lying_right[i]++; } } @@ -180,35 +179,28 @@ for _ in range(n): statement, pos = input().split() cows.append(Cow(int(pos), statement)) -cows.sort(key=lambda c: c.pos) +cows.sort(key=lambda c: (c.pos, c.statement)) -""" -lying_left[i] stores the number of cows to the left of cow i -that must be lying given that Bessie is at the position of cow i. -""" +# lying_left[i] stores the number of cows to the left of cow i +# that must be lying given that Bessie is at the position of cow i. lying_left = [0 for _ in range(n)] for i in range(1, n): - # Add up all the cows that are already lying to the left of our pos. + # Add up all the cows that are lying to the left of our pos. lying_left[i] += lying_left[i - 1] - if cows[i - 1].statement == "L" and cows[i].pos > cows[i - 1].pos: - """ - If the cow before says our position is to the left - but their position is strictly less than our position, - they're lying. - """ + if cows[i - 1].statement == "L" and cows[i].pos >= cows[i - 1].pos: + # If the cow before says our position is to the left + # but their position is strictly less than our position, they're lying. lying_left[i] += 1 -""" -lying_right stores the same thing, but does it so for the cows -to the *right* of i. -""" +# lying_right stores the same thing, but does it so for the cows +# to the *right* of i. lying_right = [0 for _ in range(n)] # Fill it up in much the same way. for i in range(n - 2, -1, -1): lying_right[i] += lying_right[i + 1] - if cows[i + 1].statement == "G" and cows[i].pos < cows[i + 1].pos: + if cows[i + 1].statement == "G" and cows[i].pos <= cows[i + 1].pos: lying_right[i] += 1 min_liars = n From 4f6d535d7205359807132554ac1044836707c63a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 28 Dec 2023 00:52:32 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/bronze/usaco-1228.mdx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/solutions/bronze/usaco-1228.mdx b/solutions/bronze/usaco-1228.mdx index a11c4cb9fb..b9da909575 100644 --- a/solutions/bronze/usaco-1228.mdx +++ b/solutions/bronze/usaco-1228.mdx @@ -99,9 +99,7 @@ public class CountingLiars { @Override public int compareTo(Cow c) { - if (pos != c.pos) { - return pos - c.pos; - } + if (pos != c.pos) { return pos - c.pos; } return statement - c.statement; } } @@ -109,14 +107,14 @@ public class CountingLiars { public static void main(String[] args) throws IOException { BufferedReader read = - new BufferedReader(new InputStreamReader(System.in)); + new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(read.readLine()); Cow[] cows = new Cow[n]; for (int i = 0; i < n; i++) { StringTokenizer cow = new StringTokenizer(read.readLine()); cows[i] = new Cow(cow.nextToken().charAt(0), - Integer.parseInt(cow.nextToken())); + Integer.parseInt(cow.nextToken())); } read.close(); @@ -129,7 +127,8 @@ public class CountingLiars { // Add up all the cows that are lying to the left of our pos. lying_left[i] += lying_left[i - 1]; - if (cows[i - 1].statement == 'L' && cows[i].pos >= cows[i - 1].pos) { + if (cows[i - 1].statement == 'L' && + cows[i].pos >= cows[i - 1].pos) { /* * If the cow before says our position is to the left * but their position is strictly less than our position, @@ -146,7 +145,8 @@ public class CountingLiars { for (int i = n - 2; i >= 0; i--) { lying_right[i] += lying_right[i + 1]; - if (cows[i + 1].statement == 'G' && cows[i].pos <= cows[i + 1].pos) { + if (cows[i + 1].statement == 'G' && + cows[i].pos <= cows[i + 1].pos) { lying_right[i]++; } } From 616f7b81f9a7f1d4e977de5d2a6a3fe2394910bb Mon Sep 17 00:00:00 2001 From: SansPapyrus683 <55369003+SansPapyrus683@users.noreply.github.com> Date: Wed, 27 Dec 2023 17:02:26 -0800 Subject: [PATCH 3/5] Apply suggestions from code review Co-authored-by: envyaims <79723780+envyaims@users.noreply.github.com> --- solutions/bronze/usaco-1228.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solutions/bronze/usaco-1228.mdx b/solutions/bronze/usaco-1228.mdx index b9da909575..7d352bc4d0 100644 --- a/solutions/bronze/usaco-1228.mdx +++ b/solutions/bronze/usaco-1228.mdx @@ -51,7 +51,7 @@ int main() { if (cows[i - 1].second == 'L' && cows[i].first >= cows[i - 1].first) { /* * If the cow before says our position is to the left - * but their position is strictly less than our position, + * but their position is strictly less than or equal to our position, * they're lying. */ lying_left[i]++; @@ -190,7 +190,7 @@ for i in range(1, n): if cows[i - 1].statement == "L" and cows[i].pos >= cows[i - 1].pos: # If the cow before says our position is to the left - # but their position is strictly less than our position, they're lying. + # but their position is strictly less than or equal to our position, they're lying. lying_left[i] += 1 # lying_right stores the same thing, but does it so for the cows From bff437e60256ae9bb2a01712d26ae88090165931 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 28 Dec 2023 01:06:04 +0000 Subject: [PATCH 4/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- solutions/bronze/usaco-1228.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/solutions/bronze/usaco-1228.mdx b/solutions/bronze/usaco-1228.mdx index 7d352bc4d0..58a9b28776 100644 --- a/solutions/bronze/usaco-1228.mdx +++ b/solutions/bronze/usaco-1228.mdx @@ -51,8 +51,8 @@ int main() { if (cows[i - 1].second == 'L' && cows[i].first >= cows[i - 1].first) { /* * If the cow before says our position is to the left - * but their position is strictly less than or equal to our position, - * they're lying. + * but their position is strictly less than or equal to our + * position, they're lying. */ lying_left[i]++; } From 73b3448f31aac5a97771e0005087f04c6c40f192 Mon Sep 17 00:00:00 2001 From: SansPapyrus683 <55369003+SansPapyrus683@users.noreply.github.com> Date: Wed, 27 Dec 2023 20:45:37 -0800 Subject: [PATCH 5/5] Update usaco-1228.mdx --- solutions/bronze/usaco-1228.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/solutions/bronze/usaco-1228.mdx b/solutions/bronze/usaco-1228.mdx index 58a9b28776..ca9e221491 100644 --- a/solutions/bronze/usaco-1228.mdx +++ b/solutions/bronze/usaco-1228.mdx @@ -45,7 +45,7 @@ int main() { // that must be lying given that Bessie is at the position of cow i. vector lying_left(n); for (int i = 1; i < n; i++) { - // Add up all the cows that are lying to the left of our pos. + // Add up all the cows that are lying to the left of our position. lying_left[i] += lying_left[i - 1]; if (cows[i - 1].second == 'L' && cows[i].first >= cows[i - 1].first) { @@ -124,7 +124,7 @@ public class CountingLiars { // that must be lying given that Bessie is at the position of cow i. int[] lying_left = new int[n]; for (int i = 1; i < n; i++) { - // Add up all the cows that are lying to the left of our pos. + // Add up all the cows that are lying to the left of our position. lying_left[i] += lying_left[i - 1]; if (cows[i - 1].statement == 'L' && @@ -185,7 +185,7 @@ cows.sort(key=lambda c: (c.pos, c.statement)) # that must be lying given that Bessie is at the position of cow i. lying_left = [0 for _ in range(n)] for i in range(1, n): - # Add up all the cows that are lying to the left of our pos. + # Add up all the cows that are lying to the left of our position. lying_left[i] += lying_left[i - 1] if cows[i - 1].statement == "L" and cows[i].pos >= cows[i - 1].pos: