Codeforces #Round 632 div2 A~C
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 }
Codeforces #Round 632 div2 A~C的更多相关文章
- Codeforces Round #539 div2
Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...
- 【前行】◇第3站◇ Codeforces Round #512 Div2
[第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...
- Codeforces Round#320 Div2 解题报告
Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...
- Codeforces Round #564(div2)
Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...
- Codeforces Round #632 (Div. 2)
Codeforces Round #632 (Div. 2) 这一场打的好差呀,这几次艰难上的分全部掉回去了,感觉就像一夜回到了解放前. 说实话,就是被B卡到了,没看到只能从小的放到大的... Lit ...
- Codeforces Round #361 div2
ProblemA(Codeforces Round 689A): 题意: 给一个手势, 问这个手势是否是唯一. 思路: 暴力, 模拟将这个手势上下左右移动一次看是否还在键盘上即可. 代码: #incl ...
- Codeforces Round #626 Div2 D,E
比赛链接: Codeforces Round #626 (Div. 2, based on Moscow Open Olympiad in Informatics) D.Present 题意: 给定大 ...
- CodeForces Round 192 Div2
This is the first time I took part in Codeforces Competition.The only felt is that my IQ was contemp ...
- Codeforces Round #359 div2
Problem_A(CodeForces 686A): 题意: \[ 有n个输入, +\space d_i代表冰淇淋数目增加d_i个, -\space d_i表示某个孩纸需要d_i个, 如果你现在手里 ...
随机推荐
- spring ioc踏出第一步
spring IOC(容器) AOP(面向切面编程) IOC容器:他的功能就是可以整合像 Structs2 .Hibernate.Mybatis: IOC:控制反转:所谓的控制就是控制资源的获取方法, ...
- Linux面试必备
1.Linux的体系结构
- 代码审计 - BugkuCTF
extract变量覆盖: 相关函数: extract()函数:从数组中将变量导入到当前的符号表.把数组键名作为变量名,数组的键值作为变量值.但是当变量中有同名的元素时会默认覆盖掉之前的变量值. tri ...
- Wi-Fi IoT套件连PCF8563实现电子钟功能
首先跟同样新入手单片机开发的小伙伴分享一点I2C通信的知识.我估计大部分入手开发板的小伙伴都有一定程序开发的能力,但是底层开发可能是新接触,我看有的小伙伴配置开发环境都有障碍,其实并不是多复杂,只是首 ...
- no-referrer-when-downgrade
原因: 从一个网站链接到另外一个网站会产生新的http请求,referrer是http请求中表示来源的字段.no-referrer-when-downgrade表示从https协议降为http协议时不 ...
- 学习Python之路
陆续学习python已经有一段时间了,但是真正的安下心来学习还是在最近的一个月时间里,虽然每天学习的时间很有限,但是通过点滴的学习让自己感到从未有过的充实,完全打掉了以往认学学习一门语言难于登天的心理 ...
- 小麦苗数据库巡检脚本,支持Oracle、MySQL、SQL Server和PG等数据库
目录 一.巡检脚本简介 二.巡检脚本特点 三.巡检结果展示 1.Oracle数据库 2.MySQL数据库 3.SQL Server数据库 4.PG数据库 5.OS信息 四.脚本运行方式 1.Oracl ...
- is == id ,编码
一. id 查询内存地址. # name = 'alex' # print(id(name)) # name1 = 'alex' # name2 = 'alex' # print(name1 == n ...
- Spring听课笔记(专题二下)
第4章 Spring Bean基于注解的装配 4.1 Bean的定义及作用域的注解实现 1. Bean定义的注解 -- @Component是一个通用注解,可用于任何bean -- @Reposito ...
- RedisUtil,Redis工具类
RedisUtil,Redis工具类 1.配置maven,增加依赖 2.配置工具类 1.配置maven,增加依赖 <dependency> <groupId>redis.cli ...