Google Code Jam 2010 Qualification Round – Problem C Theme Park
Roller coasters are so much fun! It seems like everybody who visits the theme park wants to ride the roller coaster. Some people go alone; other people go in groups, and don’t want to board the roller coaster unless they can all go together. And everyone who rides the roller coaster wants to ride again. A ride costs 1 Euro per person; your job is to figure out how much money the roller coaster will make today.
The roller coaster can hold k people at once. People queue for it in groups. Groups board the roller coaster, one at a time, until there are no more groups left or there is no room for the next group; then the roller coaster goes, whether it’s full or not. Once the ride is over, all of its passengers re-queue in the same order. The roller coaster will run R times in a day.
For example, suppose R=4, k=6, and there are four groups of people with sizes: 1, 4, 2, 1. The first time the roller coaster goes, the first two groups [1, 4] will ride, leaving an empty seat (the group of 2 won’t fit, and the group of 1 can’t go ahead of them). Then they’ll go to the back of the queue, which now looks like 2, 1, 1, 4. The second time, the coaster will hold 4 people: [2, 1, 1]. Now the queue looks like 4, 2, 1, 1. The third time, it will hold 6 people: [4, 2]. Now the queue looks like [1, 1, 4, 2]. Finally, it will hold 6 people: [1, 1, 4]. The roller coaster has made a total of 21 Euros!
Input
The first line of the input gives the number of test cases, T. T test cases follow, with each test case consisting of two lines. The first line contains three space-separated integers: R, k and N. The second line contains N space-separated integers gi, each of which is the size of a group that wants to ride. g0 is the size of the first group, g1 is the size of the second group, etc.
Output
For each test case, output one line containing “Case #x: y”, where x is the case number (starting from 1) and y is the number of Euros made by the roller coaster.
Limits
1 ≤ T ≤ 50.
gi ≤ k.
Small dataset
1 ≤ R ≤ 1000.
1 ≤ k ≤ 100.
1 ≤ N ≤ 10.
1 ≤ gi ≤ 10.
Large dataset
1 ≤ R ≤ 108.
1 ≤ k ≤ 109.
1 ≤ N ≤ 1000.
1 ≤ gi ≤ 107.
Sample
Input
Output
3
4 6 4
1 4 2 1
100 10 1
1
5 5 10
2 4 2 3 4 2 1 2 1 3
Case #1: 21
Case #2: 100
Case #3: 20
题目很简单,就是有很多团队在排队,一个过山车一次最多坐K个人,过山车一天运行R次,一人坐一次1欧元,问过山车一天可以赚多少钱。小规模数据模拟就可以过,但是大数据绝对超时,大数据需要在一遍算一遍记录结果,记录当前团队开始,能做多少人,后面从哪个团队开始。代码如下:
#include <stdio.h> struct g_table { int gi; unsigned long total; int next; }; g_table group[1000]; void main() { FILE *fp1,*fp2; fp1 = fopen("c.in.txt", "r"); fp2 = fopen("c.out.txt", "w+"); int t; fscanf(fp1, "%d", &t); for (int i=1; i<=t; i++) { unsigned long r,k,n; fscanf(fp1, "%lu %lu %lu", &r, &k, &n); for (unsigned long j=0; j<n; j++) { unsigned long gi; fscanf(fp1, "%d", &gi); group[j].gi = gi; group[j].total = 0; group[j].next = -1; } long long total = 0; int pos = 0; for (unsigned long j=0; j<r; j++) { if (group[pos].next != -1 && group[pos].total != 0) { // 缓存 total += group[pos].total; pos = group[pos].next; } else { // 计算 int last_pos = pos; int count = 0; unsigned max_num = 0; while (count < n && max_num + group[pos].gi <= k) { max_num += group[pos].gi; count++; pos = (pos+1) % n; } group[last_pos].next = pos; group[last_pos].total = max_num; total += group[last_pos].total; } } fprintf(fp2, "Case #%d: %lld\n", i, total); } fclose(fp1); fclose(fp2); }
近期评论