【2020.1.16】凉肝的比赛题解

A - Mezo Playing Zoma

Today, Mezo is playing a game. Zoma, a character in that game, is initially at position x=0. Mezo starts sending n commands to Zoma. There are two possible commands:

‘L’ (Left) sets the position x:=x−1;
‘R’ (Right) sets the position x:=x+1.
Unfortunately, Mezo’s controller malfunctions sometimes. Some commands are sent successfully and some are ignored. If the command is ignored then the position x doesn’t change and Mezo simply proceeds to the next command.

For example, if Mezo sends commands “LRLR”, then here are some possible outcomes (underlined commands are sent successfully):

“LRLR” — Zoma moves to the left, to the right, to the left again and to the right for the final time, ending up at position 0;
“LRLR” — Zoma recieves no commands, doesn’t move at all and ends up at position 0 as well;
“LRLR” — Zoma moves to the left, then to the left again and ends up in position −2.
Mezo doesn’t know which commands will be sent successfully beforehand. Thus, he wants to know how many different positions may Zoma end up at.

Input

The first line contains n (1≤n≤105) — the number of commands Mezo
sends.

The second line contains a string s of n commands, each either ‘L’
(Left) or ‘R’ (Right).

Output

Print one integer — the number of different positions Zoma may end up
at.

Example
Input

4
LRLR

Output

5

Note

In the example, Zoma may end up anywhere between −2 and 2.

#include <stdio.h>
int main()
{
	char a[100050];//尽可能把范围定的大一些。
	int left=0,right=0,result,i,n;
	scanf("%d",&n);
	scanf("%s",a);
	for(i=0;i<n;i++)
	{
		if(a[i]=='L')
		left++;
		if(a[i]=='R')
		right++;
	}
	result=right+left+1;
	printf("%d\n",result);
	return 0;
}

E - Deadline

Adilbek was assigned to a special project. For Adilbek it means that he has n days to run a special program and provide its results. But there is a problem: the program needs to run for d days to calculate the results.

Fortunately, Adilbek can optimize the program. If he spends x (x is a non-negative integer) days optimizing the program, he will make the program run in ⌈d/(x+1)⌉ days (⌈a⌉ is the ceiling function: ⌈2.4⌉=3, ⌈2⌉=2). The program cannot be run and optimized simultaneously, so the total number of days he will spend is equal to x+⌈d/(x+1)⌉.

Will Adilbek be able to provide the generated results in no more than n days?

Input

The first line contains a single integer T (1≤T≤50) — the number of
test cases.

The next T lines contain test cases – one per line. Each line contains
two integers n and d (1≤n≤109, 1≤d≤109) — the number of days before
the deadline and the number of days the program runs.

Output

Print T answers — one per test case. For each test case print YES
(case insensitive) if Adilbek can fit in n days or NO (case
insensitive) otherwise.

Example
Input

3

1 1

4 5

5 11

Output

YES
YES
NO

Note

In the first test case, Adilbek decides not to optimize the program at
all, since d≤n.
In the second test case, Adilbek can spend 1 day optimizing the
program and it will run ⌈5/2⌉=3 days. In total, he will spend 4 days
and will fit in the limit.

In the third test case, it’s impossible to fit in the limit. For
example, if Adilbek will optimize the program 2 days, it’ll still work
⌈11/(2+1)⌉=4 days.

题目意思:对于每组测试的数据,输入两个数,第一个n为n天要求完成任务,第二个数据d是实际要完成的天数,问你如何根据题中给的公式,策划出最佳优化方案。
解题思路:如果n>=d,则直接完成即可,无需优化。如果n<=d,则需要优化,根据公式,可以从1天开始到n,如果一旦有符合要求的(即天数小于等于要求的n),满足条件。

#include <stdio.h>
int function(long long int d,long long int i)//d,i数较大,用long long
{
	int result;
	double temp;
	temp=d*1.0/(i+1);
	int tem;tem=temp;
	if((temp*1.0)>(tem*1.0))
	result=tem+1+i;
	else
	result=temp+i;
	return result;
}
int main()
{
	int count=0,i,x;//用一个标志来判断,即count,此用法很普及。
	long long int n,d;
	scanf("%d",&x);
	while(x--)
	{
		count=0;
		scanf("%lld%lld",&n,&d);
		if(n>=d)
		count=1;
		else
		{
			for(i=1;i<n;i++)
		{
		        if(function(d,i)<=n)
				{
					count=1;
				    break;
				}
		}
		}
		if(count)
		printf("YES\n");
		else
		printf("NO\n");
	}
	return 0;
}

G - HQ9+

HQ9+ is a joke programming language which has only four one-character instructions:

“H” prints “Hello, World!”,
“Q” prints the source code of the program itself,
“9” prints the lyrics of “99 Bottles of Beer” song,
“+” increments the value stored in the internal accumulator.
Instructions “H” and “Q” are case-sensitive and must be uppercase. The characters of the program which are not instructions are ignored.

You are given a program written in HQ9+. You have to figure out whether executing this program will produce any output.

Input

The input will consist of a single line p which will give a program in
HQ9+. String p will contain between 1 and 100 characters, inclusive.
ASCII-code of each character of p will be between 33 (exclamation
mark) and 126 (tilde), inclusive.

Output

Output “YES”, if executing the program will produce any output, and
“NO” otherwise.

Examples
Input

Hi!

Output

YES

Input

Codeforces

Output

NO

Note

In the first case the program contains only one instruction — “H”,
which prints “Hello, World!”.

In the second case none of the program characters are language
instructions.

#include <stdio.h>
#include <string.h>
int main()
{
	char s[101];
	int len,i,count=0;
	gets(s);
	len=strlen(s);
	for(i=0;i<len;i++)
	if(s[i]=='H'||s[i]=='Q'||s[i]=='9')
	{
		count=1;
	    break;
	}
	if(count==1)
	printf("YES\n");
	else
	printf("NO\n");
	return 0;
}
发布了4 篇原创文章 · 获赞 0 · 访问量 123

猜你喜欢

转载自blog.csdn.net/Legend_666/article/details/104000548
今日推荐