Problem - B - Codeforces
Monocarp is a little boy who lives in Byteland and he loves programming.
Recently, he found a permutation of length n. He has to come up with a mystic permutation. It has to be a new permutation such that it differs from the old one in each position.
More formally, if the old permutation is p1,p2,…,pn and the new one is q1,q2,…,qn it must hold that
p1≠q1,p2≠q2,…,pn≠qn.
Monocarp is afraid of lexicographically large permutations. Can you please help him to find the lexicographically minimal mystic permutation?
Input
There are several test cases in the input data. The first line contains a single integer t (1≤t≤200) — the number of test cases. This is followed by the test cases description.
The first line of each test case contains a positive integer n (1≤n≤1000) — the length of the permutation.
The second line of each test case contains n distinct positive integers p1,p2,…,pn (1≤pi≤n). It’s guaranteed that p is a permutation, i. e. pi≠pj for all i≠j.
It is guaranteed that the sum of n does not exceed 1000 over all test cases.
Output
For each test case, output n positive integers — the lexicographically minimal mystic permutations. If such a permutation does not exist, output −1 instead.
Example
input
4
3
1 2 3
5
2 3 4 5 1
4
2 3 1 4
1
1
output
2 3 1
1 2 3 4 5
1 2 4 3
-1
问题解析
题目是说给你一个数组,让你对这个数组重新排序,每个位置上的数不能和之前一样,问这么排字典序最低结果是什么。
先对数组排序,把小的数都放到前面就行。再依次对比看是否和之前一样,不一样就定下来,一样就在后面的数里找不等于之前位置的数并交换。如果后面的数里没有满足条件的,我们就从定下的数里找不同的数交换(交换过去的数也不能和之前一样),如果还是找不到就说明无法满足,输出-1。
AC代码
#include
using namespace std;
#include
#include
#include
#include
#include
#include
#include
#include
#include
Problem - C - Codeforces
Byteland is a beautiful land known because of its beautiful trees.
Misha has found a binary tree with n vertices, numbered from 1 to n. A binary tree is an acyclic connected bidirectional graph containing n vertices and n−1 edges. Each vertex has a degree at most 3, whereas the root is the vertex with the number 1 and it has a degree at most 2.
Unfortunately, the root got infected.
The following process happens n times:
Misha either chooses a non-infected (and not deleted) vertex and deletes it with all edges which have an end in this vertex or just does nothing.
Then, the infection spreads to each vertex that is connected by an edge to an already infected vertex (all already infected vertices remain infected).
As Misha does not have much time to think, please tell him what is the maximum number of vertices he can save from the infection (note that deleted vertices are not counted as saved).
Input
There are several test cases in the input data. The first line contains a single integer t (1≤t≤5000) — the number of test cases. This is followed by the test cases description.
The first line of each test case contains one integer n (2≤n≤3⋅10^ 5) — the number of vertices of the tree.
The i-th of the following n−1 lines in the test case contains two positive integers ui and vi (1≤ui,vi≤n), meaning that there exists an edge between them in the graph.
It is guaranteed that the graph is a binary tree rooted at 1. It is also guaranteed that the sum of n over all test cases won’t exceed 3⋅10^ 5.
Output
For each test case, output the maximum number of vertices Misha can save.
Example
input
4
2
1 2
4
1 2
2 3
2 4
7
1 2
1 5
2 3
2 4
5 6
5 7
15
1 2
2 3
3 4
4 5
4 6
3 7
2 8
1 9
9 10
9 11
10 12
10 13
11 14
11 15
output
0
2
2
10
问题解析
题目是说有一个二叉树,它的根节点被污染了,被污染的节点每次会污染和他直接连接的节点,你每次可以干掉一个没被污染的节点,这样这个节点的子节点就不会被污染了,问你最后可以使得多少多少节点被救下来。
可以先通过dfs一次求的每个节点的子树有多少节点(相当于把这个节点砍掉能拯救的节点数),顺便把每个节点的左右子节点分类好(一开始的链接不会分出左右子树,要我们自己分)。
然后就是计算救下的节点了,每次我们可以选择砍掉左边的节点或砍掉右边的节点,如果砍掉左边节点,那左边的节点我们就能全部救下来,然后右边的子树就会被污染,我们就看右子树的两个子树来决定砍掉哪边的节点,即:
max(dfs(root->left)+size[root->right],dfs(root->right)+size[root->left]);
AC代码
#include
using namespace std;
#include
#include
#include
#include
#include
#include
#include
#include
#include
Problem - D - Codeforces
Lena is a beautiful girl who likes logical puzzles.
As a gift for her birthday, Lena got a matrix puzzle!
The matrix consists of n rows and m columns, and each cell is either black or white. The coordinates (i,j) denote the cell which belongs to the i-th row and j-th column for every 1≤i≤n and 1≤j≤m. To solve the puzzle, Lena has to choose a cell that minimizes the Manhattan distance to the farthest black cell from the chosen cell.
More formally, let there be k≥1 black cells in the matrix with coordinates (xi,yi) for every 1≤i≤k. Lena should choose a cell (a,b) that minimizes
m
a
x
=
(
∣
a
−
x
i
∣
+
∣
b
−
y
i
∣
)
.
max=(|a−xi|+|b−yi|).
max=(∣a−xi∣+∣b−yi∣).
As Lena has no skill, she asked you for help. Will you tell her the optimal cell to choose?
Input
There are several test cases in the input data. The first line contains a single integer t (1≤t≤10000) — the number of test cases. This is followed by the test cases description.
The first line of each test case contains two integers n and m (2≤n,m≤1000) — the dimensions of the matrix.
The following n lines contain m characters each, each character is either ‘W’ or ‘B’. The j-th character in the i-th of these lines is ‘W’ if the cell (i,j) is white, and ‘B’ if the cell (i,j) is black.
It is guaranteed that at least one black cell exists.
It is guaranteed that the sum of n⋅m does not exceed 106.
Output
For each test case, output the optimal cell (a,b) to choose. If multiple answers exist, output any.
Example
input
5
3 2
BW
WW
WB
3 3
WWB
WBW
BWW
2 3
BBB
BBB
5 5
BWBWB
WBWBW
BWBWB
WBWBW
BWBWB
9 9
WWWWWWWWW
WWWWWWWWW
BWWWWWWWW
WWWWWWWWW
WWWWBWWWW
WWWWWWWWW
WWWWWWWWW
WWWWWWWWW
WWWWWWWWB
output
2 1
2 2
1 2
3 3
6 5
问题解析
问题是说,有一个矩阵由白点(W)和黑点(B)组成,让你找到一个点,使得这个点到最远的黑点距离最小。
其实我们就直接找最远的黑点就行了,最远的黑点只有四个,就是左上、坐下、右上、右下这四个方向的点,我们只要找出这四个点,然后枚举所有点到这四个点的最远距离,选最远距离最小的那个点。
AC代码
#include
using namespace std;
#include
#include
#include
#include
#include
#include
#include
#include
#include