Codeforces Round #618 (Div. 2) - BenFromHRBUST

A. Non-zero

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

Problem Description

Guy-Manuel and Thomas have an array a of n integers [a1,a2,…,an]. 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 ai:=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+ … +an≠0 and a1⋅a2⋅ … ⋅an≠0.

Input

Each test contains multiple test cases.
The first line contains the number of test cases t (1≤t≤103). 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 a1,a2,…,an (−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
4
3
2 -1 -1
4
-1 0 0 1
2
-1 2
3
0 -2 1
output
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>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
 
using namespace std;
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<VII> VIII;
typedef pair<int, int> PII;
 
#define rep(i,a,b) for(int i=(a); i<=(b); i++)
#define per(i,a,b) for(int i=(a); i>=(b); i--)
 
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
//const ll MOD = 233333333;
 
int main()
{
    int T;
    scanf("%d", &T);
    while(T --)
    {
        int n;
        scanf("%d", &n);
        int zero = 0;
        int sum = 0;
        rep(i,1,n)
        {
            int x;
            scanf("%d", &x);
            if(x == 0)
            {
                zero ++;
                x ++;
            }
            sum += x;
        }
        if(sum == 0)
        {
            printf("%d\n", zero+1);
        }
        else
        {
            printf("%d\n", zero);
        }
    }
    return 0;
}


B. Assigning to Classes

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

Problem Description

Reminder: the median of the array [a1,a2,…,a2k+1] of odd number of elements is defined as follows: let [b1,b2,…,b2k+1] be the elements of the array in the sorted order. Then median of this array is equal to bk+1.
There are 2n students, the i-th student has skill level ai. 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 a1,a2,…,a2n (1≤ai≤109) — skill levels of students.
It is guaranteed that the sum of n over all test cases does not exceed 105.

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
output
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>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
 
using namespace std;
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<VII> VIII;
typedef pair<int, int> PII;
 
#define rep(i,a,b) for(int i=(a); i<=(b); i++)
#define per(i,a,b) for(int i=(a); i>=(b); i--)
 
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
//const ll MOD = 233333333;
 
int num[N*2];
 
int main()
{
    int T;
    scanf("%d", &T);
    while(T --)
    {
        int n;
        scanf("%d", &n);
        rep(i,1,2*n) scanf("%d", &num[i]);
        sort(num+1, num+1+2*n);
        printf("%d\n", num[n+1]-num[n]);
    }
    return 0;
}



C. Anu Has a Function

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

Problem Description

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 [a1,a2,…,an] is defined as 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 n (1≤n≤105).
The second line contains n integers a1,a2,…,an (0≤ai≤109). 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.

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] 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.

题意

如题,给定函数:f(x,y)=(x|y)−y。
给定一个序列,可以任意调换序列内顺序,使得f(f(…f(f(a1,a2),a3),…an−1),an)值最小。
输出调换顺序后的序列。

思路

我们可以发现,对于函数f(f(…f(f(a1,a2),a3),…an−1),an)值即为,将a数组所有的数表示为二进制后,当a1的第pos位为1且a2…a3的第pos位均不为1时,所有这样的第pos位的权重之和。

Code

#include <bits/stdc++.h>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
 
using namespace std;
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<VII> VIII;
typedef pair<int, int> PII;
 
#define rep(i,a,b) for(int i=(a); i<=(b); i++)
#define per(i,a,b) for(int i=(a); i>=(b); i--)
 
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
//const ll MOD = 233333333;
 
int num[N];
int mp[N][33];
int tol[N];
 
int main()
{
    int n;
    scanf("%d", &n);
    rep(i,1,n)
    {
        scanf("%d", &num[i]);
        int xx = num[i];
        int cnt = 0;
        while(xx)
        {
            mp[i][++cnt] = xx%2;
            tol[cnt] += xx%2;
            xx/=2;
        }
    }
    int maxn = -1;
    int pos = 0;
    rep(i,1,n)
    {
        int cnt = 1;
        int ans = 0;
        rep(j,1,32)
        {
            if(mp[i][j]==1 && tol[j] == 1)
            {
                ans += cnt;
            }
            cnt *= 2;
        }
        if(ans > maxn)
        {
            pos = i;
            maxn = ans;
        }
    }
    printf("%d ", num[pos]);
    rep(i,1,n) i==pos||printf("%d ", num[i]);
    printf("\n");
    return 0;
}


D. Aerodynamic

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output

Problem Description

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 (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 AB−→−=(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 xi,yi (|xi|,|yi|≤109), 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).

Examples

input
4
1 0
4 1
3 4
0 3
output
YES

input
3
100 86
50 0
150 0
output
nO

input
8
0 0
1 0
2 1
3 3
4 6
3 6
2 5
1 3
output
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>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
 
using namespace std;
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<VII> VIII;
typedef pair<int, int> PII;
 
#define rep(i,a,b) for(int i=(a); i<=(b); i++)
#define per(i,a,b) for(int i=(a); i>=(b); i--)
 
const int N = 1e5 + 5;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
//const ll MOD = 233333333;
 
struct P
{
    int x, y;
} p[N];
 
int main()
{
    int n;
    scanf("%d", &n);
    rep(i,1,n) scanf("%d%d", &p[i].x, &p[i].y);
    p[0].x = p[n].x, p[0].y = p[n].y;
    if(n%2 == 1)
    {
        printf("NO\n");
    }
    else
    {
        int flag = 0;
        rep(i,0,n/2-1)
        {
            if(p[i+1].x-p[i].x!=p[i+n/2].x-p[i+1+n/2].x || p[i+1].y-p[i].y!=p[i+n/2].y-p[i+1+n/2].y)
            {
                flag = 1;
                break;
            }
        }
        if(flag == 1)
        {
            printf("NO\n");
        }
        else
        {
            printf("YES\n");
        }
    }
    return 0;
}


E. Water Balance

time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

Problem Description

There are n water tanks in a row, i-th of them contains ai liters of water. The tanks are numbered from 1 to n from left to right.
You can perform the following operation: choose some subsegment [l,r] (1≤l≤r≤n), and redistribute water in tanks l,l+1,…,r evenly. In other words, replace each of al,al+1,…,ar by al+al+1+⋯+arr−l+1. For example, if for volumes [1,3,6,7] you choose l=2,r=3, new volumes of water will be [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 a is lexicographically smaller than a sequence b of the same length if and only if the following holds: in the first (leftmost) position where a and b differ, the sequence a has a smaller element than the corresponding element in b.

Input

The first line contains an integer n (1≤n≤106) — the number of water tanks.
The second line contains n integers a1,a2,…,an (1≤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 i-th line print the final volume of water in the i-th tank.
Your answer is considered correct if the absolute or relative error of each ai does not exceed 10−9.
Formally, let your answer be a1,a2,…,an, and the jury’s answer be b1,b2,…,bn. Your answer is accepted if and only if |ai−bi|max(1,|bi|)≤10−9 for each i.

Examples

input
4
7 5 5 7
output
5.666666667
5.666666667
5.666666667
7.000000000

input
5
7 8 8 10 12
output
7.000000000
8.000000000
8.000000000
10.000000000
12.000000000

input
10
3 9 5 5 1 7 5 3 8 7
output
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].
In the second sample, you can’t get any lexicographically smaller sequence.

题意

给定一个序列,可以选择一个[l, r]区间,使得这个区间内的所有值替换为这个区间内的平均值。
可以对这个序列操作任意次,使得这个序列字典序最小,问这个最小字典序的序列是什么。

思路

假设我们已经有两个区间[l1, r1],[l2, r2],区间内的值分别为x1,x2,当前有一个数k加入这两个区间之后,我们可以知道,如果k<x2,我们可以将k加入区间[l2, r2]中,此时第二个区间变为[l2, r2+1],区间内的值为x2’,如果x2’<x1,同理,我们可以将第二个区间加入第一个区间中。
可以证明,复杂度为O(n).

我也不知道为啥,卿姐说的。

Code

#include <bits/stdc++.h>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
 
using namespace std;
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VII;
typedef vector<VII> VIII;
typedef pair<int, int> PII;
 
#define rep(i,a,b) for(int i=(a); i<=(b); i++)
#define per(i,a,b) for(int i=(a); i>=(b); i--)
 
const int N = 1e6 + 5;
const int INF = 0x3f3f3f3f;
const ll MOD = 1e9 + 7;
//const ll MOD = 998244353;
//const ll MOD = 233333333;
 
int num[N];
int sz[N];
double st[N];
 
int main()
{
    int n;
    scanf("%d", &n);
    rep(i,1,n) scanf("%d", &num[i]);
    int cnt = 0;
    rep(i,1,n)
    {
        st[++cnt] = 1.0*num[i];
        sz[cnt] = 1;
        while(cnt>1 && st[cnt]<st[cnt-1])
        {
            st[cnt-1] = (st[cnt-1]*sz[cnt-1]+st[cnt]*sz[cnt])/(sz[cnt-1]+sz[cnt]);
            sz[cnt-1] = (sz[cnt-1]+sz[cnt]);
            -- cnt;
        }
    }
    rep(i,1,cnt)
    {
        rep(j,1,sz[i])
        {
            printf("%.15f\n", st[i]);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值