## A. Non-zero

• time limit per test: 1 second
• memory limit per test: 256 megabytes

Guy-Manuel and Thomas have an array $a$ of $n$ integers $[a_1,a_2,…,a_n]$. In one step they can add $1$ to any element of the array. Formally, in one step they can choose any integer index $i (1≤i≤n)$ and do $a_i:=a_i+1$.

If either the sum or the product of all elements in the array is equal to zero, Guy-Manuel and Thomas do not mind to do this operation one more time.

What is the minimum number of steps they need to do to make both the sum and the product of all elements in the array different from zero? Formally, find the minimum number of steps to make $a_1+a_2+ … +a_n≠0$ and $a_1⋅a_2⋅ … ⋅a_n≠0$.

### Input

Each test contains multiple test cases.

The first line contains the number of test cases $t (1≤t≤10^3)$. The description of the test cases follows.

The first line of each test case contains an integer $n (1≤n≤100)$ — the size of the array.

The second line of each test case contains $n$ integers $a_1,a_2,…,a_n (−100≤a_i≤100)$ — elements of the array .

### Output

For each test case, output the minimum number of steps required to make both sum and product of all elements in the array different from zero.

4
3
2 -1 -1
4
-1 0 0 1
2
-1 2
3
0 -2 1

1
2
0
2

### Note

In the first test case, the sum is $0$. If we add 1 to the first element, the array will be $[3,−1,−1]$, the sum will be equal to $1$ and the product will be equal to $3$.

In the second test case, both product and sum are $0$. If we add $1$ to the second and the third element, the array will be $[−1,1,1,1]$, the sum will be equal to $2$ and the product will be equal to $−1$. It can be shown that fewer steps can’t be enough.

In the third test case, both sum and product are non-zero, we don’t need to do anything.

In the fourth test case, after adding $1$ twice to the first element the array will be $[2,−2,1]$, the sum will be $1$ and the product will be $−4$.

### Code

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 100 + 5;
int a[MAXN];

int main() {
// freopen("in.txt", "r", stdin);
int T; scanf("%d", &T);
for(int t=1; t<=T; t++) {
int n; scanf("%d", &n);
int ans = 0;
for(int i=0; i<n; i++) scanf("%d", &a[i]);
for(int i=0; i<n; i++) {
if(a[i] == 0) {
ans++;
a[i] = 1;
}
}
int sum = 0;
for(int i=0; i<n; i++) sum += a[i];
if(sum == 0) ans++;
printf("%d\n", ans);
}
return 0;
}

## B. Assigning to Classes

• time limit per test: 2 seconds
• memory limit per test: 256 megabytes

Reminder: the median of the array $[a_1,a_2,…,a_{2k+1}]$ of odd number of elements is defined as follows: let $[b_1,b_2,…,b_{2k+1}]$ be the elements of the array in the sorted order. Then median of this array is equal to $b_{k+1}$.

There are $2n$ students, the $i$-th student has skill level $a_i$. It’s not guaranteed that all skill levels are distinct.

Let’s define skill level of a class as the median of skill levels of students of the class.

As a principal of the school, you would like to assign each student to one of the $2$ classes such that each class has odd number of students (not divisible by $2$). The number of students in the classes may be equal or different, by your choice. Every student has to be assigned to exactly one class. Among such partitions, you want to choose one in which the absolute difference between skill levels of the classes is minimized.

What is the minimum possible absolute difference you can achieve?

### Input

Each test contains multiple test cases. The first line contains the number of test cases $t (1≤t≤104)$. The description of the test cases follows.

The first line of each test case contains a single integer $n (1≤n≤105)$ — the number of students halved.

The second line of each test case contains $2n$ integers $a_1,a_2,…,a_{2n} (1≤a_i≤10^9)$ — skill levels of students.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

### Output

For each test case, output a single integer, the minimum possible absolute difference between skill levels of two classes of odd sizes.

### Example

#### input

3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16

0
1
5

### Note

In the first test, there is only one way to partition students — one in each class. The absolute difference of the skill levels will be $|1−1|=0$.

In the second test, one of the possible partitions is to make the first class of students with skill levels $[6,4,2]$, so that the skill level of the first class will be $4$, and second with $[5,1,3]$, so that the skill level of the second class will be $3$. Absolute difference will be $|4−3|=1$.

Note that you can’t assign like $[2,3]$, $[6,5,4,1]$ or $[]$, $[6,5,4,1,2,3]$ because classes have even number of students.

$[2]$, $[1,3,4]$ is also not possible because students with skills $5$ and $6$ aren’t assigned to a class.

In the third test you can assign the students in the following way: $[3,4,13,13,20]$,$[2,5,8,16,17]$ or $[3,8,17]$,$[2,4,5,13,13,16,20]$. Both divisions give minimal possible absolute difference.

### Code

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5 + 5;
int a[2 * MAXN];

int main() {
// freopen("in.txt", "r", stdin);
int T; scanf("%d", &T);
for(int t=1; t<=T; t++) {
int n; scanf("%d", &n);
for(int i=0; i<2*n; i++) scanf("%d", &a[i]);
sort(a, a + 2 * n);
printf("%d\n", a[n]-a[n-1]);
}
return 0;
}

## C. Anu Has a Function

• time limit per test: 1 second
• memory limit per test: 256 megabytes

Anu has created her own function $f: f(x,y)=(x|y)−y$ where $|$ denotes the bitwise OR operation. For example, $f(11,6)=(11|6)−6=15−6=9$. It can be proved that for any nonnegative numbers $x$ and $y$ value of $f(x,y)$ is also nonnegative.

She would like to research more about this function and has created multiple problems for herself. But she isn’t able to solve all of them and needs your help. Here is one of these problems.

A value of an array $[a_1,a_2,…,a_n]$ is defined as $f(f(…f(f(a_1,a_2),a_3),…a_{n−1}),a_n)$ (see notes). You are given an array with not necessarily distinct elements. How should you reorder its elements so that the value of the array is maximal possible?

### Input

The first line contains a single integer $n$ ($1≤n≤105$).

The second line contains $n$ integers $a_1,a_2,…,a_n$ ($0≤a_i≤10^9$). Elements of the array are not guaranteed to be different.

### Output

Output $n$ integers, the reordering of the array with maximum value. If there are multiple answers, print any.

4
4 0 11 6

11 6 4 0

1
13

13

### Note

In the first testcase, value of the array $[11,6,4,0]$ is $f(f(f(11,6),4),0)=f(f(9,4),0)=f(9,0)=9$.

$[11,4,0,6]$ is also a valid answer.

### Code

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5 + 5;
int a[MAXN];

int main() {
// freopen("in.txt", "r", stdin);
int n; scanf("%d", &n);
for(int i=0; i<n; i++) scanf("%d", &a[i]);
for(int i=30; i>=0; i--) {
int cnt = 0, first = 0;
for(int j=0; j<n; j++) {
int x = ((a[j] >> i) & 1);
if(x == 1) {
first = j;
cnt++;
}
}
if(cnt == 1) {
printf("%d ", a[first]);
for(int i=0; i<n; i++) if(i != first) printf("%d ", a[i]);
return 0;
}
}
for(int i=0; i<n; i++) printf("%d ", a[i]);
return 0;
}

## D. Aerodynamic

• time limit per test: 1 second
• memory limit per test: 256 megabytes
Guy-Manuel and Thomas are going to build a polygon spaceship.

You’re given a strictly convex (i. e. no three points are collinear) polygon $P$ which is defined by coordinates of its vertices. Define $P(x,y)$ as a polygon obtained by translating $P$ by vector $\vec{(x,y)}$. The picture below depicts an example of the translation:

Define $T$ as a set of points which is the union of all $P(x,y)$ such that the origin $(0,0)$ lies in $P(x,y)$ (both strictly inside and on the boundary). There is also an equivalent definition: a point $(x,y)$ lies in T only if there are two points $A$,$B$ in $P$ such that $\vec{AB}=\vec(x,y)$. One can prove $T$ is a polygon too. For example, if $P$ is a regular triangle then $T$ is a regular hexagon. At the picture below $P$ is drawn in black and some $P(x,y)$ which contain the origin are drawn in colored:

The spaceship has the best aerodynamic performance if $P$ and $T$ are similar. Your task is to check whether the polygons $P$ and $T$ are similar.

### Input

The first line of input will contain a single integer $n$ ($3≤n≤105$) — the number of points.

The $i$-th of the next $n$ lines contains two integers $x_i$,$y_i$ ($|x_i|,|y_i|≤10^9$), denoting the coordinates of the $i$-th vertex.

It is guaranteed that these points are listed in counterclockwise order and these points form a strictly convex polygon.

### Output

Output “YES” in a separate line, if $P$ and $T$ are similar. Otherwise, output “NO” in a separate line. You can print each letter in any case (upper or lower).

4
1 0
4 1
3 4
0 3

YES

3
100 86
50 0
150 0

NO

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

YES

### Note

The following image shows the first sample: both $P$ and $T$ are squares. The second sample was shown in the statements.

### Code

#include <bits/stdc++.h>
using namespace std;

const int MAXN = 1e5 + 5;
int x[MAXN], y[MAXN];

int main() {
// freopen("in.txt", "r", stdin);
int n; scanf("%d", &n);
for(int i=0; i<n; i++) scanf("%d %d", &x[i], &y[i]);
if(n & 1) {
printf("NO\n");
return 0;
}
int x0 = x[0] + x[n/2], y0 = y[0] + y[n/2];
for(int i=0; i<n/2; i++) {
if(x[i]+x[n/2+i]!=x0 || y[i]+y[n/2+i]!=y0) {
printf("NO\n");
return 0;
}
}
printf("YES\n");
return 0;
}

## 发送评论编辑评论

|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
（╯‵□′）╯︵┴─┴
￣﹃￣
(/ω＼)
∠( ᐛ 」∠)＿
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ｀)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ(￣∇￣o)
ヾ(´･ ･｀｡)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò｡)
Σ(っ °Д °;)っ
( ,,´･ω･)ﾉ"(´っω･｀｡)
╮(╯▽╰)╭
o(*////▽////*)q
＞﹏＜
( ๑´•ω•) "(ㆆᴗㆆ)

Emoji