codechef Johnny and the Beanstalk 题解

One evening Johnny found some funny looking beens in his grandfather‘s garden shed, and decided to plant one of them. Next morning, to his surprise he found an enormous beanstalk growing in his back yard. Undaunted by its size, he decided to count its leaves.

You must know that beanstalks in Byteland grow in a very special way. At the lowest (1st) level, there is exactly one stem. At any level(including the 1st), a stem can end (forming exactly one leaf), or branch into exactly two stems which grow into the next
level, following the same rules.

Johnny believes he has managed to count the number of leaves at each of the levels of the beanstalk. However, you must know that before he began to count, Johnny ate one or two of the other beans he found in his grandfather‘s shed, and that‘s why he is not
quite sure of his results. Please verify whether Johnny‘s results may possibly be correct, at least in theory.

Input

The input starts with a line containing integer t, the number of test cases (1<= t <= 20). The descriptions of exactly t test cases follow.

Each test case starts with an integer k, representing the number of levels of the beanstalk (1<= k<=106). The next k non-negative space-separated integers (not greater than 106) represent the number of leaves
of the beanstalk at successive levels, starting from level 1.

Output

For each test case, output a line containing exactly one of the words ‘Yes‘ or ‘No‘, depending on whether a beanstalk having the stated leaf counts can grow in accordance with the Bytelandian rules.

Example

Input:
2
3
0 1 2
3
0 0 3

Output:
Yes
No

注意:

1 上百万的数据,小心输入输出。

2 有时候并不需要处理完所有的数组中的数据,但是必须要清除读入stdin的数据。

3 清除的时候要小心了,也许之前的‘\n‘字符已经读出了,如果继续使用‘\n‘判断,那么就会出错。我这里干脆不处理跳过行了。直接读完所有数据,使用getchar速度也很快的。

水题要注意的东西也挺多的,不知道什么时候才能轻松避开这些陷阱。

#pragma once
#include <cstdio>

class JohnnyandtheBeanstalk
{
public:
	int scanInt()
	{
		char c = getchar();
		while (c < ‘0‘ || ‘9‘ < c)
		{
			c = getchar();
		}
		int num = 0;
		while (‘0‘ <= c && c <= ‘9‘)
		{
			num = (num<<3) + (num<<1) + (c - ‘0‘);
			c = getchar();
		}
		return num;
	}

	void run()
	{
		int T = 0, k = 0, leaves = 0;
		T = scanInt();
		while (T--)
		{
			k = scanInt();
			int steam = 1;
			bool flag = true;
			for (int i = 1; i <= k; i++)
			{
				leaves = scanInt();
				if (leaves > steam)
				{
					flag = false;

				}
				steam = (steam - leaves) << 1;
			}
			if (!flag || steam != 0) puts("No");
			else puts("Yes");
		}
	}
};

int johnnyandtheBeanstalk()
{
	JohnnyandtheBeanstalk jobean;
	jobean.run();
	return 0;
}

codechef Johnny and the Beanstalk 题解

时间: 2024-10-13 22:45:22

codechef Johnny and the Beanstalk 题解的相关文章

Codechef July Challenge 2014部分题解

Dish Owner(并查集) 链接:http://www.codechef.com/JULY14/problems/DISHOWN/ 题意分析:本题主要操作就是给出0 x y时,拥有第x道菜的厨师与拥有第y道菜的厨师pk,谁拥有的所有菜的其中一道菜(不一定是x或y)的分值比较高谁就获胜,并赢得loser的所有菜.即比较的是每个人分值最高的菜,所以对于非loser的人来说,他的分值最高的菜是不变的.综合题意用并查集易解. #include <cstdio> const int Maxn=100

codechef Row and Column Operations 题解

You are given an N × N grid initially filled by zeros. Let the rows and columns of the grid be numbered from1 to N, inclusive. There are two types of operations can be applied to the grid: RowAdd R X: all numbers in the row R should be increased by X

codechef Correctness of Knight Move题解

Chef develops his own computer program for playing chess. He is at the very beginning. At first he needs to write the module that will receive moves written by the players and analyze it. The module will receive a string and it should report at first

codechef Sums in a Triangle题解

Let's consider a triangle of numbers in which a number appears in the first line, two numbers appear in the second line, three in the third line, etc. Develop a program which will compute the largest of the sums of numbers that appear on the paths st

codechef Holes in the text 题解

Chef wrote some text on a piece of paper and now he wants to know how many holes are in the text. What is a hole? If you think of the paper as the plane and a letter as a curve on the plane, then each letter divides the plane into regions. For exampl

codechef Little Elephant and Permutations题解

The Little Elephant likes permutations. This time he has a permutation A[1], A[2], ..., A[N] of numbers 1, 2, ...,N. He calls a permutation A good, if the number of its inversions is equal to the number of its local inversions. The number of inversio

CodeChef Chef and Digit Jumps 题解

原题链接:Chef and Digit Jumps 题意:原题中有链接. 题解:一道很明显的bfs题,就是跳就可以了,当然,跳的时候可以加一些优化,具体看代码 #include <queue> #include <cstdio> #include <cstring> using namespace std; #define Maxn 100000 char s[Maxn+5]; int n; int a[Maxn+5]; queue<int> q; vect

codechef The Ball And Cups题解

The Ball And Cups At the end of a busy day, The Chef and his assistants play a game together. The game is not just for fun but also used to decide who will have to clean the kitchen. The Chef is a Game Master, so his concern is how to manage the game

codechef Little Elephant and Bombs题解

The Little Elephant from the Zoo of Lviv currently is on the military mission. There are N enemy buildings placed in a row and numbered from left to right strating from 0. Each building i (except the first and the last) has exactly two adjacent build