Codeforces Round #238 (Div. 2)

A. Gravity Flip
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Chris is bored during his physics lessons (too easy), so he has built a toy box to keep himself occupied. The box is special, since it has the ability to change gravity.

There are n columns of toy cubes in the box arranged in a line. The i-th column contains ai cubes. At first, the gravity in the box is pulling the cubes downwards. When Chris switches the gravity, it begins to pull all the cubes to the right side of the box. The figure shows the initial and final configurations of the cubes in the box: the cubes that have changed their position are highlighted with orange.

Given the initial configuration of the toy cubes in the box, find the amounts of cubes in each of the n columns after the gravity switch!

Input

The first line of input contains an integer n (1 ≤ n ≤ 100), the number of the columns in the box. The next line contains n space-separated integer numbers. The i-th number ai (1 ≤ ai ≤ 100) denotes the number of cubes in the i-th column.

Output

Output n integer numbers separated by spaces, where the i-th number is the amount of cubes in the i-th column after the gravity switch.

Examples
Input
Copy
4
3 2 1 2
Output
1 2 2 3 
Input
Copy
3
2 3 8
Output
2 3 8 
Note

The first example case is shown on the figure. The top cube of the first column falls to the top of the last column; the top cube of the second column falls to the top of the third column; the middle cube of the first column falls to the top of the second column.

In the second example case the gravity switch does not change the heights of the columns.

题意:将左边的方块移到右边,使其不为递减序列。

思路:sort递增排排一遍

#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn = 110;
int vis[maxn];

int main()
{
	int s,e;
	int n,number;
	while(scanf("%d",&n)!=EOF)
	{
		
		for(int i = 0; i < n; i++)
			scanf("%d",&vis[i]);
		sort(vis,vis+n);
		for(int i = 0; i < n; i ++)
			printf("%d ",vis[i]);
		printf("\n");
			
	}
	return 0;
}


B. Domino Effect
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Chris knows there's no fun in playing dominoes, he thinks it's too random and doesn't require skill. Instead, he decided to play with the dominoes and make a "domino show".

Chris arranges n dominoes in a line, placing each piece vertically upright. In the beginning, he simultaneously pushes some of the dominoes either to the left or to the right. However, somewhere between every two dominoes pushed in the same direction there is at least one domino pushed in the opposite direction.

After each second, each domino that is falling to the left pushes the adjacent domino on the left. Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right. When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces. The figure shows one possible example of the process.

Given the initial directions Chris has pushed the dominoes, find the number of the dominoes left standing vertically at the end of the process!

Input

The first line contains a single integer n (1 ≤ n ≤ 3000), the number of the dominoes in the line. The next line contains a character string s of length n. The i-th character of the string si is equal to

  • "L", if the i-th domino has been pushed to the left;
  • "R", if the i-th domino has been pushed to the right;
  • ".", if the i-th domino has not been pushed.

It is guaranteed that if si = sj = "L" and i < j, then there exists such k that i < k < j and sk = "R"; if si = sj = "R" and i < j, then there exists such k that i < k < j and sk = "L".

Output

Output a single integer, the number of the dominoes that remain vertical at the end of the process.

Examples
Input
Copy
14
.L.R...LR..L..
Output
4
Input
Copy
5
R....
Output
0
Input
Copy
1
.
Output
1
Note

The first example case is shown on the figure. The four pieces that remain standing vertically are highlighted with orange.

In the second example case, all pieces fall down since the first piece topples all the other pieces.

In the last example case, a single piece has not been pushed in either direction.

题意:给定长为n的字符串(多米诺骨牌),‘.’表示牌中立,符号‘L’出现时,多米诺骨牌往左倒,只要遇到‘.’中立牌,那么中立牌也往左倒,符号‘R’出现,牌往右倒,只要遇到‘.’中立牌,牌往右倒,问最后剩多少张中立牌?

思路:模拟倒牌过程,用单独的一个数组进行标记,只要倒掉的牌都标记为1,中立的牌为0,最后遍历一遍,剩下多少个0也就是中立的牌数,细节的地方是,比如:R...L。这种情况,中间位置的牌为中立牌并且不会倒下。

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn = 3010;
char vis[maxn];
int book[maxn];

int main()
{
	int s,e;
	int n,number,flag,sum;
	while(scanf("%d",&n)!=EOF)
	{
		scanf("%s",vis);
		memset(book,0,sizeof(book));//数组记录多米罗骨牌是否中立 
		flag = -1;//flag记录是否出现了R...L这样的区间 
		for(int i = 0; i < n; i ++)
		{
			if(vis[i] == 'L')
			{
				book[i] = 1;
				for(int j = i-1; j>=0&&vis[j]=='.';j--)//将位于右边的牌全部标记为倒下。 
					book[j] = 1;
				if(flag!=-1)//如果出现两边往中间倒的情况 
				{
					int number = i-(i-flag)/2;//计算出中间位置 
					if((i-flag)%2==0&&number >=0)//如果区间内的牌数为奇数(因为下标从0开始),并且在数组范围内 
					{
						if(vis[number] == '.')//如果中间位置的牌为中立牌 
							book[number] = 0;//标记此牌为中立牌 
					}
					flag = -1;//重置,方便下一次查找	
				}
			}
			else if(vis[i] == 'R')
			{
				book[i] = 1;
				flag = i;//如果出现了两边往中间倒的区间的左边界,记录下该位置 
				for(int j = i+1;vis[j]=='.'&&vis[j]!='\0'; j ++)
					book[j] = 1;
			}
		}
		sum = 0;
		for(int i = 0; i < n; i ++)
			if(!book[i])
				sum ++;
		printf("%d\n",sum);
			
	}
	return 0;
}

C. Unusual Product
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Chris is a huge fan of linear algebra. This time he has been given a homework about the unusual square of a square matrix.

The dot product of two integer number vectors x and y of size n is the sum of the products of the corresponding components of the vectors. The unusual square of an n × n square matrix A is defined as the sum of n dot products. The i-th of them is the dot product of the i-th row vector and the i-th column vector in the matrix A.

Fortunately for Chris, he has to work only in GF(2)! This means that all operations (addition, multiplication) are calculated modulo 2. In fact, the matrix A is binary: each element of A is either 0 or 1. For example, consider the following matrix A:

The unusual square of A is equal to (1·1 + 1·0 + 1·1) + (0·1 + 1·1 + 1·0) + (1·1 + 0·1 + 0·0) = 0 + 1 + 1 = 0.

However, there is much more to the homework. Chris has to process q queries; each query can be one of the following:

  1. given a row index i, flip all the values in the i-th row in A;
  2. given a column index i, flip all the values in the i-th column in A;
  3. find the unusual square of A.

To flip a bit value w means to change it to 1 - w, i.e., 1 changes to 0 and 0 changes to 1.

Given the initial matrix A, output the answers for each query of the third type! Can you solve Chris's homework?

Input

The first line of input contains an integer n (1 ≤ n ≤ 1000), the number of rows and the number of columns in the matrix A. The next n lines describe the matrix: the i-th line contains n space-separated bits and describes the i-th row of A. The j-th number of the i-th line aij (0 ≤ aij ≤ 1) is the element on the intersection of the i-th row and the j-th column of A.

The next line of input contains an integer q (1 ≤ q ≤ 106), the number of queries. Each of the next q lines describes a single query, which can be one of the following:

  • 1 i — flip the values of the i-th row;
  • 2 i — flip the values of the i-th column;
  • 3 — output the unusual square of A.

Note: since the size of the input and output could be very large, don't use slow output techniques in your language. For example, do not use input and output streams (cin, cout) in C++.

Output

Let the number of the 3rd type queries in the input be m. Output a single string s of length m, where the i-th symbol of s is the value of the unusual square of A for the i-th query of the 3rd type as it appears in the input.

Examples
Input
Copy
3
1 1 1
0 1 1
1 0 0
12
3
2 3
3
2 2
2 2
1 3
3
3
1 2
2 1
1 1
3
Output
Copy
01001

题意:给你一个n*n的矩阵,再输入m行指令,1 i 表示改变第i行的值(进行异或运算),2 i 表示改变第i列的值,3表示输出‘不普通的矩阵和’,不普通的矩阵和其实就是第1-n行的值分别乘以第i-n列的值得总和对2取余,输出各个3指令下不普通矩阵的值。

思路:一开始自己当做一个模拟题来写,超时,然后看了lsb的题解后发现,其实就一个小技巧然后自己还给想复杂了。当i!=j时,vis[i][j]*vis[j][i]会计算两次,所以对2取余后仍然不影响原本的值,i==j时,改变了对角线上的值才会改变矩阵的值,所以只需要在每次改变值后记录下来即可,不普通的矩阵和其实就是对角线之和对2取余。


#include<stdio.h>
#include<string.h>
const int maxn = 1010;
int vis[maxn][maxn],num[maxn],n;
int main()
{
	int m,k,number,number2,sum;
	while(scanf("%d",&n)!=EOF)
	{
		for(int i = 0; i < n; i ++)
			for(int j = 0; j < n; j ++)
				scanf("%d",&vis[i][j]);
		scanf("%d",&m);
		
		k  = sum = 0;//记录和的个数 
		for(int i = 0; i < n; i ++)
			sum += vis[i][i];
		sum = sum%2;
		
		for(int i = 0; i < m; i ++)
		{
			scanf("%d",&number);
			if(number == 3)
			{
				num[k++] = sum;
			}
			else //如果要改变行和列 
			{
				scanf("%d",&number2);
				sum = (sum +1)%2;
			}
		}
		for(int i = 0; i < k; i ++)
			printf("%d",num[i]);
		printf("\n");
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值