2022.1.23(测试题解)

目录

P7938 [A] Beautiful Array

题目描述

输入格式

输出格式

题意翻译

输入输出样例

说明/提示

Explanation

Constraints

思路

代码展示 

CF922A Cloning Toys

题目描述

输入格式

输出格式

题意翻译

输入输出样例

思路

代码展示 


今天早上复习了这周学过的内容,下午测试ac了两个题目

P7938 [A] Beautiful Array

题目描述

In this problem, we define a sequence of ( and ) as a "bracket sequence".

The definition of Regular Bracket Sequence is as follows:

  1. () is a Regular Bracket Sequence.
  2. If A is a Regular Bracket Sequence, then (A) is also a Regular Bracket Sequence.
  3. If A and B are Regular Bracket Sequences, then AB is also a Regular Bracket Sequence.

For example: ()(()), and ()() are all Regular Bracket Sequences, but )(()( are not.

In particular, an empty sequence is not a Regular Bracket Sequence sequence in this problem.

Now cute Ran gives you a bracket sequence ss of length nn. She wants you to construct 2\cdot m2⋅m strictly increasing arrays. Let us denote them as p_1,p_2,\cdots,p_{2 m}p1​,p2​,⋯,p2m​ (you can leave any of them empty). You need to ensure that all integers between 1\sim n1∼n appear exactly once in these arrays.

An array p_i=\{r_1,r_2,\cdots,r_k\}pi​={r1​,r2​,⋯,rk​} is Beautiful if \{s_{r_1},s_{r_2},\cdots,s_{r_k}\}{sr1​​,sr2​​,⋯,srk​​} is a Regular Bracket Sequence.

Ran wonders whether it is possible to construct these arrays so that at least mm of the 2\cdot m2⋅m arrays are "beautiful arrays".

输入格式

Each test contains multiple test cases.

The first line contains an integer TT, the number of test cases.

For each test case, the first line contains two integers nn and mm, and the second line contains a bracket sequence ss.

输出格式

For each test case, print one line.

If it is possible to construct these arrays, print 11. Otherwise print 00.

题意翻译

定义一个字符串为括号串当且仅当其仅由 ( 和 ) 组成。

试将一个长度为 nn 的括号串分为 2m2m 个子序列,子序列可以为空,且每个字符都必须分到恰好一个子序列中,使得至少 mm 个子序列为匹配的括号串。空序列不算匹配的括号序列。无解请输出 00,否则输出 11。本题多组数据,其中数据组数为 TT。

定义 AA 为 BB 的子序列当且仅当 AA 能由 BB 在顺序不改变的前提下删除若干元素后得到。

*样例 11 解释:你可以将第一个和第二个字符分入第一个子序列,让第二个子序列为空子序列。此时第一个子序列为 (),第二个为空,总计有一个匹配的括号序列,满足要求。

输入输出样例

输入 #1复制

2
2 1
()
2 99
()

输出 #1复制

1
0

说明/提示

Explanation

For the first test case, we can construct p_1=\{1,2\}p1​={1,2} and p_2=\{\}p2​={}. So p_1p1​ is a "beautiful array".

For the second test case, it is obvious that we cannot use two numbers to construct 9999 beautiful arrays.

Constraints

1\le T,n,m\le 2001≤T,n,m≤200.

思路

1、输入n,m----判断n个字符中是不是可以至少组成 m 对括号

2、用t1计数左括号,t2计数右括号,如果字符是左括号t1++;如果是右括号t2++,并且如果t1>0,t1--,t2--,cnt++;

3、假如cnt>m即满足题意则输出1;反之输出0;

代码展示 

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t,n,m;
	char ch;
	scanf("%d",&t);
	while(t--)
	{
		int t1=0,t2=0,cnt=0;
		cin>>n>>m;
		getchar();
		while(n--)
		{
			ch=getchar();
			if(ch=='(')
			{
				t1++; 
			}	
			else
			{
				t2++;
				if(t1>0){
					t1--;t2--;cnt++;
				}
			}	
		}
		if(cnt>=m)
			cout<<1<<endl;
		else
			cout<<0<<endl;
	}
	return 0;
}

CF922A Cloning Toys

题目描述

Imp likes his plush toy a lot.

Recently, he found a machine that can clone plush toys. Imp knows that if he applies the machine to an original toy, he additionally gets one more original toy and one copy, and if he applies the machine to a copied toy, he gets two additional copies.

Initially, Imp has only one original toy. He wants to know if it is possible to use machine to get exactly xx copied toys and yy original toys? He can't throw toys away, and he can't apply the machine to a copy if he doesn't currently have any copies.

输入格式

The only line contains two integers xx and yy ( 0<=x,y<=10^{9}0<=x,y<=109 ) — the number of copies and the number of original toys Imp wants to get (including the initial one).

输出格式

Print "Yes", if the desired configuration is possible, and "No" otherwise.

You can print each letter in arbitrary case (upper or lower).

题意翻译

题目描述

Imp非常喜欢他的毛绒玩具。

最近,他发现了一个可以克隆毛绒玩具的机器。Imp知道如果他将一个玩具本体进行克隆,他将会得到两个本体(新增一个)和一个克隆体;而如果将一个克隆体进行克隆,他将会得到三个克隆体(新增两个)。

一开始,Imp只有一个毛绒玩具本体。他想要知道他能否使用这个机器得到恰好xx 个克隆体和yy 个本体。他不能把玩具扔掉,也不能在没有克隆体的时候对一个克隆体进行克隆。 输入格式

一行两个整数x,y(0 \le x,y \le 10^9)x,y(0≤x,y≤109) ,分别表示Imp想要得到的玩具克隆体数量和本体数量(包括一开始的一个本体)。 输出格式

如果能够满足题意,输出"Yes",否则输出"No"。大小写不敏感。 说明

在样例一中,Imp可以对本体进行两次克隆,再对克隆体进行两次克隆。 翻译贡献者:浮尘ii

输入输出样例

输入 #1复制

6 3

输出 #1复制

Yes

输入 #2复制

4 2

输出 #2复制

No

输入 #3复制

1000 1001

输出 #3复制

Yes

思路

1、因为

方法一:将本体进行克隆,他将会得到两个本体(新增一个)和一个克隆体

方法二:将克隆体进行克隆,他将会得到三个克隆体(新增两个)。

2、需求是x个克隆体,y个本体

3、所以一般满足(x-(y-1))%2==0就说明可以,因为本体本来就有一个(所以y-1),这个式子就代表用方法一得到y个主体后(同时也得到了y个x),剩下的x是不是偶数;

是偶数:满足题意输出yes

4、但是要排除特殊情况

①x==0&&y==1---即不用克隆

②x==0但是y不等于1---一旦用本体克隆必然会得到克隆体

③y==1但是x不等于0---没有克隆体让他克隆

④y==0---不可能得到,因为不能扔,而他本来就有一个本体

⑤y>1+x----想得到的本体大于克隆体加一,但是要想得到本体同时就会得到一个克隆体

代码展示 

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int x,y,flag=0;
	cin>>x>>y;
	if (x==0&&y==1)
        cout<<"Yes";
    else if (x==0||y==0||y==1||y>1+x)
        cout<<"No";
    else if ((x-(y-1))%2==0)
        cout<<"Yes";
    else
        cout<<"No";
	return 0;	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值