For a permutation p1,p2,…,pN of 1…N (1≤N≤2⋅105), let f(p)=. A permutation is good if it can be turned into the identity permutation using at most f(p) swaps of adjacent elements.
Given a permutation, find which cyclic shifts of it are good.
INPUT FORMAT (input arrives from the terminal / stdin):
The input consists of T (1≤T≤105) independent tests. Each test is specified as follows:
The first line contains N.
The second line contains p1,p2,…,pN (1≤pi≤N), which is guaranteed to be a permutation of 1…N.
It is guaranteed that the sum of N over all tests does not exceed 106.
OUTPUT FORMAT (print output to the terminal / stdout):
For each test, output two lines:
On the first line, output the number of good cyclic shifts k.
Then output a line with k space-separated integers s (0≤s<N) in increasing order, meaning that p is good when cyclically shifted to the right s times.
SAMPLE INPUT:
3
5
5 4 3 2 1
4
1 2 4 3
5
1 2 3 4 5
SAMPLE OUTPUT:
0
2
0 1
5
0 1 2 3 4
Consider the second test case, where p=[1,2,4,3].
f(p)=(|1−1|+|2−2|+|4−3|+|3−4|)/2=1. Since p can be turned into the identity permutation in one move by swapping p3and p4, p is good.
Cyclically shifting p to the right 1 time, we get q=[3,1,2,4]. f(q)(|3−1|+|1−2|+|2−3|+|4−4|)/2=2. Since q can be turned into the identity permutation in two moves by swapping q1 two steps forward, q is good.
It can be seen that the other two cyclic shifts are not good.
SCORING:
Input 2: N≤10
Inputs 3-5: T≤10,N≤2000
Inputs 6-11: No additional constraints.
Problem credits: Akshaj Arora
You are given integers N,Q (1≤N,Q≤2⋅105) and Q constraints represented by four integers ti,li,ri,ki (1≤ti≤2,1≤li≤ri≤N,0≤ki≤109,all ki are distinct).
Construct an array a consisting of N integers between 0 and 109 such that for all 1≤i≤Q, mina[li...ri]=ki if ti=1 and maxa[li...ri]=ki if ti=2. If multiple valid arrays exist, print any. If no valid array exists, print −1.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains an integer T (1≤T≤104) representing the number of independent test cases.
For each test case, the first line contains two integers N,Q.
The next Q lines each contain 4 integers ti li ri ki.
It is guaranteed that neither the sum of N nor the sum of Q over all test cases exceed 2⋅105.
OUTPUT FORMAT (print output to the terminal / stdout):
For each test case, if a valid array exists, output N space-separated integers a1…aN on a new line. Otherwise, print −1.
In the first test case, the answer is −1 because the minimum value of the array cannot be both 1 and 2 at the same time.
In the second test case, a[1...2] has a minimum of 1 at a[1] in the sample output, satisfying the first constraint. Since a[2]=2, the second constraint is also satisfied.
In the third test case, there are multiple solutions. For instance, the array [4,3,2,1]
would also be accepted.
In the second test case, the array [3,5,1] satisfies the first constraint but not the second constraint. Contrarily, the array [3,1,1] satisfies the second constraint but not the first constraint. It can be proven that no array can satisfy both constraints at the same time, hence the answer is −1.
For all other test cases, it can be proven that the constructed array satisfies all Q
constraints.
SCORING:
Inputs 3-4: N,Q≤100 and all ti within the same test case are equal
Inputs 5-6: all ti within the same test case are equal
Inputs 7-10: N,Q≤100
Inputs 11-14: No additional constraints.
Bessie is playing a video game where she needs to defeat a line of N enemies with initial HPs given by the list v1…vN (1≤N≤2⋅105,0≤vi≤109). In one attack, she can perform the following sequence of steps:
Choose i such that the ith enemy is still alive (that is, vi>0).
Deal one damage to the ith enemy and all enemies adjacent to it that are still alive. Specifically, for each j∈[max(i−1,1),min(i+1,N)], if vj>0, subtract one from vj.
Help Bessie determine the minimum number of attacks she needs to defeat all enemies (that is, reduce all vi to 0).
Additionally, you are given a parameter M (0≤M≤2). If M>0, please output a construction achieving the minimum number of attacks with a small number of runs, where a run consists of attacking the same enemy consecutively.
Let R be the number of runs in your construction. Your construction should be in the following format: output R on its own line, followed by R lines each containing two integers i and r (1≤i≤N,0≤r≤109), meaning that Bessie attacks the i-th enemy r times consecutively.
Depending on the value of M, R must satisfy one of the following constraints:
M=1: R≤2N(it can be proven that a construction always exists).
M=2: R≤f(N), where f(N) is the maximum minimum number of runs over all lists of length N.
INPUT FORMAT (input arrives from the terminal / stdin):
Each input consists of T (1≤T≤105) independent tests. The first line contains T
and M.
Each test is specified as follows:
The first line contains N.
The second line contains v1…vN.
It is guaranteed that the sum of N over all tests does not exceed 106.
OUTPUT FORMAT (print output to the terminal / stdout):
For each test, output the minimum number of attacks on the first line.
Then if M>0, output R+1 additional lines as specified above. Any valid construction will be accepted.
SAMPLE INPUT:
2 0
1
10
3
6 1 7
SAMPLE OUTPUT:
10
12
For the second test, you can first perform one attack on the middle enemy. Then, in any order after that, perform five attacks on the first enemy and six attacks on the last enemy.
SAMPLE INPUT:
2 1
1
10
3
6 1 7
SAMPLE OUTPUT:
10
2
1 0
1 10
12
4
2 1
1 5
3 2
3 4
This output receives credit because R=2≤2 for test 1 and R=4≤6 for test 2.
SAMPLE INPUT:
2 2
1
10
3
6 1 7
SAMPLE OUTPUT:
10
1
1 10
12
3
2 1
3 6
1 5
This output receives credit because R=1≤f(1) for test 1 and R=3≤f(3) for test 2.
SCORING:
Inputs 4-7: M=0
Inputs 8-11: M=1
Input 12-13: M=2
Problem credits: Benjamin Qi
You have a bunch of triangular regions that tessellate an infinite 2D plane. The tesselation is defined as follows (see the diagram for a better understanding):
Recall that Euler's formula states that eix=cos(x)+isin(x) for real x. First, draw a vertex at x+yexp(πi/3)on the complex plane for all integers x,y.
Then for every three vertices from the step above forming an equilateral triangle with side length 1, draw the edges forming its border. Additionally, draw a vertex at the center of the triangle and edges from the center of the triangle to each of three outer vertices.
You are given N (2≤N≤2⋅105) input points, each of which lies strictly within some region (i.e., not on any vertex or edge). For any pair of the input points, define the distance between them to be the smallest number of edges crossed when drawing a path from one point to the other that doesn't pass through any vertex.
Output the sum of all N(N−1)/2 pairwise distances between the input points.
INPUT FORMAT (input arrives from the terminal / stdin):
The first line contains T(T≥1), the number of independent tests. Each test is specified as follows:
The first line contains N.
The next N lines each contain three integers x, y, and z(0≤x,y<106,0≤z<12) representing a point at x+yexp(πi/3)+ϵ⋅exp((1+2z)πi/12) on the complex plane (ϵ
being a small positive number).
It is guaranteed that the sum of N over all tests doesn't exceed 2⋅105.
OUTPUT FORMAT (print output to the terminal / stdout):
For each test, output the sum of all N(N−1)/2 pairwise distances on a new line.