Google Kickstart 2020 Round A: Plates Solution

Problem Statement

Problem

Dr. Patel has N stacks of plates. Each stack contains K plates. Each plate has a positive beauty value, describing how beautiful it looks.

Dr. Patel would like to take exactly P plates to use for dinner tonight. If he would like to take a plate in a stack, he must also take all of the plates above it in that stack as well.

Help Dr. Patel pick the P plates that would maximize the total sum of beauty values.

Input

The first line of the input gives the number of test cases, TT test cases follow. Each test case begins with a line containing the three integers NK and P. Then, N lines follow. The i-th line contains K integers, describing the beauty values of each stack of plates from top to bottom.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the maximum total sum of beauty values that Dr. Patel could pick.

Limits

Time limit: 20 seconds per test set.

Memory limit: 1GB.

1 ≤ T ≤ 100.

1 ≤ K ≤ 30.

1 ≤ P ≤ N * K.

The beauty values are between 1 and 100, inclusive.

Test set 1

1 ≤ N ≤ 3.

Test set 2

1 ≤ N ≤ 50.

Sample


Input


Output

2
2 4 5
10 10 100 30
80 50 10 50
3 2 3
80 80
15 50
20 10
Case #1: 250
Case #2: 180

In Sample Case #1, Dr. Patel needs to pick P = 5 plates:

  • He can pick the top 3 plates from the first stack (10 + 10 + 100 = 120).
  • He can pick the top 2 plates from the second stack (80 + 50 = 130) .

In total, the sum of beauty values is 250.

In Sample Case #2, Dr. Patel needs to pick P = 3 plates:

  • He can pick the top 2 plates from the first stack (80 + 80 = 160).
  • He can pick no plates from the second stack.
  • He can pick the top plate from the third stack (20).

In total, the sum of beauty values is 180.

Note: Unlike previous editions, in Kick Start 2020, all test sets are visible verdict test sets, meaning you receive instant feedback upon submission.

Problem link

Video Tutorial

You can find the detailed video tutorial here

Thought Process

First thought is similar to merging multiple lists, this won‘t work since the lists are not sorted and we are not allowed to sort due to the order constraint.

Second thought is keep a max heap, and always merge the largest value from all the top elements in the lists. This is wrong since greedy won‘t work here. E.g., below if we want to pick 3 plates, using max heap will pick 2, 2, 2, instead of 1, 1, 100

2, 2, 2, 2, 2

1, 1, 100, 100, 100

Third thought seems we have to brute force, for all the combos of P, we check what‘s the maximum value. Quote from the analysis, it‘s exponential time complexity

For example, if N = 3 and for any given values of K and P, generate all possible triples (S1, S2, S3) such that S1+S2+S3 = P and 0 ≤ Si ≤ K. Note: Si is the number of plates picked from the i-th stack.

This can be done via recursion and the total time complexity is O(KN) which abides by the time limits.

Forth and final solution to optimize this is using dynamic programming. It‘s very common in those coding competitions.

Quote from the analysis

First, let‘s consider an intermediate state dp[i][j] which denotes the maximum sum that can be obtained using the first i stacks when we need to pick j plates in total.

Next, we iterate over the stacks and try to answer the question: What is the maximum sum if we had to pick j plates in total using the i stacks we‘ve seen so far? This would give us dp[i][j]. However, we need to also decide, among those j plates, how many come from the i-th stack? i.e., Let‘s say we pick x plates from the i-th stack, then dp[i][j] = max(dp[i][j], sum[i][x]+dp[i-1][j-x]). Therefore, in order to pick j plates in total from i stacks, we can pick anywhere between [0, 1, ..., j] plates from the i-th stack and [j, j-1, ..., 0] plates from the previous i-1 stacks respectively. Also, we need to do this for all values of 1 ≤ j ≤ P.

The flow would look like:

for i [1, N]:

 for j [0, P]:

  dp[i][j] := 0

   for x [0, min(j, K)]:

    dp[i][j] = max(dp[i][j], sum[i][x]+dp[i-1][j-x])

If we observe closely, this is similar to the 0-1 Knapsack Problem with some added complexity. To conclude, the overall time complexity would be O(N*P*K).

Solutions

 1 public static void main(String[] args) {
 2     Scanner s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
 3
 4     int testCases = s.nextInt();
 5     int caseNum = 1;
 6     Plates plates = new Plates();
 7
 8     while (caseNum <= testCases) {
 9         int n = s.nextInt();
10         int k = s.nextInt();
11         int p = s.nextInt();
12
13         int[][] values = new int[n][k];
14         for (int i = 0; i < n; i++) {
15             for (int j = 0; j < k; j++) {
16                 values[i][j] = s.nextInt();
17             }
18         }
19
20         System.out.println(String.format("Case #%d: %d", caseNum, plates.maxPlatesBeautyValue(values, p)));
21         caseNum++;
22     }
23 }
24
25 private int maxPlatesBeautyValue(int[][] values, int numberOfPlates) {
26     int n = values.length;
27     int k= values[0].length;
28     int p = numberOfPlates;
29
30
31     // could use a one dimension array
32     int[][] prefixSum = new int[n + 1][k + 1];
33     int[][] lookup = new int[n + 1][p + 1];
34
35     for (int i = 1; i <= n; i++) {
36         for (int j = 1; j <= k; j++) {
37             prefixSum[i][j] = prefixSum[i][j - 1] + values[i - 1][j - 1];
38         }
39     }
40
41     for (int i = 1; i <= n; i++) {
42         for (int j = 1; j <= p; j++) {
43             lookup[i][j] = 0;
44
45             for (int x = 0; x <= Math.min(j, k); x++) {
46                 lookup[i][j] = Math.max(lookup[i][j], prefixSum[i][x] + lookup[i - 1][j - x]);
47             }
48         }
49     }
50
51     return lookup[n][p];
52 }

DP solution

Time Complexity: O(N*P*K)

Space Complexity: O(N*max(P, K))

References

原文地址:https://www.cnblogs.com/baozitraining/p/12596326.html

时间: 2024-11-06 21:05:44

Google Kickstart 2020 Round A: Plates Solution的相关文章

2019 google kickstart round A

第一题: n个人,每个人有一个对应的技能值s,现在要从n个人中选出p个人,使得他们的技能值相同. 显然,如果存在p个人的技能值是相同的,输出0就可以了.如果不存在,就要找出p个人,对他们进行训练,治他们的技能值相同. 训练一个小时,技能值增加1,只有一个教练,也就是只能同时训练一个 人.找出最佳的p个人的方案,输出最小的训练时间. AC的代码: #include <stdio.h> #include <stdlib.h> #include <algorithm> usi

Google Kick Start 2020 Round A

Allocation 题意 N个房子出售,每个卖Ai刀,现有B刀资金,求最多买多少个. 思路 贪心,排序后从小到大买 代码 #include<bits/stdc++.h> using namespace std; const int MAX=1e5+5; int a[MAX]; int main() { int T,cas=0; scanf("%d",&T); while(T--) { int n,b,res=0; scanf("%d%d",&a

Google Code Jam Round 1A 2015 Problem B. Haircut 二分

Problem You are waiting in a long line to get a haircut at a trendy barber shop. The shop has B barbers on duty, and they are numbered 1 through B. It always takes the kth barber exactly Mk minutes to cut a customer's hair, and a barber can only cut

google kcikstart 2018 round c

https://code.google.com/codejam/contest/4384486/dashboard#s=p0 A 题意 给定一个无向图,其中存在着唯一的一个环,求每个点到这个环的最短距离. 数据范围 ≤ T ≤ 100. 1 ≤ xi ≤ N, for all i. 1 ≤ yi ≤ N, for all i. xi ≠ yi, for all i. (xi, yi) ≠ (xj, yj), for all i ≠ j. The graph in which planets ar

Codeforces Round 615 div3 Solution

Problem A. Collecting Coins Solution Observe that the easiest solution would be increase every one's number of coins to \(\max(A,B,C)\) Then all we have to do is to distribute the coins left evenly to three of them which is typically just checking if

google code jam Round 1A 2015 Problem C. Logging

Problem A certain forest consists of N trees, each of which is inhabited by a squirrel. The boundary of the forest is the convex polygon of smallest area which contains every tree, as if a giant rubber band had been stretched around the outside of th

Google codejam Qualification Round 2015 B 巧妙枚举结果 + 贪心

背景:想了好久只想到用深搜的指数级别枚举办法来过了小数据,大数据自然超时,后来看了解题报告,才过. 思路:当前所有盘子中,煎饼个数最多的盘子里有n个煎饼,i 从 1 - n 枚举分裂之后的煎饼最多盘子里的个数,然后用贪心的方法计算要达到当前状态所需的最少分裂步数 k ,最后用时就是 i + k ,求出所有用时中最小的即可 感悟:这个题的精华之处是所有最终状态最多只有1000种,对于每种最终状态所需要的的步数又可以通过高效的贪心方法求出,思维很巧妙,算是暴力枚举和贪心的巧妙结合. 我的代码: #i

【二分答案】Google Code Jam Round 1A 2018

题意:有R个机器人,去买B件商品,有C个收银员,每个收银员有能处理的商品数量上限mi,处理单件商品所需的时间si,以及最后的装袋时间pi. 每个收银员最多只能对应一个机器人,每个机器人也最多只能对应一个收银员. 让你给每个机器人安排他购买的商品数,以及对应哪个机器人,问你最少需要多长时间才能买回所有商品. 二分答案lim,将收银员按照min(m[i],(lim-p[i])/s[i])(此即为该收银员在当前限制下能处理的商品数)从大到小排序,贪心加入,看是否满足即可. #include<cstdi

美国政府关于Google公司2013年度的财务报表红头文件

请管理员移至新闻版块,谢谢! 来源:http://www.sec.gov/ 财务报表下载↓ 此文仅作参考分析. 10-K 1 goog2013123110-k.htm FORM 10-K UNITED STATES SECURITIES AND EXCHANGE COMMISSION Washington, D.C. 20549     FORM 10-K (Mark One)       ý ANNUAL REPORT PURSUANT TO SECTION 13 OR 15(d) OF TH