[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). 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.
第一题是很简单的一题,只要算一下当前观众的害羞值与以及站起来的观众之差,如果站起来的观众总人数小于他的害羞值,则增加一定数量的朋友加进来站,让他可以也站起来。
最后输出站起来的朋友的总数量即可。
#include<fstream>
#include<string>
using namespace std;
int main(){
ifstream in("b.in");
ofstream out("b.out");
int T;
in >> T;
for (int i = 0; i < T; i++){
int N;
in >> N;
string line;
in >> line;
//out << "N=" << N<<endl;
//out << "line=" << line << endl;
int *shyness = new int[N+1]();
int sum = 0; //number of audience who have standed up
int friends_num = 0;
for (int j = 0; j <=N; j++){
shyness[j] = line[j]-48;
//out << shyness[j] << endl;
int need = j - sum;
if (need > 0&­ness[j]>0){
friends_num += need;
sum += need;
}
sum += shyness[j];
}
out << "Case #" << i + 1 << ": " << friends_num << endl;
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
[C++]Standing Ovation——Google Code Jam 2015 Qualification Round的更多相关文章
- [C++]Infinite House of Pancakes——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) ...
- 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 ...
- Google Code Jam 2009 Qualification Round Problem B. Watersheds
https://code.google.com/codejam/contest/90101/dashboard#s=p1 Problem Geologists sometimes divide an ...
- 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 ...
- Google Code Jam 2015 R1C B
题意:给出一个键盘,按键都是大写字母.给出一个目标单词和一个长度L.最大值或者最大长度都是100.现在随机按键盘,每个按键的概率相同. 敲击出一个长度为L的序列.求该序列中目标单词最多可能出现几次,期 ...
- Google Code Jam 2015 R2 C
题意:给出若干个句子,每个句子包含多个单词.确定第一句是英文,第二句是法文.后面的句子两者都有可能.两个语种会有重复单词. 现在要找出一种分配方法(给每个句子指定其文种),使得既是英文也是法文的单词数 ...
- Google Code Jam 2015 Round1A 题解
快一年没有做题了, 今天跟了一下 GCJ Round 1A的题目, 感觉难度偏简单了, 很快搞定了第一题, 第二题二分稍微考了一下, 还剩下一个多小时, 没仔细想第三题, 以为 前两个题目差不多可以晋 ...
- Google Code Jam 2014 Qualification 题解
拿下 ABD, 顺利晋级, 预赛的时候C没有仔细想,推荐C题,一个非常不错的构造题目! A Magic Trick 简单的题目来取得集合的交并 1: #include <iostream> ...
- [C++]Store Credit——Google Code Jam Qualification Round Africa 2010
Google Code Jam Qualification Round Africa 2010 的第一题,很简单. Problem You receive a credit C at a local ...
随机推荐
- SQL游标+递归查询客户子客户转换率
ALTER PROCEDURE [dbo].[Account3YearsConversion ] as DECLARE @AccountId UNIQUEIDENTIFIER , @yearbefor ...
- VB.Net常用数学函数整理
System.Math 类中定义了用于数学计算的函数.Math 类包括三角函数.对数函数和其他常用数学函数.下列函数是在 System 名称空间的 Math 类中定义的函数. 注意:要使用这些函数 ...
- 终于解决“Git Windows客户端保存用户名与密码”的问题
这就是正确答案,我们已经验证过了,下面详细描述一下解决方法: 1. 在Windows中添加一个HOME环境变量,值为%USERPROFILE%,如下图: 2. 在“开始>运行”中打开%Home% ...
- codeforces 8C. Looking for Order 状压dp
题目链接 给n个物品的坐标, 和一个包裹的位置, 包裹不能移动. 每次最多可以拿两个物品, 然后将它们放到包里, 求将所有物品放到包里所需走的最小路程. 直接状压dp就好了. #include < ...
- 【Chromium中文文档】Chromium如何展示网页
Chromium如何展示网页 转载请注明出处:https://ahangchen.gitbooks.io/chromium_doc_zh/content/zh//Start_Here_Backgrou ...
- DRAM与NAND Flash产业六大趋势预测分析
集邦科技(TrendForce)旗下的分析部门DRAMeXchange的研究,针对对DRAM与NANDFlash产业的长久观察下,提出了对2012-2015年间产业发展的六大趋势预测: 趋势一 ...
- openstack 采用conductor的原因
供参考. Tan0同学给我的解释: 两个原因 一个是为了isolation做准备 因为升级主要就是升DB的schema 如果让compute直接读写DB,那每次升级都得升compute 现在隔离开之后 ...
- MySql数据库连接池
1.传统链接(如下为示意图) 注意: (1).传统方式找DriverManager要连接,数目是有限的. (2).传统方式的close(),并没有将Connection重用,只是切断应用程序和数据库的 ...
- 多个target下编译的时候出错问题的解决
在工程里如果有多个target的时候,如图 那么编译的时候一定要注意Xcode右侧勾选了正确的target,否则有可能会导致一系列让你想不到的bug. ,另外,如果工程中有framework,那么一定 ...
- 黑马程序员_<<String类>>
--------------------ASP.Net+Android+IOS开发..Net培训.期待与您交流! -------------------- 1. String类 1.概述 String ...