GCJ 2015Q(Standing Ovation-贪心)

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, TT 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
levelk. For example, the string "409" would mean that there were four audience members withSi = 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

Output

4
4 11111
1 09
5 110011
0 1
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<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<iostream>
#include<cmath>
#include<cctype>
#include<ctime>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (x<<1)
#define Rson ((x<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,127,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define INF (2139062143)
#define F (100000007)
#define SMAX (1000+10)
typedef long long ll;
ll mul(ll a,ll b){return (a*b)%F;}
ll add(ll a,ll b){return (a+b)%F;}
ll sub(ll a,ll b){return (a-b+(a-b)/F*F+F)%F;}
void upd(ll &a,ll b){a=(a%F+b%F)%F;}
int T,smax;
char s[SMAX];
int main()
{
//	freopen("A-large.in","r",stdin);
//	freopen("A-large.out","w",stdout);

	cin>>T;
	For(kcase,T)
	{
		scanf("%d%s",&smax,s);
		int n=smax+1;

		int ans=0,tot=0;
		Rep(i,n)
		{
			if (tot<i) ans+=i-tot,tot=i;
			tot+=s[i]-'0';
		}

		printf("Case #%d: %d\n",kcase,ans);
	}

	return 0;
}
时间: 2024-10-08 11:08:33

GCJ 2015Q(Standing Ovation-贪心)的相关文章

GCJ 2015Q(Infinite House of Pancakes-贪心与枚举)

Problem At the Infinite House of Pancakes, there are only finitely many pancakes, but there are infinitely many diners who would be willing to eat them! When the restaurant opens for breakfast, among the infinitely many diners, exactly D have non-emp

(翻译)《Hands-on Node.js》—— 引言

今天开始会和大熊君{{bb}}一起着手翻译node的系列外文书籍,大熊负责翻译<Node.js IN ACTION>一书,而我暂时负责翻译这本<Hands-on Node.js>(下载),为方便理解不采用直译的方式,若有翻译不当的地方还请不吝赐教,我会及时堪正. 引言 在09年欧洲JSConf大会上,一位叫做Ryan Dahl的年轻程序员介绍了一项他所负责的项目.该项目结合了谷歌V8 Javascript引擎以及event loop机制,从而可以使用JS来创建服务端平台.该项目跟其

让你跟上nodejs的资源

For a long time, JavaScript developers hoped for a server-side solution that would allow them to fully and easily leverage their favorite programming language on the backend. Through the years, a number of options have been made available like Aptana

words2

餐具:coffee pot 咖啡壶coffee cup 咖啡杯paper towel 纸巾napkin 餐巾table cloth 桌布tea -pot 茶壶tea set 茶具tea tray 茶盘caddy 茶罐dish 碟plate 盘saucer 小碟子rice bowl 饭碗chopsticks 筷子soup spoon 汤匙knife 餐刀cup 杯子glass 玻璃杯mug 马克杯picnic lunch 便当fruit plate 水果盘toothpick 牙签中餐:bear's

8 分析句子结构

1.一些语法困境 普遍存在的歧义 2.文法的用途 学习文法的一个好处是,它提供了一个概念性的框架和词汇拼写这些直觉. 成分结构基于对词与其他词结合在一起形成单元的观察.一个词序列形成这样一个单元被证明是可替代的--也就是说, 在一个符合语法规则的句子中的词序列可以被一个更小的序列替代而不会导致句子不符合语法规则. 3.上下文无关文法 一种简单的文法 groucho_grammer = nltk.CFG.fromstring(""" s -> NP VP VP ->

Using APIs in Your Ethereum Smart Contract with Oraclize

Homepage Coinmonks HOMEFILTER ▼BLOCKCHAIN TUTORIALSCRYPTO ECONOMYTOP READSCONTRIBUTEFORUM & JOBS Go to the profile of Matt Solomon Matt Solomon Apr 20 Getting data from an outside source onto the blockchain is a non-trivial problem, and one solution

GCJ 2009 Round2 A 构造贪心策略

题意: 给你一个n?n的元素只为0或1的矩阵,要把该矩阵通过行交换变为上三角矩阵,且交换方式只能为相邻行交换.问最小的交换步数是多少? 思路: 首先这个行的利用价值就是最右边的1位置是多少,直接把行抽象为最右边1的数字即可.注意:这里WA了一发,没有注意数字要初始化为0.无论多么简单,写完之后检查代码且保证逻辑没有一丝错误十分重要! 这个题主要的是构造贪心的法则:题中只需要满足第一行的数字小于1,第二行的数字小于2,-..,第n行的数字小于n.那么就从第一行开始,一行一行的来看,注意若当前行的数

GCJ 2008 Round 1A A 排序贪心

题意:给你两个维数相同的向量,它们之中的元素可以任意交换位置,求它们内积的最大值. 思路:乍一看此题摸不着什么头脑,只是凭借直觉感觉一个升序一个降序求内积即可.这样的感觉有时是对的,有时是错的,如果实现复杂的话比赛中就不该冒这个险.最好简单的证明一下. 证明:先讨论二维向量的情况对于按升序排列的(x1,x2)按升序排列的(y1,y2),显然恰与思路想法相反,其内积减去思路想法的内积有: x1y1+x2y2?x1y2?x2y1=x1(y1?y2)+x2(y2?y1) 显然y1?y2≤0又x1?x2

HDU 4004 The Frog&#39;s Games(基本算法-贪心,搜索-二分)

The Frog's Games Problem Description The annual Games in frogs' kingdom started again. The most famous game is the Ironfrog Triathlon. One test in the Ironfrog Triathlon is jumping. This project requires the frog athletes to jump over the river. The