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.
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.
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.
2
3 2
3 3
BW
WB
BB
BWB
BWW
BWB
题意:给你一个n*m的矩阵,(2<=n,m<=100),要求你涂色,记W是四周至少有一个黑色方块的白色方块,B是四周至少有一个白色方块的黑色方块,如果B=W+1,那这个涂色方案就是好的,求怎么涂才能变成好的
题解:因为矩阵至少是2X2的,所以没必要特判,随便让四个角之一的格子变成白的,其他都是黑色即可
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <stack> 7 #include <queue> 8 #include <vector> 9 #include <map> 10 #include <set> 11 #include <unordered_set> 12 #include <unordered_map> 13 #define ll long long 14 #define fi first 15 #define se second 16 #define pb push_back 17 #define me memset 18 const int N = 1e6 + 10; 19 const int mod = 1e9 + 7; 20 using namespace std; 21 typedef pair<int,int> PII; 22 typedef pair<long,long> PLL; 23 int t; 24 int n,m; 25 int main() { 26 ios::sync_with_stdio(false); 27 cin>>t; 28 while(t--){ 29 cin>>n>>m; 30 for(int i=1;i<=n;++i) { 31 for (int j=1;j<=m;++j) { 32 if (i ==1&&j==m) printf("W"); 33 else printf("B"); 34 } 35 printf("\n"); 36 } 37 } 38 39 return 0; 40 }
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:
- Choose any pair of indexes (i,j)(i,j) such that 1≤i<j≤n1≤i<j≤n. It is possible to choose the same pair (i,j)(i,j) more than once.
- Add aiai to ajaj. In other words, jj-th element of the array becomes equal to ai+ajai+aj.
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?
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.
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).
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
YES NO YES YES NO
题意:给你一个长度为n的数组a和b,a只包含-1,0,1,对于i<j,有a[j]=a[j]+a[i],问能通过这样的操作将数组a变成数组b
题解:首先假如a[1]!b[1],直接输出NO,(假如不这样可能会T?),然后倒着遍历b,假如b[i]>a[i],那就必须要在i前面找a[i]==1,同理假如b[i]<a[i],就要找a[i]==-1.
代码:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <stack> 7 #include <queue> 8 #include <vector> 9 #include <map> 10 #include <set> 11 #include <unordered_set> 12 #include <unordered_map> 13 #define ll long long 14 #define fi first 15 #define se second 16 #define pb push_back 17 #define me memset 18 const int N = 1e6 + 10; 19 const int mod = 1e9 + 7; 20 using namespace std; 21 typedef pair<int,int> PII; 22 typedef pair<long,long> PLL; 23 24 int t; 25 int n,a[N],b[N]; 26 27 int main() { 28 ios::sync_with_stdio(false); 29 cin>>t; 30 while(t--){ 31 cin>>n; 32 for(int i=0;i<n;++i) cin>>a[i]; 33 for(int i=0;i<n;++i) cin>>b[i]; 34 35 if(a[0]!=b[0]){ 36 printf("NO\n"); 37 continue; 38 } 39 if(n==1 && a[0]==b[0]){ 40 printf("YES\n"); 41 continue; 42 } 43 bool flag=0; 44 for(int i=n-1;i>=1;--i){ 45 flag=0; 46 if(b[i]==a[i]){ 47 flag=1; 48 continue; 49 } 50 else if(b[i]<a[i]){ 51 for(int j=0;j<i;++j){ 52 if(a[j]==-1){ 53 flag=1; 54 break; 55 } 56 } 57 } 58 else if(b[i]>a[i]){ 59 for(int j=0;j<i;++j){ 60 if(a[j]==1){ 61 flag=1; 62 break; 63 } 64 } 65 } 66 if(flag==0) break; 67 } 68 if(flag) printf("YES\n"); 69 else printf("NO\n"); 70 } 71 72 return 0; 73 }
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.
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 a single integer — the number of good subarrays of aa.
3
1 2 -3
5
3
41 -41 41
3
题意:给你一个数组,求元素和不等于0的连续子数组有多少个.
题解:对数组的每一个位置求前缀和s,假如si==sj,那么ai+1+.....aj=0,所以我们就不要考虑i+1之前的数了(子数组的子数组中也不能为0),用一个map来存前缀和的位置,遍历一边前缀和,可以理解为每次从该位置开始向左找,如果找到第一个与它相等的值,我们就记录这个区间的长度,
用代码来表示就是,假如在当前位置之前有与它相等的前缀和,那么我们就选择距离当前位置最近的那个(子数组的子数组中也不能为0),记录这个区间的长度即可
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <stack> 7 #include <queue> 8 #include <vector> 9 #include <map> 10 #include <set> 11 #include <unordered_set> 12 #include <unordered_map> 13 #define ll long long 14 #define fi first 15 #define se second 16 #define pb push_back 17 #define me memset 18 const int N = 1e6 + 10; 19 const int mod = 1e9 + 7; 20 using namespace std; 21 typedef pair<int,int> PII; 22 typedef pair<long,long> PLL; 23 24 int n; 25 ll a[N]; 26 map<ll,ll> mp; 27 28 int main() { 29 ios::sync_with_stdio(false); 30 cin>>n; 31 for(int i=1;i<=n;++i) cin>>a[i]; 32 33 ll ans=0,sum=0,pos=0; 34 mp[0]=1; 35 for(int i=1;i<=n;++i){ 36 sum+=a[i]; 37 if(mp[sum]) pos=max(pos,mp[sum]); 38 ans+=i-pos; 39 mp[sum]=i+1; 40 } 41 printf("%lld\n",ans); 42 43 return 0; 44 }