outputstandard output
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×m board. Each cell of the board should be colored in white or black.
Lets B be the number of black cells that have at least one white neighbor adjacent by the side. Let W 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+1.
The first coloring shown below has B=5 and W=4 (all cells have at least one neighbor with the opposite color). However, the second coloring is not good as it has B=4, W=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 t (1≤t≤20). Each of the next t lines contains two integers n,m (2≤n,m≤100) — the number of rows and the number of columns in the grid.
Output
For each test case print n lines, each of length m, where i-th line is the i-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
inputCopy
2
3 2
3 3
outputCopy
BW
WB
BB
BWB
BWW
BWB
Note
In the first testcase, B=3, W=2.
In the second testcase, B=5, W=4. You can see the coloring in the statement.
题意
有白格子(W)相邻的黑格子(B)的数目==有黑格子相邻的白格子的数目+1
思路
一个角落的出现一个W,其余全为B,那么有白格子相邻的黑格子的数目恒为2,且有黑格子相邻的白格子的数目1
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
int main()
{
int t,n,m;cin>>t;
while(t--)
{
cin>>n>>m;
printf("W");
int l=m-1;
while(l--)
printf("B");
cout<<endl;
for(int i=1;i<n;i++)
{
for(int j=1;j<=m;j++)
cout<<"B";
cout<<endl;
}
}
}
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 a and b of length n. It turned out that array a contains only elements from the set {−1,0,1}.
Anton can perform the following sequence of operations any number of times:
Choose any pair of indexes (i,j) such that 1≤i
For example, if you are given array [1,−1,0], you can transform it only to [1,−1,−1], [1,0,0] and [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 a so that it becomes equal to array b. Can you help him?
Input
Each test contains multiple test cases.
The first line contains the number of test cases t (1≤t≤10000). The description of the test cases follows.
The first line of each test case contains a single integer n (1≤n≤105) — the length of arrays.
The second line of each test case contains n integers a1,a2,…,an (−1≤ai≤1) — elements of array a. There can be duplicates among elements.
The third line of each test case contains n integers b1,b2,…,bn (−109≤bi≤109) — elements of array b. There can be duplicates among elements.
It is guaranteed that the sum of n over all test cases doesn’t exceed 105.
Output
For each test case, output one line containing “YES” if it’s possible to make arrays a and b equal by performing the described operations, or “NO” if it’s impossible.
You can print each letter in any case (upper or lower).
Example
inputCopy
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
outputCopy
YES
NO
YES
YES
NO
Note
In the first test-case we can choose (i,j)=(2,3) twice and after that choose (i,j)=(1,2) twice too. These operations will transform [1,−1,0]→[1,−1,−2]→[1,1,−2]
In the second test case we can’t make equal numbers on the second position.
In the third test case we can choose (i,j)=(1,2) 41 times. The same about the fourth test case.
In the last lest case, it is impossible to make array a equal to the array b.
题意
给一个a数组,一个b数组。其中a数组只含有 -1、0、1。有一种操作aj=aj+ai(i
从a数组最后一个数开始往前遍历,如果ai
CODE
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
int a[100010],b[100010];
int main()
{
int t,n;cin>>t;
int flag[5];
while(t--)
{
cin>>n;
memset(flag,0,sizeof(flag));//-1是2
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]==-1)flag[2]++;
else if(a[i]==1)flag[1]++;
}
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);
bool ans=false;
for(int i=n;i>=1;i--)
{
if(ans)break;
if(a[i]==-1)flag[2]--;
else if(a[i]==1)flag[1]--;
if(a[i]!=b[i])
{
if(a[i]<b[i])
{
//printf("1=%d\n",flag[1]);
if(flag[1]>0)
a[i]=b[i];
else ans=1;
}
else
{
// printf("-1=%d\n",flag[2]);
if(flag[2]>0)
a[i]=b[i];
else ans=1;
}
}
}
if(ans)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
}
outputstandard output
Eugene likes working with arrays. And today he needs your help in solving one challenging task.
An array c is a subarray of an array b if c can be obtained from b 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] is good, as all arrays [−1], [−1,2], [−1,2,−3], [2], [2,−3], [−3] have nonzero sums of elements. However, array [−1,2,−1,−3] isn’t good, as his subarray [−1,2,−1] has sum of elements equal to 0.
Help Eugene to calculate the number of nonempty good subarrays of a given array a.
Input
The first line of the input contains a single integer n (1≤n≤2×105) — the length of array a.
The second line of the input contains n integers a1,a2,…,an (−109≤ai≤109) — the elements of a.
Output
Output a single integer — the number of good subarrays of a.
Examples
inputCopy
3
1 2 -3
outputCopy
5
inputCopy
3
41 -41 41
outputCopy
3
Note
In the first sample, the following subarrays are good: [1], [1,2], [2], [2,−3], [−3]. However, the subarray [1,2,−3] isn’t good, as its subarray [1,2,−3] has sum of elements equal to 0.
In the second sample, three subarrays of size 1 are the only good subarrays. At the same time, the subarray [41,−41,41] isn’t good, as its subarray [41,−41] has sum of elements equal to 0.
题意
定义“好数组”概念为数组和不为0,即数组的和如果不是0就是好数组。问能给定的数组的子数组里面有多少个好数组
思路
求前缀和,如果前缀和ai==aj,则表示i+1~j这段的和为0,i到j的好数组的个数就不能包含这一段
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
map<ll,ll>m;
int main()
{
int n;
cin>>n;
ll sum=0,x,temp=-1,ans=0;
m[0]=0;
for(ll i=1;i<=n;i++)
{
scanf("%lld",&x);
sum+=x;
if(m.count(sum))//与前面的一个地方前缀和相同
{
temp=max(temp,m[sum]);
}
ans+=i-temp-1;
m[sum]=i;
}
cout<<ans<<endl;
return 0;
}
outputstandard output
There are n children, who study at the school №41. It is well-known that they are good mathematicians. Once at a break, they arranged a challenge for themselves. All children arranged in a row and turned heads either to the left or to the right.
Children can do the following: in one second several pairs of neighboring children who are looking at each other can simultaneously turn the head in the opposite direction. For instance, the one who was looking at the right neighbor turns left and vice versa for the second child. Moreover, every second at least one pair of neighboring children performs such action. They are going to finish when there is no pair of neighboring children who are looking at each other.
You are given the number n, the initial arrangement of children and the number k. You have to find a way for the children to act if they want to finish the process in exactly k seconds. More formally, for each of the k moves, you need to output the numbers of the children who turn left during this move.
For instance, for the configuration shown below and k=2 children can do the following steps:
At the beginning, two pairs make move: (1,2) and (3,4). After that, we receive the following configuration:
At the second move pair (2,3) makes the move. The final configuration is reached. Good job.
It is guaranteed that if the solution exists, it takes not more than n2 “headturns”.
Input
The first line of input contains two integers n and k (2≤n≤3000, 1≤k≤3000000) — the number of children and required number of moves.
The next line contains a string of length n and consists only of characters L and R, where L means that the child looks to the left and R means that the child looks to the right.
Output
If there is no solution, print a single line with number −1.
Otherwise, output k lines. Each line has to start with a number ni (1≤ni≤n2) — the number of pairs of children, who turn at this move. After that print ni distinct integers — the numbers of the children who will turn left during this move.
After performing all “headturns”, there can’t be a pair of two neighboring children looking at each other.
If there are many solutions, print any of them.
Examples
inputCopy
2 1
RL
outputCopy
1 1
inputCopy
2 1
LR
outputCopy
-1
inputCopy
4 2
RLRL
outputCopy
2 1 3
1 2
Note
The first sample contains a pair of children who look at each other. After one move, they can finish the process.
In the second sample, children can’t make any move. As a result, they can’t end in k>0 moves.
The third configuration is described in the statement.