AtCoder Beginner Contest 283

AtCoder Beginner Contest 283

A - Power

Problem Statement

Given integers A A A and B B B, print the value A B A^B AB.

Constraints

  • 1 ≤ A , B ≤ 9 1 \leq A, B \leq 9 1A,B9
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

A A A B B B

Output

Print the answer.


Sample Input 1
4 3
Sample Output 1
64

4 3 = 64 4^3 = 64 43=64, so 64 64 64 should be printed.


Sample Input 2
5 5
Sample Output 2
3125

Sample Input 3
8 1
Sample Output 3
8

题目大意:

给两个小于 9 9 9 的正整数 A A A B B B ,求 A B A^B AB

分析:

随便写,不爆 i n t int int

#include<bits/stdc++.h>
using namespace std;
int a,b;
int main(){
	cin>>a>>b;
	cout<<(int)pow(a,b)<<endl;
	return 0;
}

B - First Query Problem

Problem Statement

You are given an integer N N N and a sequence A = ( A 1 , A 2 , … , A N ) A=(A _ 1,A _ 2,\ldots,A _ N) A=(A1,A2,,AN) of length N N N.

Given Q Q Q queries, process them in the given order. Each query is of one of the following two kinds:

  • 1 k x : set the value A k A _ k Ak to x x x.
  • 2 k : print the value A k A _ k Ak.

Constraints

  • 1 ≤ N ≤ 1 0 5 1 \leq N \leq 10 ^ 5 1N105
  • 1 ≤ Q ≤ 1 0 5 1 \leq Q \leq 10 ^ 5 1Q105
  • 0 ≤ A i ≤ 1 0 9   ( 1 ≤ i ≤ N ) 0 \leq A _ i \leq 10 ^ 9\ (1\leq i\leq N) 0Ai109 (1iN)
  • 1 ≤ k ≤ N 1\leq k\leq N 1kN for all queries.
  • 0 ≤ x ≤ 1 0 9 0\leq x\leq 10 ^ 9 0x109 for all queries of the first kind.
  • There is at least one query of the second kind.
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

N N N
A 1 A _ 1 A1 A 2 A _ 2 A2 … \ldots A N A _ N AN
Q Q Q
query ⁡ 1 \operatorname{query} _ 1 query1
query ⁡ 2 \operatorname{query} _ 2 query2
⋮ \vdots
query ⁡ Q \operatorname{query} _ Q queryQ

Here, query ⁡ i \operatorname{query} _ i queryi denotes the i i i-th query, given in one of the following formats:

1 1 1 k k k x x x
2 2 2 k k k

Output

Print q q q lines, where q q q is the number of queries of the second kind. The j j j-th ( 1 ≤ j ≤ q ) (1\leq j\leq q) (1jq) line should contain the response to the j j j-th such query.


Sample Input 1
3
1 3 5
7
2 2
2 3
1 3 0
2 3
1 2 8
2 2
2 1
Sample Output 1
3
5
0
8
1

Initially, A = ( 1 , 3 , 5 ) A=(1,3,5) A=(1,3,5).

  • For the 1 1 1-st query, A = ( 1 , 3 , 5 ) A=(1,3,5) A=(1,3,5), where A 2 = 3 A _ 2=3 A2=3, so 3 3 3 should be printed.
  • For the 2 2 2-nd query, A = ( 1 , 3 , 5 ) A=(1,3,5) A=(1,3,5), where A 3 = 5 A _ 3=5 A3=5, so 5 5 5 should be printed.
  • The 3 3 3-rd query sets the value A 3 A _ 3 A3 to 0 0 0, making A = ( 1 , 3 , 0 ) A=(1,3,0) A=(1,3,0).
  • For the 4 4 4-th query, A = ( 1 , 3 , 0 ) A=(1,3,0) A=(1,3,0), where A 3 = 0 A _ 3=0 A3=0, so 0 0 0 should be printed.
  • The 5 5 5-th query sets the value A 2 A _ 2 A2 to 8 8 8, making A = ( 1 , 8 , 0 ) A=(1,8,0) A=(1,8,0).
  • For the 6 6 6-th query, A = ( 1 , 8 , 0 ) A=(1,8,0) A=(1,8,0), where A 2 = 8 A _ 2=8 A2=8, so 8 8 8 should be printed.
  • For the 7 7 7-th query, A = ( 1 , 8 , 0 ) A=(1,8,0) A=(1,8,0), where A 1 = 1 A _ 1=1 A1=1, so 1 1 1 should be printed.

Sample Input 2
5
22 2 16 7 30
10
1 4 0
1 5 0
2 2
2 3
2 4
2 5
1 4 100
1 5 100
2 3
2 4
Sample Output 2
2
16
0
0
16
100

Sample Input 3
7
478 369 466 343 541 42 165
20
2 1
1 7 729
1 6 61
1 6 838
1 3 319
1 4 317
2 4
1 1 673
1 3 176
1 5 250
1 1 468
2 6
1 7 478
1 5 595
2 6
1 6 599
1 6 505
2 3
2 5
2 1
Sample Output 3
478
317
838
838
176
595
468

题目大意:

给定一个长度为 n n n 的数组 a i a_i ai q q q 次询问:

  • o p = 1 op = 1 op=1 a i ← k a_i \gets k aik
  • o p = 2 op = 2 op=2,输出 a i a_i ai

分析:

#include<bits/stdc++.h>
using namespace std;
#define MAXN 100010
int n,q,a[MAXN];
int main(){
	cin>>n;
	for(int i=1;i<=n;++i)
		cin>>a[i];
	cin>>q;
	for(int i=1;i<=q;++i){
		short op;
		cin>>op;
		if(op==1){
			int k,x;
			cin>>k>>x;
			a[k]=x;
		}else{
			int k;
			cin>>k;
			cout<<a[k]<<endl;
		}
	}
	return 0;
}

C - Cash Register

Problem Statement

Takahashi is a cashier.

There is a cash register with 11 11 11 keys: 00, 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. The cash register initially displays 0 0 0. Whenever he types the key 00, the displayed number is multiplied by 100 100 100; whenever he types one of the others, the displayed number is multiplied by 10 10 10, and then added by the number written on the key.

Takahashi wants the cash register to display an integer S S S. At least how many keystrokes are required to make it display S S S?

Constraints

  • 1 ≤ S ≤ 1 0 100000 1\leq S\leq 10^{100000} 1S10100000
  • S S S is an integer.

Input

The input is given from Standard Input in the following format:

S S S

Output

Print the answer in a line.


Sample Input 1
40004
Sample Output 1
4

For example, the following four keystrokes make the cash register display 40004 40004 40004. Initially, the cash register displays 0 0 0.

  • Type the key 4. It now displays 4 4 4.
  • Type the key 00. It now displays 400 400 400.
  • Type the key 0. It now displays 4000 4000 4000.
  • Type the key 4. It now displays 40004 40004 40004.

He cannot make it display 40004 40004 40004 with three or fewer keystrokes, so 4 4 4 should be printed.


Sample Input 2
1355506027
Sample Output 2
10

Sample Input 3
10888869450418352160768000001
Sample Output 3
27

Note that S S S may not fit into a 64 64 64- bit ⁡ \operatorname{bit} bit integer type.

题目大意:

给定一个数字串 S S S,每次你可以按一下几个键:012345678900

有一个数 x x x,初始值为 0 0 0

每次按下一个键 t t t x x x 将变为 10 + t 10 + t 10+t。特别的,如果按下 00,xx 将便为 100 x 100x 100x

问:至少要按几次按键才能将 x x x 便为 S S S

分析:

先处理 00 00 00 ,找到则删除并且 + + a n s ++ans ++ans ,剩下的长度即为还需按下的次数。

#include<bits/stdc++.h>
using namespace std;
string s;
int ans=0;
int main(){
	cin>>s;
	int pos=s.find("00");
	while(pos!=-1){
		s.erase(pos,2);
		++ans;
		pos=s.find("00",pos);
	}cout<<ans+s.length()<<endl;
	return 0;
}

D - Scope

Problem Statement

A string consisting of lowercase English letters, (, and ) is said to be a good string if you can make it an empty string by the following procedure:

  • First, remove all lowercase English letters.
  • Then, repeatedly remove consecutive () while possible.

For example, ((a)ba) is a good string, because removing all lowercase English letters yields (()), from which we can remove consecutive () at the 2 2 2-nd and 3 3 3-rd characters to obtain (), which in turn ends up in an empty string.

You are given a good string S S S. We denote by S i S_i Si the i i i-th character of S S S.

For each lowercase English letter a, b, … \ldots , and z, we have a ball with the letter written on it. Additionally, we have an empty box.

For each i = 1 , 2 , i = 1,2, i=1,2, … \ldots , ∣ S ∣ |S| S in this order, Takahashi performs the following operation unless he faints.

  • If S i S_i Si is a lowercase English letter, put the ball with the letter written on it into the box. If the ball is already in the box, he faints.
  • If S i S_i Si is (, do nothing.
  • If S i S_i Si is ), take the maximum integer j j j less than i i i such that the j j j-th through i i i-th characters of S S S form a good string. (We can prove that such an integer j j j always exists.) Take out from the box all the balls that he has put in the j j j-th through i i i-th operations.

Determine if Takahashi can complete the sequence of operations without fainting.

Constraints

  • 1 ≤ ∣ S ∣ ≤ 3 × 1 0 5 1 \leq |S| \leq 3 \times 10^5 1S3×105
  • S S S is a good string.

Input

The input is given from Standard Input in the following format:

S S S

Output

Print Yes if he can complete the sequence of operations without fainting; print No otherwise.


Sample Input 1
((a)ba)
Sample Output 1
Yes

For i = 1 i = 1 i=1, he does nothing.
For i = 2 i = 2 i=2, he does nothing.
For i = 3 i = 3 i=3, he puts the ball with a written on it into the box.
For i = 4 i = 4 i=4, j = 2 j=2 j=2 is the maximum integer less than 4 4 4 such that the j j j-th through 4 4 4-th characters of S S S form a good string, so he takes out the ball with a written on it from the box.
For i = 5 i = 5 i=5, he puts the ball with b written on it into the box.
For i = 6 i = 6 i=6, he puts the ball with a written on it into the box.
For i = 7 i = 7 i=7, j = 1 j=1 j=1 is the maximum integer less than 7 7 7 such that the j j j-th through 7 7 7-th characters of S S S form a good string, so he takes out the ball with a written on it, and another with b, from the box.

Therefore, the answer to this case is Yes.


Sample Input 2
(a(ba))
Sample Output 2
No

For i = 1i=1, he does nothing.
For i = 2i=2, he puts the ball with a written on it into the box.
For i = 3i=3, he does nothing.
For i = 4i=4, he puts the ball with b written on it into the box.
For i = 5i=5, the ball with a written on it is already in the box, so he faints, aborting the sequence of operations.

Therefore, the answer to this case is No.


Sample Input 3
(((())))
Sample Output 3
Yes

Sample Input 4
abca
Sample Output 4
No

题目大意:

假设有一个字符串,只包含 ()和小写字母。如果通过以下步骤,能使字符串为空,则称这个字符串为好的:

  • 删除所有小写字母
  • 不停地删除连续的()

给定一个好的字符串 S S S。字符串中所有的小写字母对应一个小球。此外,我们有一个箱子。

一个人按照 1 , 2 , 3 , ⋯   , ∣ S ∣ 1,2,3,\cdots,|S| 1,2,3,,S 的顺序取球:

  • 如果 S i S_i Si(,什么也不做。
  • 如果 S i S_i Si 为小写字母,就将这个小球放入箱子中。如果这个小球已经出现在箱子中,他会晕倒。
  • 如果 S i S_i Si),取小于 i i i 的最大的 j j j,使 S i ∼ S j S_i \sim S_j SiSj 这个子串是好的。将 j j j i i i 操作中放入的小球全部取出。

在这个过程中,如果他晕倒了,输出No。否则输出Yes

分析:

可以使用栈和哈希表模拟实现。

#include<bits/stdc++.h>
using namespace std;
#define MAXN 300010
string s;
void ini(int rl[]){
	stack<int>parenthesis;
	for(int i=0;i<s.length();++i){
		if(s[i]=='(')
			parenthesis.push(i);
		if(s[i]==')'){
			rl[i]=parenthesis.top();//已知右括号位置,求左括号位置
			parenthesis.pop();
		}
	}return;
}
bool ch2(char x){//判断该字符是否为小写字母
	return 'a'<=x&&x<='z';
}
bool oprt(){
	stack<int>box;//盒中存储放入的字母的下标
	int vh[26];
	int rl[MAXN];
	ini(rl);
	for(int i=0;i<s.length();++i){//遍历字符串
		if(ch2(s[i])){//当前字符为小写字母,放入盒中
			if(vh[s[i]-'a']!=0)//如果盒中已有该字母,返回false
				return false;
			else{
				++vh[s[i]-'a'];//可用bool型,当时抽了写了int型()
				box.push(i);//将该字母的下标存入
			}
		}if(s[i]==')')//右括号
			while(!box.empty()&&rl[i]<box.top()){//将区间(rl[i],i)的所有字母弹出
				--vh[s[box.top()]-'a'];
				box.pop();
			}
	}return true;
}
int main(){
	cin>>s;
	if(oprt())
		cout<<"Yes"<<endl;
	else
		cout<<"No"<<endl;
	return 0;
}

E - Don’t Isolate Elements

Problem Statement

You are given a matrix A A A with H H H rows and W W W columns. The value of each of its elements is 0 0 0 or 1 1 1. For an integer pair ( i , j ) (i, j) (i,j) such that 1 ≤ i ≤ H 1 \leq i \leq H 1iH and 1 ≤ j ≤ W 1 \leq j \leq W 1jW, we denote by A i , j A_{i,j} Ai,j the element at the i i i-th row and j j j-th column.

You can perform the following operation on the matrix A A A any number of times (possibly zero):

  • Choose an integer i i i such that 1 ≤ i ≤ H 1 \leq i \leq H 1iH. For every integer j j j such that 1 ≤ j ≤ W 1 \leq j \leq W 1jW, replace the value of A i , j A_{i,j} Ai,j with 1 − A i , j 1-A_{i,j} 1Ai,j.

A i , j A_{i,j} Ai,j is said to be isolated if and only if there is no adjacent element with the same value; in other words, if and only if none of the four integer pairs ( x , y ) = ( i − 1 , j ) , ( i + 1 , j ) , ( i , j − 1 ) , ( i , j + 1 ) (x,y) = (i-1,j),(i+1,j),(i,j-1),(i,j+1) (x,y)=(i1,j),(i+1,j),(i,j1),(i,j+1) satisfies 1 ≤ x ≤ H , 1 ≤ y ≤ W 1 \leq x \leq H, 1 \leq y \leq W 1xH,1yW, and A i , j = A x , y A_{i,j} = A_{x,y} Ai,j=Ax,y.

Determine if you can make the matrix A A A in such a state that no element is isolated by repeating the operation. If it is possible, find the minimum number of operations required to do so.

Constraints

  • 2 ≤ H , W ≤ 1000 2 \leq H,W \leq 1000 2H,W1000
  • A i , j = 0 A_{i,j} = 0 Ai,j=0 or A i , j = 1 A_{i,j} = 1 Ai,j=1
  • All values in the input are integers.

Input

The input is given from Standard Input in the following format:

H H H W W W
A 1 , 1 A_{1,1} A1,1 A 1 , 2 A_{1,2} A1,2 … \ldots A 1 , W A_{1,W} A1,W
A 2 , 1 A_{2,1} A2,1 A 2 , 2 A_{2,2} A2,2 … \ldots A 2 , W A_{2,W} A2,W
⋮ \vdots
A H , 1 A_{H,1} AH,1 A H , 2 A_{H,2} AH,2 … \ldots A H , W A_{H,W} AH,W

Output

If you can make it in such a state that no element is isolated by repeating the operation, print the minimum number of operations required to do so; otherwise, print -1.


Sample Input 1
3 3
1 1 0
1 0 1
1 0 0
Sample Output 1
1

An operation with i = 1 i = 1 i=1 makes A = ( ( 0 , 0 , 1 ) , ( 1 , 0 , 1 ) , ( 1 , 0 , 0 ) ) A = ((0,0,1),(1,0,1),(1,0,0)) A=((0,0,1),(1,0,1),(1,0,0)), where there is no longer an isolated element.


Sample Input 2
4 4
1 0 0 0
0 1 1 1
0 0 1 0
1 1 0 1
Sample Output 2
2

Sample Input 3
2 3
0 1 0
0 1 1
Sample Output 3
-1

题目大意:

给定一个 H × W H\times W H×W 01 01 01 矩阵 a a a,称位于第 i i i 行第 j j j 列的元素为 a i , j a_{i,j} ai,j

你可以进行如下的操作任意次(可以是 0 0 0 次):

  • 选择任意一行,翻转此行内的所有元素。

我们称 a i , j a_{i,j} ai,j 被隔离,当且仅当与其四联通的四个元素 a i − 1 , j , a i + 1 , j , a i , j − 1 , a i , j + 1 a_{i - 1,j}, a_{i + 1, j}, a_{i, j - 1}, a_{i, j + 1} ai1,j,ai+1,j,ai,j1,ai,j+1 01 01 01 性与其均不相同。

请输出使得给定矩阵中没有元素被隔离所需要的最小操作次数。如果无论如何操作都无法满足要求则输出 -1

2 ≤ n , m ≤ 1000 2\le n, m \le 1000 2n,m1000

分析:

骗分:

很明显的可以骗分的题。输出 − 1 -1 1 可以过 12 / 30 12/30 12/30 的点。

动规:

1.每一行最多只会被取反一次。
2.设当前一行行号为 i i i ,假设 i i i 足够大,第 i − 2 i-2 i2 行的取反不会影响该行是否具有被隔离的元素,只有 i − 1 ∼ i + 1 i-1\sim i+1 i1i+1 行的取反会对该行产生影响。
3.无论上面的矩阵如何操作使得无被隔离元素,都不影响下面的操作,交界处的是否取反可以设成一个参数。

则具有最优解子结构,可以使用动规。

定义 d p [ i ] [ j ] [ k ] dp[i][j][k] dp[i][j][k] ,其中 i i i 为行号, j j j 为上一行是否选择取反, k k k 为本行是否选择取反, d p [ i ] [ j ] [ k ] dp[i][j][k] dp[i][j][k] 表示经过操作后如果 i − 1 i-1 i1 行没有被隔离元素,操作到第 i i i 行时需要做的最少操作次数,有被隔离元素时值取无穷大。
则有 i f ( f l a g )   d p [ i ] [ j ] [ k ] = min ⁡ ( d p [ i − 1 ] [ 0 / 1 ] [ j ] + k ) ;   d p [ 1 ] [ 0 ] [ 0 ] = 0 , d p [ 1 ] [ 0 ] [ 1 ] = 1 if(flag)~dp[i][j][k]=\min(dp[i-1][0/1][j]+k);~dp[1][0][0]=0,dp[1][0][1]=1 if(flag) dp[i][j][k]=min(dp[i1][0/1][j]+k); dp[1][0][0]=0,dp[1][0][1]=1

#include<bits/stdc++.h>//看了官方题解之后写的,看完只有一个感想:这代码真抽象
using namespace std;
#define MAXN 1010
int h,w,dp[MAXN][2][2];
short a[MAXN][MAXN];
void invert(short arr[],int pos,short f){//arr数组为被操作数组,pos表示在矩阵中的行数,f表示是否取反
	arr[0]=arr[w+1]=-1;//将-1定为边界
	if(pos==0){//该行为边界,所有元素都为-1
		for(int i=1;i<=w;++i)
			arr[i]=-1;
		return;
	}for(int i=1;i<=w;++i)
		arr[i]=(f?!a[pos][i]:a[pos][i]);//f为true是取反,否则复制一遍
	return;
}bool check(short x[],short y[],short z[],int pos){//判断y[pos]是否被隔离,x为上一行,z为下一行
	return y[pos]==x[pos]||y[pos]==z[pos]||y[pos]==y[pos-1]||y[pos]==y[pos+1];
}
int main(){
	cin>>h>>w;
	for(int i=1;i<=h;++i)
		for(int j=1;j<=w;++j)
			cin>>a[i][j];
	memset(dp,63,sizeof(dp));
	dp[1][0][0]=0,dp[1][0][1]=1;//初始化
	short mns[w+2];//mns数组元素全部为-1
	invert(mns,0,114);//你什么都没看见
	for(int i=2;i<=h;++i)///枚举行数,跳过第一行,因为无论如何第0行都是不需要操作的
	for(short j=0;j<=1;++j)//第i-2行是否取反
	for(short k=0;k<=1;++k)//第i-1行是否取反
	for(short l=0;l<=1;++l){//第i行是否取反
		short x[w+2],y[w+2],z[w+2];//将操作过后的(i-2)~i行存到xyz三个数组里
		invert(x,i-2,j);
		invert(y,i-1,k);
		invert(z,i,l);
		bool flag=true;
		for(int m=1;m<=w;++m){
			if(!check(x,y,z,m)){//判断第i-1行是否有元素被隔离
				flag=false;
				break;
			}if(i==h&&!check(y,z,mns,m)){//特别地,对于操作到最后一行时,除了h-1行需要无被隔离元素,第h行也需要
				flag=false;
				break;
			}
		}if(flag)//符合条件
			dp[i][k][l]=min(dp[i][k][l],dp[i-1][j][k]+l);//状态转移
	}int ans=INT_MAX;
	for(int i=0;i<=1;++i)
		for(int j=0;j<=1;++j)
			ans=min(ans,dp[h][i][j]);//在所有结果中寻找最小的一个
	cout<<(ans<=1000?ans:-1)<<endl;//如果ans最后大于1000,则不可能使矩阵无被隔离元素
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值