寒假总结补充2 2-5

[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.

题目大意:给出一组仅由括号组成的字符串,长度为n,要求构成m个或更多个元素组成的长度严格递减(递增)的括号串,每个串必须符合串的要求 ( 可以有空串 )

例如:给出的是

18    5

(())()(()((()()()(

其可以构成8个串,其构成严格递减,数量大于5,符合要求

(())()()()()()

(())()()()()

(())()()()

(())()()

(())()

(())

()

如果m大于8,则该样例就不成立了

所以在输入的时候利用栈的思想,输入时找出所有能够匹配的括号对,最后判断括号对的数量是否有m-1个以上

代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<malloc.h>
int main(){
	int t;
	scanf("%d",&t);
	int n,m;char x[210];
	for(int o=0;o<t;o++){
		scanf("%d%d",&n,&m);getchar();
		int ip=0,end=0;char ok;
		for(int j=0;j<n;j++){
			scanf("%c",&ok);
			if(ok=='(') ip++;
			else if(ok==')'&&ip>0) {
				ip--;end++;
			} 
		}
		if(end>=m) printf("1\n");
		else printf("0\n");
	}
	return 0;
}

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

说明/提示

In the first example, Imp has to apply the machine twice to original toys and then twice to copies.

先特判,没有玩具本体或者只有一个本体而克隆体大于一个时情况不存在

再公式计算,如果除去第一个玩具本体,克隆体的数量一定大于等于本体,且一个本体可以产生一个克隆体,一个克隆体可以产生两个克隆体,那么克隆体多出的部分都是自己复制自己出来的,所以:克隆体-克隆出的本体=2的倍数,根据情况输出。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<malloc.h>

int main()
{
	int x,y;
	scanf("%d%d",&x,&y);
	if(y==0||y==1&&x>0) printf("No");
	else if((x-y+1)%2!=0||(x-y+1)<0) printf("No");
	else printf("Yes");
	return 0;
}

 [B1] Alice Wins! (easy version) - 洛谷

题目描述

The difference between the versions is the limit of operations.

Alice is a cute girl who has a lot of dolls.

There are 4\cdot n4⋅n dolls playing rock-paper-scissors. They are divided into two teams: Team A and Team B. Each team contains 2\cdot n2⋅n dolls.

A total of 2\cdot n2⋅n rounds of the game will be played. In the ii-th round, the ii-th doll in Team A will play against the ii-th doll in Team B. If the doll in Team A wins, Team A will get 11 point. If it loses, Team A will lose 11 point. If it ties, Team A will not get points.

Alice knows all the dolls' choices in this game. To be precise, she uses two arrays aa and bb to represent the choices of the dolls in the two teams. a_iai​ means the choice of the ii-th doll in Team A, and b_ibi​ means the choice of the ii-th doll in Team B. In this question, we use 11 for rock, 22 for scissors, and 33 for paper.

Now for each team, Alice wants to change the choices of at most nn dolls to make the score of Team A as high as possible.

Find the maximum score of Team A and its construction method. If there are multiple answers print any of them (you still have to maximize the score of Team A).

输入格式

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

For each test case, the first line contains one integer nn.

Then two lines follow, containing an array aa of length 2\cdot n2⋅n and an array bb of length 2\cdot n2⋅n, respectively.

输出格式

For each test case, print three lines.

The first line contains one integer, the maximum score of Team A.

The second line contains an array a'a′ of length 2\cdot n2⋅n, which represents the aa array after Alice's modification. For integers 11 to 2\cdot n2⋅n, if a_i \ne a'_iai​=ai′​, then it means you have modified the choice of one player in Team A.

The third line contains an array b'b′ of length 2\cdot n2⋅n, which represents the bb array after Alice's modification. For integers 11 to 2\cdot n2⋅n, if b_i \ne b'_ibi​=bi′​, then it means you have modified the choice of one player in Team B.

题意翻译

AB 每队 2n2n 人正在玩石头剪刀布。A 队第 ii 个人出 a_iai​,B 队第 ii 个人出 b_ibi​。编号相同的人会对战。若 A 队赢则加一分,平不得分,输扣一分。你可以至多改变每队 nn 个人的出拳方案,使得 A 队的得分最高。输出得分的最大值和任意一组构造方案。

本题中,我们用 11 代表石头,22 代表剪刀,33 代表布。

输入输出样例

输入 #1复制

2
1
1 2
1 2
2
2 3 1 3
1 2 2 1

输出 #1复制

2
1 1
2 2
4
3 1 1 3
1 2 2 1

说明/提示

Explanation

For the first test case, we can change a_2a2​ to 11 and b_1b1​ to 22. Then Team A can get 22 points. It can be proved that this is the maximum score that Team A can get.

For the second test case, we can change a_1a1​ to 33 and a_2a2​ to 11.

Constraints

1\le T,n \le 10^5;\ 1\le a_i,b_i \le 31≤T,n≤105; 1≤ai​,bi​≤3. The sum of nn over all test cases \le 10^5≤105.

这个题目用到的方法是将前后两段猜拳进行分离,前段只修改A组,后端只修改B组(或反之),以达到节省时间的目的

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<malloc.h>//石头1,剪刀2,布3
int a[200100]={0},b[200100]={0},tip[200100]={0};

int main(){
	int T;
	scanf("%d",&T);
	int d,n,k;
	for(int t=0;t<T;t++){
		k=0;scanf("%d",&d);n=d*2;
		printf("%d\n",n);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		for(int i=1;i<=d;i++){
			scanf("%d",&b[i]);
			if(a[i]==1) b[i]=2;
			else if(a[i]==2) b[i]=3;
			else b[i]=1;
			printf("%d ",a[i]);
		}
		for(int i=d+1;i<=n;i++){
			scanf("%d",&b[i]);
			if(b[i]==1) printf("3 ");
			else if(b[i]==2) printf("1 ");
			else printf("2 ");
		}printf("\n");
		for(int i=1;i<=n;i++) printf("%d ",b[i]);
		printf("\n");
	}
	return 0;
}

[B2] Alice Wins! (hard version) - 洛谷

题目描述

The difference between the versions is the limit of operations.

Alice is a cute girl who has a lot of dolls.

There are 4\cdot n4⋅n dolls playing rock-paper-scissors. They are divided into two teams: Team A and Team B. Each team contains 2\cdot n2⋅n dolls.

A total of 2\cdot n2⋅n rounds of the game will be played. In the ii-th round, the ii-th doll in Team A will play against the ii-th doll in Team B. If the doll in Team A wins, Team A will get 11 point. If it loses, Team A will lose 11 point. If it ties, Team A will not get points.

Alice knows all the dolls' choices in this game. To be precise, she uses two arrays aa and bb to represent the choices of the dolls in the two teams. a_iai​ means the choice of the ii-th doll in Team A, and b_ibi​ means the choice of the ii-th doll in Team B. In this question, we use 11 for rock, 22 for scissors, and 33 for paper.

Now for each team, Alice wants to change the choices of exact nn dolls to make the score of Team A as high as possible.

Find the maximum score of Team A and its construction method. If there are multiple answers print any of them (you still have to maximize the score of Team A).

输入格式

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

For each test case, the first line contains one integer nn.

Then two lines follow, containing an array aa of length 2\cdot n2⋅n and an array bb of length 2\cdot n2⋅n, respectively.

输出格式

For each test case, output three lines.

The first line contains one integer, which is the maximize the score of Team A.

The second line contains an array a'a′ of length 2\cdot n2⋅n, which represents the modified aa array. For integers 11 to 2\cdot n2⋅n, if a_i \ne a'_iai​=ai′​, then it means you have modified the hand shape of one player in Team A.

The third line contains an array b'b′ of length 2\cdot n2⋅n, which represents the modified bb array. For integers 11 to 2\cdot n2⋅n, if b_i \ne b'_ibi​=bi′​, then it means you have modified the hand shape of one player in Team B.

题意翻译

AB 每队 2n2n 人正在玩石头剪刀布。A 队第 ii 个人出 a_iai​,B 队第 ii 个人出 b_ibi​。编号相同的人会对战。若 A 队赢则加一分,平不得分,输扣一分。你必须恰好改变每队 nn 个人的出拳方案,使得 A 队的得分最高。输出得分的最大值和任意一组构造方案。

本题中,我们用 11 代表石头,22 代表剪刀,33 代表布。

输入输出样例

输入 #1复制

1
1
1 2
1 2

输出 #1复制

2
1 1
2 2

说明/提示

Explanation

For the first test case, we can change a_2a2​ to 11 and b_1b1​ to 22. Then Team A can get 22 points. It can be proved that this is the maximum score that Team A can get.

Constraints

1\le T,n \le 10^5;\ 1\le a_i,b_i \le 31≤T,n≤105; 1≤ai​,bi​≤3. The sum of nn of all test cases \le 10^5≤105.

与上一题不同,这里的要求是“正好”,上一题要求是“至多”

看着题解写的,写麻了

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<malloc.h>
int a[200100]={0},b[200100]={0},c[200100]={0},d[200100]={0},tip[200100]={0};
int fuck(int i,int j){
	if(i==1&&j==2||i==2&&j==3||i==3&&j==1) return 1;
	else return 0;
}
int main(){
	int t;
	scanf("%d",&t);
	while(t--){
		int n;
		scanf("%d",&n);
		int all=0,a1=0,b1=0;
		for(int i=1;i<=n*2;i++){
			scanf("%d",&a[i]);
			c[i]=a[i];
			tip[i]=0;
		}
		for(int i=1;i<=n*2;i++){
			scanf("%d",&b[i]);
			d[i]=b[i];
		}
		for(int i=1;i<=2*n;i++){
			if(fuck(a[i],b[i]))continue;
			all++;
			if(all%2==1){
				a1++;
				c[i]=d[i]-1;
				if(c[i]==0)c[i]=3;
			}else{
				b1++;
				d[i]=c[i]+1;
				if(d[i]==4)d[i]=1;
			}
			tip[i]=1;			
		}
		for(int i=1;i<=2*n;i++){
			if(tip[i])continue;
			if(a1<n&&b1<n){
				c[i]+=1;
				if(c[i]==4)c[i]=1;
				d[i]+=1;
				if(d[i]==4)d[i]=1;
				a1++,b1++;
			}
			if(a1==b1&&a1==n)break;
		}
		if(b1<n){
			for(int i=1;i<=n*2;i++){
				if(tip[i]){
					for(int j=1;j<=3;j++){
						for(int k=1;k<=3;k++)
							if(j!=a[i]&&k!=b[i]&&fuck(j,k))c[i]=j,d[i]=k;
					}
					break;
				}	
			}
		}
		printf("%d\n",2*n);
		for(int i=1;i<=n*2;i++) printf("%d ",c[i]);printf("\n");
		for(int i=1;i<=n*2;i++) printf("%d ",d[i]);printf("\n");
	}
	return 0;
}

Alice and Bob are playing a Normal Game - 洛谷

数字相加的规则是第奇数个为正,第偶数个为负,最后每个数加减加减,得到最后的数字

Alice需要结果最大,在加入过程中就需要让当前加的数字之和绝对值最大,而bob需要结果最小,就要让当前数字之和趋近于零

实际上最后得到的数字的大小只取决于是谁最后一个加入数字

但是需要特判有两处,一是n+k,也就是所有数字的数量,决定是谁最后一个加入数字,二是加入最后数字时前面数字之和的正负,

假设Alice最后一个加入数字,n+k为奇数,需要加入的是正数a,若原来得到的数字b为正,若|a|>|b|,则a放在队列前面,得到a-b>0,是能得到最大值的最优解;若|a|<|b|,则a放在队列后面,得到b-a>0,也是当前最优解,其他情况也可以推出来

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<malloc.h>
int main(){
	int n,k,als=0,bob=0;
	scanf("%d%d",&n,&k);
	if(k%2==0) bob=1;else als=1;
	long long int add=0,sg;
	for(int i=1;i<=n;i++) {
		scanf("%lld",&sg);
		if(i%2==1) add+=sg;
		else add-=sg;
	}
	if(als==1){
		for(int i=1;i<k;i++){
			scanf("%lld",&sg);
			if(i%2==1){
				if(add<0) add-=sg;
				else add+=sg;
			}
			else{
				if(add>0) add=add-sg>0?add-sg:0;
				else add=add+sg<0?add+sg:0;
			}
		}
		scanf("%lld",&sg);
		if(add<0) add=sg-add;
		else add=add+sg;
		printf("%lld",add);
	}
	else{
		for(int i=1;i<k;i++){
			scanf("%lld",&sg);
			if(i%2==0){
				if(add<0) add-=sg;
				else add+=sg;
			}
			else{
				if(add>0) add=add-sg>0?add-sg:0;
				else add=add+sg<0?add+sg:0;
			}
		}
		scanf("%lld",&sg);
		if(add>0) add=-sg-add;
		else add-=sg;
		printf("%lld",add);
	}
	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值