There is an by square stamp on an infinite piece of paper. Initially, the bottom-left corner of the square stamp is aligned with the bottom-left corner of the paper. You are given two integer sequences and , each of length . For each step from to , the following happens:
Move the stamp units to the right and units upwards.
Press the stamp onto the paper, leaving an by colored square at its current position.
Note that the elements of sequences and have a special constraint: .
Note that you do not press the stamp at the bottom-left corner of the paper. Refer to the notes section for better understanding.
It can be proven that after all the operations, the colored shape on the paper formed by the stamp is a single connected region. Find the perimeter of this colored shape.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains two integers and (, ) — the number of operations performed and the side length of the square stamp.
The -th of the next lines contains two integers and () — the distance that the stamp will be moved right and up during the -th operation, respectively.
Note that there are no constraints on the sum of over all test cases.
Output
For each test case, output a single integer representing the perimeter of the colored shape on the paper.
In the first example, the stamp has a side length of and is pressed times at coordinates , , , and . The piece of paper looks like that afterwards:
Here, the square formed by the first press is colored blue, the second red, the third green, and the fourth purple. The combined shape, whose perimeter we need to calculate, looks like that:
From the diagram, it can be seen that this shape has a perimeter of .
Ans
1 2 3 4 5 6 7 8 9 10 11
voidsolve(){ int n,m; cin >> n >> m; ll res = 0; for(int i = 0;i < n;++i){ int x,y; cin >> x >> y; if(i == 0) res = 4*m; else res += 2*m+x+y-(m-x)-(m-y); } cout << res << '\n'; return; }
B. Find the Permutation(1300)
(brute force)(dfs and similar)(graphs)(implementation)(sortings)
Ques
题意
You are given an undirected graph with vertices, labeled from to . This graph encodes a hidden permutation of size . The graph is constructed as follows:
For every pair of integers , an undirected edge is added between vertex and vertex if and only if . Note that the edge is not added between vertices and , but between the vertices of their respective elements. Refer to the notes section for better understanding.
Your task is to reconstruct and output the permutation . It can be proven that permutation can be uniquely determined.
A permutation of length is an array consisting of distinct integers from to in arbitrary order. For example, is a permutation, but is not a permutation ( appears twice in the array), and is also not a permutation ( but there is in the array).
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains a single integer ().
The -th of the next lines contains a string of characters $g{i, 1}g{i, 2}\ldots g{i, n}g{i, j} = \mathtt{0}g{i, j} = \mathtt{1}g{i, j} = \mathtt{1}ij$.
It is guaranteed that there exists a permutation which generates the given graph. It is also guaranteed that the graph is undirected and has no self-loops, meaning $g{i, j} = g{j, i}g_{i, i} = \mathtt{0}$.
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, output integers representing the reconstructed permutation.
In the first case . Since there are no pairs , there are no edges in the graph.
The graph in the second case is shown below. For example, when we choose and , we add an edge between vertices and , because . However, when we choose and , and , so doesn’t hold. Therefore, we don’t add an edge between and .
In the third case, there are no edges in the graph, so there are no pairs of integers such that . Therefore, .
Ans
初始思路
经过我的不断推敲,发现一条可行的思路,把这个矩阵沿着主对角线切开,对角线以右表示这个点的右边有谁。
题解
法一
对于两个数字 i < j ,若g[i][j] = 1,表示 i 在 j 左边,若g[i][j] = 0,表示 j 在 i 左边。i > j 同理。
For an integer sequence , we define as the length of the longest subsequence of that is a palindrome.
Let represent the number of subsequences of length that are palindromes. In other words, counts the number of palindromic subsequences in that have the maximum length.
Given an integer , your task is to find any sequence of integers that satisfies the following conditions:
for all .
It can be proven that such a sequence always exists under the given constraints.
A sequence is a subsequence of a sequence if can be obtained from by the deletion of several (possibly, zero or all) element from arbitrary positions.
A palindrome is a sequence that reads the same from left to right as from right to left. For example, , , and are palindromes, while and are not.
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
The first line of each test case contains a single integer () — the length of the sequence.
Note that there are no constraints on the sum of over all test cases.
Output
For each test case, output integers , representing an array that satisfies the conditions.
If there are multiple solutions, you may output any of them.
In the first example, one possible solution is . In this case, as the longest palindromic subsequence has length . There are ways to choose a subsequence of length that is a palindrome, as shown below:
Therefore, , which is greater than . Hence, is a valid solution.
In the second example, one possible solution is . In this case, . There are ways to choose a subsequence of length that is a palindrome. Some examples are and . Therefore, , which is greater than . Hence, is a valid solution.
In the third example, and , which is greater than .