A. Sign Flipping \looparrowright

You are given nn integers a1,a2,,ana_1,a_2,\cdots,a_n, where nn is odd. You are allowed to flip the sign of some (possibly all or none) of them. You wish to perform these flips in such a way that the following conditions hold:

  1. At least n12\frac{n−1}{2} of the adjacent differences ai+1aia_{i+1}−a_i for i=1,2,,n1i=1,2,\cdots,n−1 are greater than or equal to 00.
  2. At least n12\frac{n−1}{2} of the adjacent differences ai+1aia_{i+1}−a_i for i=1,2,,n1i=1,2,\cdots,n−1 are less than or equal to 00.
    Find any valid way to flip the signs. It can be shown that under the given constraints, there always exists at least one choice of signs to flip that satisfies the required condition. If there are several solutions, you can find any of them.

Input

The input consists of multiple test cases. The first line contains an integer tt(1t500)(1≤t≤500) — the number of test cases. The description of the test cases follows.
The first line of each test case contains an integer nn (3n993≤n≤99, nn is odd) — the number of integers given to you.
The second line of each test case contains nn integers a1,a2,,ana_1,a_2,\cdots,a_n (109ai109)(−10^9≤a_i≤10^9) — the numbers themselves.
It is guaranteed that the sum of nn over all test cases does not exceed 1000010000.

Output

For each test case, print n integers b1,b2,,bnb_1,b_2,\cdots,b_n, corresponding to the integers after flipping signs. bib_i has to be equal to either aia_i or ai−a_i, and of the adjacent differences bi+1bib_{i+1}−b_i for i=1,,n1i=1,\cdots,n−1, at least n12\frac{n−1}{2} should be non-negative and at least n12\frac{n−1}{2} should be non-positive.
It can be shown that under the given constraints, there always exists at least one choice of signs to flip that satisfies the required condition. If there are several solutions, you can find any of them.

Example

input

5
3
-2 4 3
5
1 1 1 1 1
5
-2 4 7 -6 4
9
9 7 -4 -2 1 -3 9 -4 -5
9
-4 1 9 4 8 9 5 1 -9

output

-2 -4 3
1 1 1 1 1
-2 -4 7 -6 4
-9 -7 -4 2 1 -3 -9 -4 -5
4 -1 -9 -4 -8 -9 -5 -1 9

Note

In the first test case, the difference (4)(2)=2(−4)−(−2)=−2 is non-positive, while the difference 3(4)=73−(−4)=7 is non-negative.
In the second test case, we don’t have to flip any signs. All 44 differences are equal to 00, which is both non-positive and non-negative.
In the third test case, 7(4)7−(−4) and 4(6)4−(−6) are non-negative, while (4)(2)(−4)−(−2) and (6)7(−6)−7 are non-positive.

Idea

智商测试题。类似这样的构造问题,样例会误导选手,需要自己思考。
事实上,nn 个数正负交错排列,即可满足条件。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<cmath>
#include<cstdio>
using namespace std;
int main()
{
int _;
for(cin>>_;_;_--)
{
int n;
scanf("%d",&n);
int i;
for(i=1;i<=n;i++)
{
int x;
scanf("%d",&x);
//交错排列
printf("%d ",i&1?-abs(x):abs(x));
}
putchar('\n');
}
return 0;
}

B. Neighbor Grid \looparrowright

You are given a grid with nn rows and mm columns, where each cell has a non-negative integer written on it. We say the grid is good if for each cell the following condition holds: if it has a number k>0k>0 written on it, then exactly kk of its neighboring cells have a number greater than 00 written on them. Note that if the number in the cell is 00, there is no such restriction on neighboring cells.
You are allowed to take any number in the grid and increase it by 11. You may apply this operation as many times as you want, to any numbers you want. Perform some operations (possibly zero) to make the grid good, or say that it is impossible. If there are multiple possible answers, you may find any of them.
Two cells are considered to be neighboring if they have a common edge.

Input

The input consists of multiple test cases. The first line contains an integer tt (1t5000)(1≤t≤5000) — the number of test cases. The description of the test cases follows.
The first line of each test case contains two integers nn and mm (2n,m300)(2≤n,m≤300) — the number of rows and columns, respectively.
The following nn lines contain mm integers each, the jj-th element in the ii-th line ai,ja_{i,j} is the number written in the jj-th cell of the ii-th row (0ai,j109)(0≤a_{i,j}≤10^9).
It is guaranteed that the sum of n×mn\times m over all test cases does not exceed 10510^5.

Output

If it is impossible to obtain a good grid, print a single line containing “NO”.
Otherwise, print a single line containing “YES”, followed by nn lines each containing mm integers, which describe the final state of the grid. This final grid should be obtainable from the initial one by applying some operations (possibly zero).
If there are multiple possible answers, you may print any of them.

Example

input

5
3 4
0 0 0 0
0 1 0 0
0 0 0 0
2 2
3 0
0 0
2 2
0 0
0 0
2 3
0 0 0
0 4 0
4 4
0 0 0 0
0 2 0 1
0 0 0 0
0 0 0 0

output

YES
0 0 0 0
0 1 1 0
0 0 0 0
NO
YES
0 0
0 0
NO
YES
0 1 0 0
1 4 2 1
0 2 0 0
1 3 1 0

Note

In the first test case, we can obtain the resulting grid by increasing the number in row 22, column 33 once. Both of the cells that contain 11 have exactly one neighbor that is greater than zero, so the grid is good. Many other solutions exist, such as the grid $\begin{pmatrix}0&1&0&0\\0&2&1&0\\0&0&0&0\end{pmatrix}$, all of them are accepted as valid answers.
In the second test case, it is impossible to make the grid good.
In the third test case, notice that no cell has a number greater than zero on it, so the grid is automatically good.

Idea

对于一个矩阵,其四个角上的元素只有两个相邻元素,边界上(除四个顶点)的元素有三个相邻元素,其余位置上的元素都有四个相邻元素。因此,形如 $EX=\begin{pmatrix}2&3&3&3&2\\3&4&4&4&3\\2&3&3&3&2\end{pmatrix}$ 的矩阵都是满足条件的 good gird
给定 n,mn,m,可以构造形如 EXEX 的矩阵,称该矩阵为 basebase;其四个顶点元素的值为 22,边界上(除四个顶点)的元素值为 33,其余位置元素的值为 44basebase 内各个元素的值不能更大,否则不满足形成 good gird 的条件。因此,若读入的矩阵中 $a_{i,j}>base_{i,j} $,那么就无法经过操作得到 good gird;否则,增大 ai,ja_{i,j} 使得 ai,j=basei,ja_{i,j}=base_{i,j}

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
#include<cstdio>
#define N 302
using namespace std;
short base[N][N];
int n,m;
int main()
{
int _;
for(cin>>_;_;_--)
{
int n;
scanf("%d%d",&n,&m);
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
//------------------------------------------------------------------------
//顶点为2
//边界为3
//中部为4
if((i==1&&j==1)||(i==1&&j==m)||(i==n&&j==1)||(i==n&&j==m)) base[i][j]=2;
else if(i==1||i==n||j==1||j==m) base[i][j]=3;
else base[i][j]=4;
//------------------------------------------------------------------------
}
}
bool ok=1;
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
int x;
scanf("%d",&x);
if(x>base[i][j]) ok=0;
}
}
if(ok)
{
puts("YES");
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
printf("%hd ",base[i][j]);
}
putchar('\n');
}
}
else puts("NO");
}
return 0;
}

Postscript

注意数据范围,读入矩阵时不能用 short\text{short} 类型。

C. Element Extermination \looparrowright

You are given an array a of length nn, which initially is a permutation of numbers from 11 to nn. In one operation, you can choose an index ii (1i<n)(1≤i<n) such that ai<ai+1a_i<a_{i+1}, and remove either aia_i or ai+1a_{i+1} from the array (after the removal, the remaining parts are concatenated).
For example, if you have the array [1,3,2][1,3,2], you can choose i=1i=1 (since a1=1<a2=3a_1=1<a_2=3), then either remove a1a_1 which gives the new array [3,2][3,2], or remove a2a_2 which gives the new array [1,2][1,2].
Is it possible to make the length of this array equal to 11 with these operations?

Input

The first line contains a single integer tt (1t2×104)(1≤t≤2\times 10^4) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer nn (2n3×105)(2≤n≤3\times 10^5) — the length of the array.
The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \cdots, a_n (1ain1≤a_i≤n, aia_i are pairwise distinct) — elements of the array.
It is guaranteed that the sum of nn over all test cases doesn’t exceed 3×1053\times 10^5.

Output

For each test case, output on a single line the word “YES” if it is possible to reduce the array to a single element using the aforementioned operation, or “NO” if it is impossible to do so.

Example

input

4
3
1 2 3
4
3 1 2 4
3
2 3 1
6
2 4 6 1 3 5

output

YES
YES
NO
YES

Note

For the first two test cases and the fourth test case, we can operate as follow (the bolded elements are the pair chosen for that operation):
[1,2,3][1,2][1][1,2,3]→[1,2]→[1]
[3,1,2,4][3,1,4][3,4][4][3,1,2,4]→[3,1,4]→[3,4]→[4]
[2,4,6,1,3,5][4,6,1,3,5][4,1,3,5][4,1,5][4,5][4][2,4,6,1,3,5]→[4,6,1,3,5]→[4,1,3,5]→[4,1,5]→[4,5]→[4].

Idea

可行的情况是 a1<ana_1<a_n。下面给出证明。
a1<ana_1<a_n。首先,从左向右找到距离 a1a_1 最近的元素 ara_r,使得 ar>a1a_r>a_1;那么 a1ara_1\sim a_r 之间的元素都小于 ara_r,可删除。如此一来 a1a_1 就与 ara_r 相邻,可以将 ara_r 删除。重复上述步骤,最终 a1a_1 就会与 ana_n 相邻,最后将 ana_n 删除留下 a1a_1
a1ana_1\ge a_n。注意到,若要删除 a1a_1,需要找到 ar>a1a_r>a_1,那么序列最左端的值是递增的,因此序列最左端元素始终大于 ana_n,不能将序列长度变为 11;若要删除 ana_n,需要找到 ar<ana_r<a_n,那么序列最右端的值是递减的,因此序列最后端元素始终小于 a1a_1,不能将长度变为 11

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<cstdio>
#include<iostream>
#define N 300002
using namespace std;
int a[N];
int n;
int main()
{
int _;
for(cin>>_;_;_--)
{
scanf("%d",&n);
int i;
for(i=1;i<=n;i++) scanf("%d",a+i);
puts(a[1]<a[n]?"YES":"NO");
}
return 0;
}

D. Replace by MEX \looparrowright

You’re given an array of nn integers between 00 and nn inclusive.
In one operation, you can choose any element of the array and replace it by the MEX of the elements of the array (which may change after the operation).
For example, if the current array is [0,2,2,1,4][0,2,2,1,4], you can choose the second element and replace it by the MEX of the present elements — 33. Array will become [0,3,2,1,4][0,3,2,1,4].
You must make the array non-decreasing, using at most 2n2n operations.
It can be proven that it is always possible. Please note that you do not have to minimize the number of operations. If there are many solutions, you can print any of them.
An array b[1n]b[1\cdots n] is non-decreasing if and only if b1b2bnb_1≤b_2≤\cdots≤b_n.
The MEX (minimum excluded) of an array is the smallest non-negative integer that does not belong to the array. For instance:

  • The MEX of [2,2,1][2,2,1] is 00, because 00 does not belong to the array.
  • The MEX of [3,1,0,1][3,1,0,1] is 22, because 00 and 11 belong to the array, but 22 does not.
  • The MEX of [0,3,1,2][0,3,1,2] is 44 because 0,1,20, 1, 2 and 33 belong to the array, but 44 does not.

It’s worth mentioning that the MEX of an array of length nn is always between 00 and nn inclusive.

Input

The first line contains a single integer tt (1t200)(1≤t≤200) — the number of test cases. The description of the test cases follows.
The first line of each test case contains a single integer nn (3n1000)(3≤n≤1000) — length of the array.
The second line of each test case contains nn integers a1,,ana_1,\cdots,a_n (0ain)(0≤a_i≤n) — elements of the array. Note that they don’t have to be distinct.
It is guaranteed that the sum of nn over all test cases doesn’t exceed 10001000.

Output

For each test case, you must output two lines:
The first line must contain a single integer kk (0k2n)(0≤k≤2n) — the number of operations you perform.
The second line must contain kk integers x1,,xkx_1,\cdots,x_k (1xin)(1≤x_i≤n), where xix_i is the index chosen for the ii-th operation.
If there are many solutions, you can find any of them. Please remember that it is not required to minimize kk.

Example

input

5
3
2 2 3
3
2 1 0
7
0 7 3 1 3 7 7
9
2 0 1 1 2 4 4 2 0
9
8 4 7 6 1 2 3 0 5

output

0
2
3 1
4
2 5 5 4
11
3 8 9 7 8 5 9 6 4 1 2
10
1 8 1 9 5 2 4 6 3 7

Note

In the first test case, the array is already non-decreasing (223)(2≤2≤3).
Explanation of the second test case (the element modified by each operation is colored in red):
a=[2,1,0]a=[2,1,0] ; the initial MEX is 33.
a=[2,1,3]a=[2,1,{\color {red} 3}] ; the new MEX is 00.
a=[0,1,3]a = [{\color{red}{0}}, 1, 3] ; the new MEX is 22.
The final array is non-decreasing: 0130≤1≤3.
Explanation of the third test case:
a=[0,7,3,1,3,7,7]a=[0,7,3,1,3,7,7] ; the initial MEX is 22.
a=[0,2,3,1,3,7,7]a=[0,{\color{red}2},3,1,3,7,7] ; the new MEX is 44.
a=[0,2,3,1,4,7,7]a=[0,2,3,1,{\color{red}4},7,7] ; the new MEX is 55.
a=[0,2,3,1,5,7,7]a=[0,2,3,1,{\color{red}{5}},7,7] ; the new MEX is 44.
a=[0,2,3,4,5,7,7]a=[0,2,3,{\color{red}{4}},5,7,7] ; the new MEX is 11.
The final array is non-decreasing: 02345770≤2≤3≤4≤5≤7≤7.

Idea

考虑到长度为 nn 的序列,0mexn0\le \mathrm {mex}\le n,可以将原数组替换成 [1,2,,n][1,2,\cdots,n]
序列 [1,2,,n][1,2,\cdots,n] 的特征是 ai=ia_i=i。若当前数组 $a $ 的 mex>0\mathrm{mex}>0,直接替换:amex=mexa_{\mathrm{mex}}=\mathrm{mex}。 若 mex=0\mathrm{mex}=0,找一个位置 ii,满足 aiia_i\ne i,令 ai=0a_i=0,再求一次 mex\mathrm{mex},此时 mex\mathrm{mex} 必然大于 00,直接替换 amex=mexa_{\mathrm{mex}}=\mathrm{mex}
不满足 ai=ia_i=i 的位置最多有 nn 个,要让 amex=mexa_{\mathrm{mex}}=\mathrm{mex} 至多要进行两次替换,因此操作次数不会超过 nn 次。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#define N 1003
using namespace std;
int a[N];
int n;
vector<int>ans;
bool vis[N];
int cal_mex()
{
//计算mex
memset(vis,0,sizeof(vis));
int i;
for(i=1;i<=n;i++) vis[a[i]]=1;
for(i=0;i<=n;i++) if(!vis[i]) return i;
}
int main()
{
int _;
for(cin>>_;_;_--)
{
scanf("%d",&n);
int i;
ans.clear();
for(i=1;i<=n;i++) scanf("%d",a+i);
while(true)
{
bool ok=1;
for(i=1;i<=n;i++)
{
if(a[i]!=i)
{
ok=0;
break;
}
}
if(ok) break;
int mex=cal_mex();
if(!mex)//mex=0,算两次
{
for(i=1;i<=n;i++)
{
if(a[i]!=i)
{
a[i]=0;
ans.push_back(i);
break;
}
}
mex=cal_mex();
a[mex]=mex;
ans.push_back(mex);
}
else//mex>0直接替换
{
a[mex]=mex;
ans.push_back(mex);
}
}
printf("%d\n",ans.size());
for(auto v:ans) printf("%d ",v);
putchar('\n');
}
return 0;
}

E. Inversion Swap Sort \looparrowright

Madeline has an array aa of nn integers. A pair (u,v)(u,v) of integers forms an inversion in aa if: 1u<vn1≤u<v≤n, au>ava_u>a_v.
Madeline recently found a magical paper, which allows her to write two indices uu and vv and swap the values aua_u and ava_v. Being bored, she decided to write a list of pairs (ui,vi)(u_i,v_i) with the following conditions: all the pairs in the list are distinct and form an inversion in aa, all the pairs that form an inversion in aa are in the list.
Starting from the given array, if you swap the values at indices u1u_1 and v1v_1, then the values at indices u2u_2 and v2v_2 and so on, then after all pairs are processed, the array aa will be sorted in non-decreasing order.
Construct such a list or determine that no such list exists. If there are multiple possible answers, you may find any of them.

Input

The first line of the input contains a single integer nn (1n1000)(1≤n≤1000) — the length of the array.
Next line contains nn integers a1,a2,,ana_1,a_2,\cdots,a_n (1ai109)(1≤a_i≤10^9) — elements of the array.

Output

Print 1-1 if no such list exists. Otherwise in the first line you should print a single integer mm (0mn(n1)2)\left(0≤m≤\frac{n(n−1)}{2}\right) — number of pairs in the list.
The ii-th of the following mm lines should contain two integers ui,viu_i,v_i (1ui<vin)(1≤u_i<v_i≤n).
If there are multiple possible answers, you may find any of them.

Examples

input

3
3 1 2

output

2
1 3
1 2

input

4
1 8 1 6

output

2
2 4
2 3

input

5
1 1 1 2 2

output

0

Note

In the first sample test case the array will change in this order [3,1,2][3,1,2][2,1,3][2,1,3][1,2,3][1,2,3].
In the second sample test case it will be [1,8,1,6][1,6,1,8][1,1,6,8][1,8,1,6]→[1,6,1,8]→[1,1,6,8].
In the third sample test case the array is already sorted.

Idea

首先考虑 aa1n1\sim n 全排列时的做法。
posxpos_x 表示 xx 这个数在 aa 中出现的位置。
从边界入手,先尝试把 nn 放到排列的最后一个位置,然后转化为规模减一的子问题。假设操作后,我们得到的排列为 bb,要求 bb 满足:bn=nb_n=n 1i,j<n∀\ 1≤i,j<n,若 ai<aja_i<a_j,则 bi<bjb_i<b_j1i,j<n∀ 1≤i,j<n,若 ai>aja_i>a_j,则 bi>bjb_i>b_j。也就是说,要保证前面的数的相对大小关系不变,这样才能转化为一个等价的子问题。我们可以依次交换索引对应的值:(posan+1,n)(pos_{a_n+1},n),,(posan+2,n)(pos_{a_n+2},n),,(posan+3,n)(pos_{a_n+3},n),,\cdots,,(posn,n)(pos_n,n)。举例,操作 [1,5,3,4,2][1,5,3,4,2] 的过程:[1,5,3,4,2][1,5,3,4,2]\rightarrow[1,5,2,4,3][1,5,2,4,3]\rightarrow[1,5,2,3,4][1,5,2,3,4]\rightarrow[1,4,2,3,5][1,4,2,3,5]。容易发现,这样一轮操作完成后,nn 被放到了最后;同时,相当于前面所有大于 ana_n的数,全部减去 11;因此,显然前面所有数的相对大小关系不变,剩下的是一个规模更小的子问题。接着以索引 n1n-1 的位置为末尾,将数值 n1n-1 放到最后。不断重复,即可得到一个标准排列。
基于以上,当 aa 是一个全排列时,一定有解。
现在考虑 aa 不是全排列的时候。事实上,可以设置数值为第一关键字,位置为第二关键字,强行转成一个全排列。举例,a=[1,4,3,5,3,1]a=[1,4,3,5,3,1],重新赋值转换为全排列后 a=[1,5,3,6,4,2]a=[1,5,3,6,4,2]。发现转成全排列后,序列里的逆序对个数和原来是一样的。可以进行略证:a,a,ca,a,c(1,3)(1,3) 是逆序对,重新赋值后为 2,3,12,3,1,由于对于,相同数字,位置靠后的重新赋值后数值也更大,不会产生逆序对,所以逆序对个数是和原来一样的。
综上所述,对于任意的数组 aa,将 aa 经过转换后直接按全排列求解即可。对于一个全排列,总的交换次数至多为 (n1)+(n2)++1=n(n1)2(n-1)+(n-2)+\cdots+1=\frac{n(n-1)}{2},满足题目限制条件。
时间复杂度 O(n2)O(n^2)

Postscript

智商有待提高 Orz\mathrm{Orz}

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<map>
#include<utility>
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#define N 1003
using namespace std;
pair<int,int>a[N];//按照(位置,数值)排序
map<pair<int,int>,int>temp;//记录排序后(位置,数值)的序号
int num[N];//初始数组
int permutation[N];//重新赋值得到的全排列
int n;
vector<pair<int,int>>ans;//记录答案
int pos[N];//存数值的位置
int main()
{
cin>>n;
int i,j;
for(i=1;i<=n;i++) scanf("%d",num+i);
//----------------------------------------------------------------
//数值为第一关键字
//位置为第二关键字
for(i=1;i<=n;i++)
{
a[i].second=num[i];
a[i].first=i;
}
//first 位置
//second 数值
//排序
sort(a+1,a+1+n,[](const pair<int,int>& x,const pair<int,int>& y){
if(x.second==y.second) return x.first<y.first;
else return x.second<y.second;});
//-----------------------------------------------------------------
//-----------------------------------------------------------------
//得到(位置,数值)排序后的序号
for(i=1;i<=n;i++) temp[make_pair(a[i].first,a[i].second)]=i;
//初始序列中位置为i,数值为num[i]
//重新赋值,形成全排列
for(i=1;i<=n;i++) permutation[i]=temp[make_pair(i,num[i])];
//-----------------------------------------------------------------
//-----------------------------------------------------------------
for(i=n;i>=1;i--)
{
//记录[1,i]各个数值的位置
for(j=1;j<=i;j++) pos[permutation[j]]=j;
//swap(permutation[pos[permutation[i]+1]],permutation[i])
//swap(permutation[pos[permutation[i]+2]],permutation[i])
//...........
//swap(permutation[pos[i]],permutation[i])
for(j=permutation[i]+1;j<=i;j++)
{
swap(permutation[i],permutation[pos[j]]);
ans.push_back(make_pair(pos[j],i));//记录答案
}
}
//-----------------------------------------------------------------
cout<<ans.size()<<endl;
for(auto v:ans) printf("%d %d\n",v.first,v.second);
return 0;
}