Codeforces Round #632 (Div. 2) 题解
空山新雨后,天气晚来秋。
明月松间照,清泉石上流。
竹喧归浣女,莲动下渔舟。
随意春芳歇,王孙自可留。——王维
A. Little Artem
网址:https://codeforces.com/contest/1333/problem/A
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
input
2
3 2
3 3
output
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即可。
可惜,我的功夫不够,讨论了半天,当时代码:
https://codeforces.com/contest/1333/submission/76088370
代码如下:
#include<iostream>
#include<cstdio>
using namespace std;
int n, m;
int main()
{
int T;
scanf("%d", &T);
while(T --)
{
scanf("%d %d", &n, &m);
putchar('W');
for(int i = 1; i < m; ++ i)
{
putchar('B');
}
for(int i = 1; i < n; ++ i)
{
puts("");
for(int j = 0; j < m; ++ j)
putchar('B');
}
}
return 0;
}
B. Kind Anton
网址:https://codeforces.com/contest/1333/problem/B
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<j≤n. It is possible to choose the same pair (i,j) more than once.
Add ai to aj. In other words, j-th element of the array becomes equal to ai+aj.
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
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
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.
题意概述:
给你由0、1、-1构成的序列a,要求每次操作可以选择两个数(i,j),使得a[j] += a[i],问你是否可以将序列a转化为b。
考虑:a[i] < b[i]则意味着当i之前存在着某个数>0,换句话说:a[i] > b[j]意味着i之前存在某个数<0,否则无法完成转化操作。
不解释。
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#define maxn 200000 + 6
using namespace std;
int n, a[maxn], b[maxn], s_plus[maxn] = {}, s_minus[maxn] = {};
int main()
{
int T;
scanf("%d", &T);
while(T --)
{
scanf("%d", &n);
for(int i = 1; i <= n; ++ i)
{
scanf("%d", &a[i]);
if(a[i] == -1)
{
s_minus[i] = s_minus[i - 1] + 1;
s_plus[i] = s_plus[i - 1];
}
else if(a[i] == 1)
{
s_minus[i] = s_minus[i - 1];
s_plus[i] = s_plus[i - 1] + 1;
} else s_plus[i] = s_plus[i - 1], s_minus[i] = s_minus[i - 1];
}
for(int i = 1; i <= n; ++ i) scanf("%d", &b[i]);
if(a[1] != b[1])
{
puts("NO");
memset(s_plus, 0, sizeof(s_plus));
memset(s_minus, 0, sizeof(s_minus));
continue;
}
bool valid = true;
for(int i = n; i >= 2; -- i)
{
if(a[i] == b[i]) continue;
if(a[i] < b[i])
{
if(s_plus[i - 1]) continue;
else
{
puts("NO");
valid = false;
break;
}
}
else
{
if(s_minus[i - 1]) continue;
else
{
puts("NO");
valid = false;
break;
}
}
}
memset(s_plus, 0, sizeof(s_plus));
memset(s_minus, 0, sizeof(s_minus));
if(valid) puts("YES");
}
return 0;
}
C. Eugene and an array
网址:https://codeforces.com/contest/1333/problem/C
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
input
3
1 2 -3
output
5
input
3
41 -41 41
output
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.
这道题很不错,足以迷惑我。还是基础不扎实。
预处理前缀和。如果[L,R]和为0,那么有:
s[R] = s[L - 1];
接着就变成了刘汝佳的“滑动窗口”;
- 每次枚举“g好”序列的左端点,向右扩展直到有重复的停止扩展;
- 将该序列长度累和;
- 重复上述过程;
- 具体讲解见刘汝佳《算法竞赛 入门经典》chapter 8;
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
using namespace std;
const int maxn = 200000 + 5;
set <long long> cur;
int n, a[maxn];
long long s[maxn];
int main()
{
cur.clear();
memset(s, 0, sizeof(s));
scanf("%d", &n);
for(int i = 1; i <= n; ++ i)
{
scanf("%d", &a[i]);
s[i] = s[i - 1] + a[i];
}
int head = 0, tail = 0;
long long sum = 0;
while(head <= n)
{
while(!cur.count(s[tail]) && tail <= n)
{
cur.insert(s[tail]);
++ tail;
}
sum += tail - head - 1;
cur.erase(s[head]);
++ head;
}
printf("%lld\n", sum);
return 0;
}
D. Challenges in school №41
网址:https://codeforces.com/contest/1333/problem/D?csrf_token=752bb72c3fd70a04956d182748915494
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
input
2 1
RL
output
1 1
input
2 1
LR
output
-1
input
4 2
RLRL
output
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.
这道题把朝向右看作0,朝向左看成1,操作相当于冒泡排序(但仍有区别);
具体地,在一步操作中,可以将(0,1)->(1,0),但如果存在相邻两数均为1的情况时,就不可以连续进行操作,需要特殊进行处理;
接着,记录两个参量:mink,maxk,前者代表最少可以操作的次数(每一次都把可以操作的全部操作),后者不难意识到,即为操作的总次数。
若k在[mink, maxk],则说明问题有解;否则,输出-1;
那么怎么分配就随意了,我的做法是将操作记录在mink中,每一次的总操作数量和实际操作数比较,若大于,则将该mink操作全部拆为k + 1次操作;否则,能拆多少就拆多少;
若剩余的mink操作数等于当前实际剩余操作数,就按照预处理的顺次输出答案。
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#define maxn 3000 + 5
using namespace std;
vector <int> book[maxn];
int n, k, a[maxn], mink, maxk;
int main()
{
char ch[maxn];
memset(a, 0, sizeof(a));
scanf("%d %d", &n, &k);
scanf("%s", ch);
for(int i = 0; i < n; ++ i)
{
if(ch[i] == 'L') a[i] = 1;
else a[i] = 0;
}
maxk = mink = 0;
for(int i = 0; i < n; ++ i)
{
int cnt = 0;
book[i].clear();
for(int j = 1; j < n; ++ j)
{
if(a[j] > a[j - 1])
{
swap(a[j], a[j - 1]);
book[i].push_back(j);
++ j;
++ cnt;
}
}
if(cnt == 0) break;
++ mink;
maxk += cnt;
}
if(k < mink || k > maxk) puts("-1");
else
{
int i, j, delta, num = k;
for(i = 0; i < mink; ++ i)
{
delta = num - (mink - i);
if(delta == 0)
{
printf("%d ", book[i].size());
for(j = 0; j < book[i].size(); ++ j)
printf("%d ", book[i][j]);
puts("");
-- num;
}
else
{
int tmp = (book[i].size() - 1) - delta;
if(tmp <= 0)
{
for(j = 0; j < book[i].size(); ++ j) printf("1 %d\n", book[i][j]);
num -= book[i].size();
}
else
{
num -= delta + 1;
for(j = 0; j < delta; ++ j) printf("1 %d\n", book[i][j]);
printf("%d ", book[i].size() - delta);
for(j = delta; j < book[i].size(); ++ j) printf("%d ", book[i][j]);
puts("");
}
}
}
}
return 0;
}
Codeforces Round #632 (Div. 2) 题解的更多相关文章
- Codeforces Round #632 (Div. 2)
Codeforces Round #632 (Div. 2) 这一场打的好差呀,这几次艰难上的分全部掉回去了,感觉就像一夜回到了解放前. 说实话,就是被B卡到了,没看到只能从小的放到大的... Lit ...
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
随机推荐
- 微信小程序页面传值详解
我们知道,在微信小程序中,从一个页面转到另一个页面,一般情况下可以通过navigate或redirect时候的url来携带参数,然后在目标页面的onLoad函数参数中获取这些url参数.例如: / ...
- jmeter术语
1.负载:模拟业务请求操作对服务器造成压力的过程 2.性能测试(performance testing):模拟用户负载来测试系统在负载情况下,系统的响应时间.吞吐量等指标是否满足性能要求 3.负载测试 ...
- PTA | 1020. 月饼 (25)
月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时允许取出一部分库存.样 ...
- Android | 教你如何开发扫二维码功能
前言 最近要做一个停车场扫码收费的app,在网上搜了一圈,首先接触到了ZXing,上手试了下,集成过程不复杂,但是感觉效果欠佳,比如距离稍微远点儿就扫不出来了,另外角度对的不好,反光或者光线比较暗 ...
- 团队项目-运动App
一:团队成员介绍 队长:温学智 博客地址:https://www.cnblogs.com/dazhi151/ 技术型大佬,学习能力相对团队来说是最高的.并且作为班 ...
- 按公式产生随机数、java中的重载、递归、有关计算机计算的问题
1.按公式产生随机数x1=(16807*x)%(Integer.MAX_VALUE)x=x1;通过这个公式进行随机数的产生,当产生的数字大于2e+32-2时,在用产生随机数的方式进行数字的输出.主要思 ...
- NS网络仿真,小白起步版,模拟仿真之间注意的事项
FTP是基于TCP的,所以FTP应用不可以绑定UDP发送代理 FTP和CBR属于应用流,他们用来绑定TCP和UDP发送代理 TCP用于发送代理时,接收代理为TCPSink,可以绑定FTP应用.CBR流 ...
- template_showpost
使用<a href='...'>name<\a>实现点击"name"与转向'...'网址的超链接操作 from django.shortcut import ...
- 从零搭建一个SpringCloud项目之Config(五)
配置中心 一.配置中心服务端 新建项目study-config-server 引入依赖 <dependency> <groupId>org.springframework.cl ...
- ArrayBlockingQueue和LinkedBlockingQueue的使用
ArrayBlockingQueue和LinkedBlockingQueue的使用 博客分类: java.util.concurrent BlockingQueue接口定义了一种阻塞的FIFO q ...