Skip to content

Commit 4c00ccf

Browse files
William FisetWilliam Fiset
William Fiset
authored and
William Fiset
committed
MagicalCows slides + formattin
1 parent 79e68ba commit 4c00ccf

File tree

2 files changed

+12
-17
lines changed

2 files changed

+12
-17
lines changed
-1.8 MB
Binary file not shown.

src/main/java/com/williamfiset/algorithms/dp/examples/magicalcows/MagicalCows.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
/**
2-
* Solution to Magical Cows (https://open.kattis.com/problems/magicalcows)
3-
* Problem author: Graeme Zinck
4-
* Solution by: William Fiset
2+
* Solution to Magical Cows (https://open.kattis.com/problems/magicalcows) Problem author: Graeme
3+
* Zinck Solution by: William Fiset
54
*
6-
* The main thing to realize with magical cows is that the total number of cows
7-
* allowed on each farm is bounded by C which is less than or equal to 1000, so
8-
* you can keep track of all cows in a frequency table for each farm size.
9-
*
10-
* NOTE: You can ignore taking the floor/ceiling of the number of cows on a
11-
* split since when you double the number of cows you always get an even number.
5+
* <p>The main thing to realize with magical cows is that the total number of cows allowed on each
6+
* farm is bounded by C which is less than or equal to 1000, so you can keep track of all cows in a
7+
* frequency table for each farm size.
8+
*
9+
* <p>NOTE: You can ignore taking the floor/ceiling of the number of cows on a split since when you
10+
* double the number of cows you always get an even number.
1211
*/
13-
1412
import java.io.*;
1513
import java.util.*;
1614

@@ -33,8 +31,8 @@ public static void main(String[] args) throws IOException {
3331
// The number of queries
3432
final int M = Integer.parseInt(line[2]);
3533

36-
// The dp table.
37-
long[][] dp = new long[MAX_DAYS+1][C+1];
34+
// The dp table.
35+
long[][] dp = new long[MAX_DAYS + 1][C + 1];
3836

3937
// Count the initial frequency of farms of different sizes
4038
for (int i = 0; i < N; i++) {
@@ -47,11 +45,11 @@ public static void main(String[] args) throws IOException {
4745
for (int i = 1; i <= C; i++) {
4846
if (2 * i <= C) {
4947
// Cow count on farm with size `i` doubled, but the number of farms did not.
50-
dp[day][2*i] += dp[day-1][i];
48+
dp[day][2 * i] += dp[day - 1][i];
5149
} else {
5250
// The number of cows per farm on the farm with size `i` exceeds the
5351
// permitted limit, so double the number of farms.
54-
dp[day][i] += 2 * dp[day-1][i];
52+
dp[day][i] += 2 * dp[day - 1][i];
5553
}
5654
}
5755
}
@@ -73,7 +71,4 @@ private static long query(long[][] dp, int day) {
7371
}
7472
return farms;
7573
}
76-
7774
}
78-
79-

0 commit comments

Comments
 (0)