Problem

It’s opening night at the opera, and your friend is the prima donna (the lead female singer). You will not be in the audience, but you want to make sure she receives a standing ovation – with every audience member standing up and clapping their hands for her.

Initially, the entire audience is seated. Everyone in the audience has a shyness level. An audience member with shyness level Si will wait until at least Si other audience members have already stood up to clap, and if so, she will immediately stand up and clap. If Si = 0, then the audience member will always stand up and clap immediately, regardless of what anyone else does. For example, an audience member with Si = 2 will be seated at the beginning, but will stand up to clap later after she sees at least two other people standing and clapping.

You know the shyness level of everyone in the audience, and you are prepared to invite additional friends of the prima donna to be in the audience to ensure that everyone in the crowd stands up and claps in the end. Each of these friends may have any shyness value that you wish, not necessarily the same. What is the minimum number of friends that you need to invite to guarantee a standing ovation?

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each consists of one line with Smax, the maximum shyness level of the shyest person in the audience, followed by a string of Smax + 1 single digits. The kth digit of this string (counting starting from 0) represents how many people in the audience have shyness level k. For example, the string “409” would mean that there were four audience members with Si = 0 and nine audience members with Si = 2 (and none with Si = 1 or any other value). Note that there will initially always be between 0 and 9 people with each shyness level.

The string will never end in a 0. Note that this implies that there will always be at least one person in the audience.

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 minimum number of friends you must invite.

Limits

1 ≤ T ≤ 100.
Small dataset
0 ≤ Smax ≤ 6.

Large dataset
0 ≤ Smax ≤ 1000.

Sample

Input
4
4 11111
1 09
5 110011
0 1 Output
Case #1: 0
Case #2: 1
Case #3: 2
Case #4: 0

In Case #1, the audience will eventually produce a standing ovation on its own, without you needing to add anyone – first the audience member with Si = 0 will stand up, then the audience member with Si = 1 will stand up, etc.

In Case #2, a friend with Si = 0 must be invited, but that is enough to get the entire audience to stand up.

In Case #3, one optimal solution is to add two audience members with Si = 2.

In Case #4, there is only one audience member and he will stand up immediately. No friends need to be invited.

开始我花了很多时间在尝试自己所认为的最优分割方案,到最后才发现其实暴力的算法也是很好的。

所要花费的时间最多为一个盘子中最多的pancake数量,最小时间>=1,因此只要把时间 i 从1~最大值的情况计算一遍,即先把所有盘子里的pancake都分一下,直到每个盘子里pancake数量都小于等于i,记录下这样要花费的时间。

最后输出最短时间。

#include<fstream>
#include<vector>
#include<algorithm> using namespace std; int main(){ ifstream in("b.in");
ofstream out("b.out");
int T;
in >> T;
for (int t = 0; t < T; t++){
int N;
in >> N;
vector<int> num(N);
for (int j = 0; j < N; j++){
in >> num[j];
}
//从大到小排序
sort(num.begin(), num.end(), isgreater<int, int>);
int max = num[0]; //最大值
int min = max; //记录最短用时
int sum = 0;
//从1到最大值遍历
for (int i = 1; i <= max; i++) {
sum = i;
for (int j = 0; j < N; j++) {
if (num[j] > i) {
//对pancake数大于i的分出,
//使其数量<=i,
//对sum加上分pancake的次数。
if (num[j] % i == 0)
sum += (num[j] / i - 1);
else
sum += (num[j] / i);
}
}
if (sum < min)
min = sum;
}
out << "Case #" << t + 1 << ": " << min << endl;
}
in.close();
out.close();
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

[C++]Infinite House of Pancakes——Google Code Jam 2015 Qualification Round的更多相关文章

  1. [C++]Standing Ovation——Google Code Jam 2015 Qualification Round

    Problem It’s opening night at the opera, and your friend is the prima donna (the lead female singer) ...

  2. Google Code Jam 2009 Qualification Round Problem C. Welcome to Code Jam

    本题的 Large dataset 本人尚未解决. https://code.google.com/codejam/contest/90101/dashboard#s=p2 Problem So yo ...

  3. Google Code Jam 2009 Qualification Round Problem B. Watersheds

    https://code.google.com/codejam/contest/90101/dashboard#s=p1 Problem Geologists sometimes divide an ...

  4. Google Code Jam 2009 Qualification Round Problem A. Alien Language

    https://code.google.com/codejam/contest/90101/dashboard#s=p0 Problem After years of study, scientist ...

  5. Google Code Jam 2015 R1C B

    题意:给出一个键盘,按键都是大写字母.给出一个目标单词和一个长度L.最大值或者最大长度都是100.现在随机按键盘,每个按键的概率相同. 敲击出一个长度为L的序列.求该序列中目标单词最多可能出现几次,期 ...

  6. Google Code Jam 2015 R2 C

    题意:给出若干个句子,每个句子包含多个单词.确定第一句是英文,第二句是法文.后面的句子两者都有可能.两个语种会有重复单词. 现在要找出一种分配方法(给每个句子指定其文种),使得既是英文也是法文的单词数 ...

  7. Google Code Jam 2015 Round1A 题解

    快一年没有做题了, 今天跟了一下 GCJ Round 1A的题目, 感觉难度偏简单了, 很快搞定了第一题, 第二题二分稍微考了一下, 还剩下一个多小时, 没仔细想第三题, 以为 前两个题目差不多可以晋 ...

  8. Google Code Jam 2014 Qualification 题解

    拿下 ABD, 顺利晋级, 预赛的时候C没有仔细想,推荐C题,一个非常不错的构造题目! A Magic Trick 简单的题目来取得集合的交并 1: #include <iostream> ...

  9. [Google Code Jam (Qualification Round 2014) ] B. Cookie Clicker Alpha

    Problem B. Cookie Clicker Alpha   Introduction Cookie Clicker is a Javascript game by Orteil, where ...

随机推荐

  1. MS SQLSERVER通用存储过程分页

    最近在面试的时候,遇到个奇葩的秃顶老头面试官. 问:写过存储过程分页吗? 答:没写过,但是我知道分页存储的原理,我自己也写过,只是在工作中没写过. 问:那你这么多年工作中就没写过吗? 答:的确没写过, ...

  2. Java核心技术卷1Chapter7笔记 图形程序设计

    Swing是指被绘制的用户界面类,AWT是指像事件处理这样的窗口工具箱的底层机制. SWT,JavaFX是可能的代替技术. 创建框架 在Java中,顶层窗口(就是没有包含在其他窗口中的窗口)被称为框架 ...

  3. Android Intent实现页面跳转

      Intent可以来协助完成Android各个组件之间的通信   1:startActivity(intent);     //直接启动                /*              ...

  4. 使用JQuery.lettering.js实现多行文本样式自定义

    前几天一位在广告公司的朋友发来求助,说:“有一个项目要求实现对字符串进行动态拆分,然后对拆分出的字符分别使用不同的样式效果...”,听到这个需求,我内心有点不屑,这有何能,最多五分钟搞定啊~~ 于是我 ...

  5. Android 数据库ORM框架GreenDao学习心得及使用总结<一>

    转: http://www.it165.net/pro/html/201401/9026.html 最近在对开发项目的性能进行优化.由于项目里涉及了大量的缓存处理和数据库运用,需要对数据库进行频繁的读 ...

  6. php实现两分法查找

    两分法查找的前提:顺序方式存储,而且必须是排好序 直接上代码: function search($array, $target, $low = 0, $high = 0){ $len = count( ...

  7. codeforces 659E . New Reform 强连通

    题目链接 对于每一个联通块, 如果有一个强连通分量, 那么这个联通块对答案的贡献就是0. 否则对答案贡献是1. #include <iostream> #include <vecto ...

  8. PowerShell入门(一):PowerShell能干什么?

    原文链接:http://www.cnblogs.com/ceachy/archive/2013/01/30/WhatCanPowerShellDo.html PowerShell能干什么呢?就像序言中 ...

  9. zookeeper 学习笔记 (C语言版本)

    1.zookeeper简介 zookeeper是Hadoop的子项目,在大型分布式系统中,zookeeper封装好了一些复杂易出错的服务,提供简单易用的接口,给使用者提供高效稳定的服务.这些服务包括配 ...

  10. SQL Server 统计信息对查询的影响

    优化器根据开消确定选择哪个执行计划,开消又与行数统计信息有关,默认情况下统计信息是在优化的过程中自动生成的. 一旦列被标记为需要统计信息,查询优化器就会查找该列以有的统计信息,如果以有一个统计信息,下 ...