空山新雨后,天气晚来秋。

明月松间照,清泉石上流。

竹喧归浣女,莲动下渔舟。

随意春芳歇,王孙自可留。——王维

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) 题解的更多相关文章

  1. Codeforces Round #632 (Div. 2)

    Codeforces Round #632 (Div. 2) 这一场打的好差呀,这几次艰难上的分全部掉回去了,感觉就像一夜回到了解放前. 说实话,就是被B卡到了,没看到只能从小的放到大的... Lit ...

  2. Codeforces Round #182 (Div. 1)题解【ABCD】

    Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...

  3. Codeforces Round #608 (Div. 2) 题解

    目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...

  4. Codeforces Round #525 (Div. 2)题解

    Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...

  5. Codeforces Round #528 (Div. 2)题解

    Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...

  6. Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F

    Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...

  7. Codeforces Round #677 (Div. 3) 题解

    Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...

  8. Codeforces Round #665 (Div. 2) 题解

    Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...

  9. Codeforces Round #160 (Div. 1) 题解【ABCD】

    Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...

随机推荐

  1. 【WPF学习】第六十二章 构建更复杂的模板

    在控件模板和为其提供支持的代码之间又一个隐含约定.如果使用自定义控件模板替代控件的标准模板,就需要确保新模板能够满足控件的实现代码的所有需要. 在简单控件中,这个过程比较容易,因为对模板几乎没有(或完 ...

  2. IDEA+Mybatis-generator代码生成工具

    IDEA+Mybatis-generator代码生成工具(小白也能用) 插件介绍 MyBatis Generator简称MBG,是MyBatis 官方出的代码生成器.MBG能够自动生成实体类.Mapp ...

  3. JSP+SSH+Mysql+C3P0实现的传智播客网上商城

    项目简介 项目来源于:https://gitee.com/2121/shop 本系统是传智播客授课时的开发案例,基于JSP+SSH+Mysql的简单网上商城.在当代开发中,SSH的使用已经逐渐被SSM ...

  4. SaaS架构(一) 弱后端强前端的尝试和问题

    最近在公司项目组内部沙龙的时候,提出一个"弱后端强前端"的概念,其实已经在项目内部新的服务有做试点,我们整个SaaS系统,后端主要是JAVA构建,前端是Angular构建.&quo ...

  5. Java基础知识3-类和对象(1)

    面向过程和面向对象的区别 面向过程(结构化程序设计) 实际上是一个面向操作过程,首先设计一系列过程(算法)来求解问题(操作数据),然后再考虑存储数据的方式(组织数据).即程序=算法\+数据结构.对应典 ...

  6. MTK Android 耳机线控的实现方法

    android 耳机线控的实现方法 keycodeonkeydownkeyevent 耳机线控的功能 耳机线控是一种很好用,并且能提升用户体验的功能.可以用来实现一些常用和基本的功能.比如:实现音乐播 ...

  7. 通过powershell查询OU中被禁用的AD账号,并删除他们的所属组

    这个需求可以通过两个方向来实现1.找到禁用的账号,删除除domain users外的所有组,脚本内容如下 #导入AD模块import-module ActiveDirectory #被禁用户$user ...

  8. Linux C++ 网络编程学习系列(5)——多路IO之epoll边沿触发

    多路IO之epoll边沿触发+非阻塞 源码地址:https://github.com/whuwzp/linuxc/tree/master/epoll_ET_LT_NOBLOCK_example 源码说 ...

  9. AJ学IOS(34)UI之Quartz2D画画板的实现

    AJ分享,必须精品 效果: 实现过程: 首先用storyboard搭建界面,没有什么好说的. 然后就是注意的功能了,这里用了触摸事件来搭配Quartz2D的路径来画画. 思路就是把路径放到数组中 @p ...

  10. PDM添加唯一性约束