Skip to content

Commit 06cbde3

Browse files
William FisetWilliam Fiset
William Fiset
authored and
William Fiset
committed
Add solution to MagicalCows
1 parent 87491c5 commit 06cbde3

File tree

14 files changed

+107
-0
lines changed

14 files changed

+107
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
5
2+
10
3+
20
4+
40
5+
80
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
1 5 5
2+
1
3+
1
4+
1
5+
1
6+
1
7+
0
8+
1
9+
2
10+
3
11+
4
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
5
2+
7
3+
14
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2 5 3
2+
1
3+
2
4+
1
5+
2
6+
1
7+
0
8+
1
9+
2
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/**
2+
* Solution to Magical Cows (https://open.kattis.com/problems/magicalcows)
3+
* Problem author: Graeme Zinck
4+
* Solution by: William Fiset
5+
*
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.
12+
*/
13+
14+
import java.io.*;
15+
import java.util.*;
16+
17+
public class MagicalCows {
18+
19+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
20+
21+
// The maximum number of days.
22+
static final int MAX_DAYS = 50;
23+
24+
public static void main(String[] args) throws IOException {
25+
String[] line = br.readLine().split(" ");
26+
27+
// The maximum number of cows on a farm
28+
final int C = Integer.parseInt(line[0]);
29+
30+
// The initial number of farms
31+
final int N = Integer.parseInt(line[1]);
32+
33+
// The number of queries
34+
final int M = Integer.parseInt(line[2]);
35+
36+
// The dp table.
37+
long[][] dp = new long[MAX_DAYS+1][C+1];
38+
39+
// Count the initial frequency of farms of different sizes
40+
for (int i = 0; i < N; i++) {
41+
int cows = Integer.parseInt(br.readLine());
42+
dp[0][cows]++;
43+
}
44+
45+
for (int day = 1; day <= MAX_DAYS; day++) {
46+
// For all farm sizes between 1 and `C`, double the number of cows.
47+
for (int i = 1; i <= C; i++) {
48+
if (2 * i <= C) {
49+
// Cow count on farm with size `i` doubled, but the number of farms did not.
50+
dp[day][2*i] += dp[day-1][i];
51+
} else {
52+
// The number of cows per farm on the farm with size `i` exceeds the
53+
// permitted limit, so double the number of farms.
54+
dp[day][i] += 2 * dp[day-1][i];
55+
}
56+
}
57+
}
58+
59+
// Answer each query
60+
for (int i = 0; i < M; i++) {
61+
int day = Integer.parseInt(br.readLine());
62+
System.out.println(query(dp, day));
63+
}
64+
}
65+
66+
// Find the number of farms on a particular day by summing all farms
67+
// of every frequency for that day.
68+
private static long query(long[][] dp, int day) {
69+
long farms = 0;
70+
long[] frequencies = dp[day];
71+
for (int i = 0; i < frequencies.length; i++) {
72+
farms += frequencies[i];
73+
}
74+
return farms;
75+
}
76+
77+
}
78+
79+
Binary file not shown.

0 commit comments

Comments
 (0)