Codeforces Round #618 (Div. 2)全解

A. Non-zero

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Guy-Manuel and Thomas have an array aa of nn integers [a1,a2,…,ana1,a2,…,an]. In one step they can add 11 to any element of the array. Formally, in one step they can choose any integer index ii (1≤i≤n1≤i≤n) and do ai:=ai+1ai:=ai+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 a1+a2+a1+a2+ …… +an≠0+an≠0 and a1⋅a2⋅a1⋅a2⋅ …… ⋅an≠0⋅an≠0.

Input

Each test contains multiple test cases.

The first line contains the number of test cases tt (1≤t≤1031≤t≤103). The description of the test cases follows.

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

The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−100≤ai≤100−100≤ai≤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.

Example

input

Copy

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

output

Copy

1
2
0
2

Note

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

In the second test case, both product and sum are 00. If we add 11 to the second and the third element, the array will be [−1,1,1,1][−1,1,1,1], the sum will be equal to 22 and the product will be equal to −1−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 11 twice to the first element the array will be [2,−2,1][2,−2,1], the sum will be 11 and the product will be −4−4.

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
const int mod = 1e9 + 7;
int te, n, m;
int ar[N];
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int te;
    cin >> te;
    while (te--) {
        cin >> n;
        int sum = 0, ans = 0, zsum = 0;
        for (int i = 1; i <= n; i++) {
            cin >> ar[i];
            sum += ar[i];
            if (ar[i] == 0) {
                ans++, sum++;
            }
        }
        if (sum == 0)ans++;
        cout << ans << "\n";
    }
    return 0;
}

B. Assigning to Classes

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Reminder: the median of the array [a1,a2,…,a2k+1][a1,a2,…,a2k+1] of odd number of elements is defined as follows: let [b1,b2,…,b2k+1][b1,b2,…,b2k+1] be the elements of the array in the sorted order. Then median of this array is equal to bk+1bk+1.

There are 2n2n students, the ii-th student has skill level aiai. 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 22 classes such that each class has odd number of students (not divisible by 22). 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 tt (1≤t≤1041≤t≤104). The description of the test cases follows.

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

The second line of each test case contains 2n2n integers a1,a2,…,a2na1,a2,…,a2n (1≤ai≤1091≤ai≤109) — skill levels of students.

It is guaranteed that the sum of nn over all test cases does not exceed 105105.

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

Copy

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

output

Copy

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|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][6,4,2], so that the skill level of the first class will be 44, and second with [5,1,3][5,1,3], so that the skill level of the second class will be 33. Absolute difference will be |4−3|=1|4−3|=1.

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

[2][2], [1,3,4][1,3,4] is also not possible because students with skills 55 and 66 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][3,4,13,13,20],[2,5,8,16,17] or [3,8,17],[2,4,5,13,13,16,20][3,8,17],[2,4,5,13,13,16,20]. Both divisions give minimal possible absolute difference.

    直接中位数相减即可。

#include <bits/stdc++.h>
using namespace std;
const int N = 5e5 + 10;
const int mod = 1e9 + 7;
int te, n, m;
int ar[N];
int main() {
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int te;
    cin >> te;
    while (te--) {
        cin >> n;
        n *= 2;
        int sum = 0, ans = 0, zsum = 0;
        for (int i = 1; i <= n; i++) {
            cin >> ar[i];
        }
        sort(ar + 1, ar + n + 1);
        cout << ar[n / 2 + 1] - ar[n / 2] << "\n";
    }
    return 0;
}

C. Anu Has a Function

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Anu has created her own function ff: f(x,y)=(x|y)−yf(x,y)=(x|y)−y where || denotes the bitwise OR operation. For example, f(11,6)=(11|6)−6=15−6=9f(11,6)=(11|6)−6=15−6=9. It can be proved that for any nonnegative numbers xx and yy value of f(x,y)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 [a1,a2,…,an][a1,a2,…,an] is defined as f(f(…f(f(a1,a2),a3),…an−1),an)f(f(…f(f(a1,a2),a3),…an−1),an) (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 nn (1≤n≤1051≤n≤105).

The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤ai≤1090≤ai≤109). Elements of the array are not guaranteed to be different.

Output

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

Examples

input

4
4 0 11 6

output

11 6 4 0

input

1
13

output

13 

Note

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

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

找最高位为1且仅出现一次的那个数放在最前面就行

#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
int sum[N];
int main() {
	int n;
	scanf("%d", &n);
	vector<int> a(n);
	int ma = 0;
	for (auto& i : a) {
		scanf("%d", &i);
		int cnt = 0, t = i;
		while (t) {
			if (t & 1) {
				++sum[cnt];
			}
			t >>= 1;
			cnt++;
		}
		ma = max(ma, cnt);
	}
	int ans = inf;
	for (int i = ma - 1; i >= 0; i--) {
		if (sum[i] == 1) {
			ans = i;
			break;
		}
	}
	if (ans != inf) {
		int tp = 0;
		for (int i = 0; i < n; i++) {
			if (a[i] & (1 << ans)) {
				tp = i;
				break;
			}
		}
		printf("%d", a[tp]);
		for (int i = 0; i < n; i++) {
			if (i != tp) {
				printf(" %d", a[i]);
			}
		}
		printf("\n");
	}
	else {
		for (int i = 0; i < n; i++) {
			printf("%d", a[i]);
			if (i != n - 1) printf(" ");
			else printf("\n");
		}
	}
	return 0;
}

 

D. Aerodynamic

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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 PP which is defined by coordinates of its vertices. Define P(x,y)P(x,y) as a polygon obtained by translating PP by vector (x,y)−→−−(x,y)→. The picture below depicts an example of the translation:

 

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

 

The spaceship has the best aerodynamic performance if PP and TT are similar. Your task is to check whether the polygons PP and TT are similar.

Input

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

The ii-th of the next nn lines contains two integers xi,yixi,yi (|xi|,|yi|≤109|xi|,|yi|≤109), denoting the coordinates of the ii-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 PP and TT are similar. Otherwise, output "NO" in a separate line. You can print each letter in any case (upper or lower).

Examples

input

Copy

4
1 0
4 1
3 4
0 3

output

Copy

YES

input

Copy

3
100 86
50 0
150 0

output

Copy

nO

input

Copy

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

output

Copy

YES

Note

The following image shows the first sample: both PP and TT are squares. The second sample was shown in the statements.

判断中心对称。

 

#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e5 + 10;
const double  eps = 1e-9;
struct point {
	int x, y;
	point operator - (const point& b) {
		return { x - b.x, y - b.y };
	}
	bool operator == (const point& b) {
		return x == b.x && y == b.y;
	}
};
bool ki(point a, point b, point c, point d) {
	return (b - a) == (c - d);
}
int main() {
	int n;
	scanf("%d", &n);
	vector<point> q(n);
	for (auto& v : q) {
		scanf("%d%d", &v.x, &v.y);
	}
	if (n & 1) {
		printf("NO\n");
	}
	else {
		bool can = true;
		for (int i = 0; can && i < n / 2; i++) {
			if (!ki(q[i], q[i + 1], q[i + n / 2], q[(i + n / 2 + 1) % n])) {
				can = false;
			}
		}
		if (can)printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}

 

E. Water Balance

time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

There are nn water tanks in a row, ii-th of them contains aiai liters of water. The tanks are numbered from 11 to nn from left to right.

You can perform the following operation: choose some subsegment [l,r][l,r] (1≤l≤r≤n1≤l≤r≤n), and redistribute water in tanks l,l+1,…,rl,l+1,…,r evenly. In other words, replace each of al,al+1,…,aral,al+1,…,ar by al+al+1+⋯+arr−l+1al+al+1+⋯+arr−l+1. For example, if for volumes [1,3,6,7][1,3,6,7] you choose l=2,r=3l=2,r=3, new volumes of water will be [1,4.5,4.5,7][1,4.5,4.5,7]. You can perform this operation any number of times.

What is the lexicographically smallest sequence of volumes of water that you can achieve?

As a reminder:

A sequence aa is lexicographically smaller than a sequence bb of the same length if and only if the following holds: in the first (leftmost) position where aa and bb differ, the sequence aa has a smaller element than the corresponding element in bb.

Input

The first line contains an integer nn (1≤n≤1061≤n≤106) — the number of water tanks.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1061≤ai≤106) — initial volumes of water in the water tanks, in liters.

Because of large input, reading input as doubles is not recommended.

Output

Print the lexicographically smallest sequence you can get. In the ii-th line print the final volume of water in the ii-th tank.

Your answer is considered correct if the absolute or relative error of each aiai does not exceed 10−910−9.

Formally, let your answer be a1,a2,…,ana1,a2,…,an, and the jury's answer be b1,b2,…,bnb1,b2,…,bn. Your answer is accepted if and only if |ai−bi|max(1,|bi|)≤10−9|ai−bi|max(1,|bi|)≤10−9 for each ii.

Examples

input

Copy

4
7 5 5 7

output

Copy

5.666666667
5.666666667
5.666666667
7.000000000

input

Copy

5
7 8 8 10 12

output

Copy

7.000000000
8.000000000
8.000000000
10.000000000
12.000000000

input

Copy

10
3 9 5 5 1 7 5 3 8 7

output

Copy

3.000000000
5.000000000
5.000000000
5.000000000
5.000000000
5.000000000
5.000000000
5.000000000
7.500000000
7.500000000

Note

In the first sample, you can get the sequence by applying the operation for subsegment [1,3][1,3].

In the second sample, you can't get any lexicographically smaller sequence.

从后往前乱搞。

#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const int N = 1e6 + 10;
int n;
double res[N], sum[N];
int tmp[N], tim[N];
void dfs(int x) {
    if (x == n) {
        return;
    }
    if (res[x] >= res[x + 1]) {
        tmp[tim[x]] = tmp[x] = tmp[x + 1];
        tim[tmp[x]] = tim[x];
        res[tim[x]] = res[x] = res[tmp[x]] =
            (sum[tmp[x]] - sum[tim[x] - 1]) / (tmp[x] - tim[x] + 1);
        dfs(tmp[x]);
    }
}
int main() {
    cin >> n;
    for (int i = 1; i <= n; ++i) {
        tmp[i] = i; tim[i] = i;
        scanf("%lf", &res[i]);
        sum[i] = sum[i - 1] + res[i];
    }
    for (int i = n; i >= 1; --i) {
        dfs(i);
    }
    for (int i = 1; i <= n; i = tmp[i] + 1) {
        for (int j = i; j <= tmp[i]; j++) {
            printf("%.9lf\n", res[i]);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值