A. Little Artem
Young boy Artem tries to paint a picture, and he asks his mother Medina to help him. Medina is very busy, that's why she asked for your help.
Artem wants to paint an n×mn×m board. Each cell of the board should be colored in white or black.
Lets BB be the number of black cells that have at least one white neighbor adjacent by the side. Let WW be the number of white cells that have at least one black neighbor adjacent by the side. A coloring is called good if B=W+1B=W+1.
The first coloring shown below has B=5B=5 and W=4W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4B=4, W=4W=4 (only the bottom right cell doesn't have a neighbor with the opposite color).
Please, help Medina to find any good coloring. It's guaranteed that under given constraints the solution always exists. If there are several solutions, output any of them.
Input
Each test contains multiple test cases.
The first line contains the number of test cases tt (1≤t≤201≤t≤20). Each of the next tt lines contains two integers n,mn,m (2≤n,m≤1002≤n,m≤100) — the number of rows and the number of columns in the grid.
Output
For each test case print nn lines, each of length mm, where ii-th line is the ii-th row of your colored matrix (cell labeled with 'B' means that the cell is black, and 'W' means white). Do not use quotes.
It's guaranteed that under given constraints the solution always exists.
Example
input
2
3 2
3 3
output
BW
WB
BB
BWB
BWW
BWB
题意:给你一个n*m的矩阵,(2<=n,m<=100),要求你涂色,记W是四周至少有一个黑色方块的白色方块,B是四周至少有一个白色方块的黑色方块,如果B=W+1,那这个涂色方案就是好的,求怎么涂才能变成好的
题解:因为矩阵至少是2X2的,所以没必要特判,随便让四个角之一的格子变成白的,其他都是黑色即可
代码:
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
B. Kind Anton
Once again, Boris needs the help of Anton in creating a task. This time Anton needs to solve the following problem:
There are two arrays of integers aa and bb of length nn. It turned out that array aa contains only elements from the set {−1,0,1}{−1,0,1}.
Anton can perform the following sequence of operations any number of times:
For example, if you are given array [1,−1,0][1,−1,0], you can transform it only to [1,−1,−1][1,−1,−1], [1,0,0][1,0,0] and [1,−1,1][1,−1,1] by one operation.
Anton wants to predict if it is possible to apply some number (zero or more) of these operations to the array aa so that it becomes equal to array bb. Can you help him?
Input
Each test contains multiple test cases.
The first line contains the number of test cases tt (1≤t≤100001≤t≤10000). The description of the test cases follows.
The first line of each test case contains a single integer nn (1≤n≤1051≤n≤105) — the length of arrays.
The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−1≤ai≤1−1≤ai≤1) — elements of array aa. There can be duplicates among elements.
The third line of each test case contains nn integers b1,b2,…,bnb1,b2,…,bn (−109≤bi≤109−109≤bi≤109) — elements of array bb. There can be duplicates among elements.
It is guaranteed that the sum of nn over all test cases doesn't exceed 105105.
Output
For each test case, output one line containing "YES" if it's possible to make arrays aa and bb equal by performing the described operations, or "NO" if it's impossible.
You can print each letter in any case (upper or lower).
Example
input
5
3
1 -1 0
1 1 -2
3
0 1 1
0 2 2
2
1 0
1 41
2
-1 0
-1 -41
5
0 1 -1 1 -1
1 1 -1 1 -1
output
YES
NO
YES
YES
NO
题意:给你一个长度为n的数组a和b,a只包含-1,0,1,对于i
题解:首先假如a[1]!b[1],直接输出NO,(假如不这样可能会T?),然后倒着遍历b,假如b[i]>a[i],那就必须要在i前面找a[i]==1,同理假如b[i]
代码:
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
C. Eugene and an array
Eugene likes working with arrays. And today he needs your help in solving one challenging task.
An array cc is a subarray of an array bb if cc can be obtained from bb by deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.
Let's call a nonempty array good if for every nonempty subarray of this array, sum of the elements of this subarray is nonzero. For example, array [−1,2,−3][−1,2,−3] is good, as all arrays [−1][−1], [−1,2][−1,2], [−1,2,−3][−1,2,−3], [2][2], [2,−3][2,−3], [−3][−3] have nonzero sums of elements. However, array [−1,2,−1,−3][−1,2,−1,−3] isn't good, as his subarray [−1,2,−1][−1,2,−1] has sum of elements equal to 00.
Help Eugene to calculate the number of nonempty good subarrays of a given array aa.
Input
The first line of the input contains a single integer nn (1≤n≤2×1051≤n≤2×105) — the length of array aa.
The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109) — the elements of aa.
Output
Output a single integer — the number of good subarrays of aa.
Examples
input
3
1 2 -3
output
5
input
3
41 -41 41
output
3
题意:给你一个数组,求元素和不等于0的连续子数组有多少个.
题解:对数组的每一个位置求前缀和s,假如si==sj,那么ai+1+…..aj=0,所以我们就不要考虑i+1之前的数了(子数组的子数组中也不能为0),用一个map来存前缀和的位置,遍历一边前缀和,可以理解为每次从该位置开始向左找,如果找到第一个与它相等的值,我们就记录这个区间的长度,
用代码来表示就是,假如在当前位置之前有与它相等的前缀和,那么我们就选择距离当前位置最近的那个(子数组的子数组中也不能为0),记录这个区间的长度即可
1 #include
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
手机扫一扫
移动阅读更方便
你可能感兴趣的文章