Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6027eb0

Browse files
committed
Magical Cows DP problem added
1 parent dfa82f5 commit 6027eb0

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

‎AlgorithmCode/MagicalCows.java

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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /