Codeforces Round #618 (Div. 2)
Posted smallocean
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #618 (Div. 2)相关的知识,希望对你有一定的参考价值。
A. Non-zero
Description:
Guy-Manuel and Thomas have an array (a) of (n) integers [(a_1, a_2, dots, 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 le i le 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 +) (dots) (+ a_n
e 0) and (a_1 cdot a_2 cdot) (dots) (cdot a_n
e 0).
Input:
Each test contains multiple test cases.
The first line contains the number of test cases (t) ((1 le t le 10^3)). The description of the test cases follows.
The first line of each test case contains an integer (n) ((1 le n le 100))?— the size of the array.
The second line of each test case contains (n) integers (a_1, a_2, dots, a_n) ((-100 le a_i le 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.
Sample Input:
4
3
2 -1 -1
4
-1 0 0 1
2
-1 2
3
0 -2 1
Sample Output:
1
2
0
2
思路:
遇0加1,和为零再加1.
AC代码:
#include<bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int s[150];
int main() {
int _,n;
scanf("%d",&_);
while(_--){
scanf("%d",&n);
int sum=0;
for(int i=1;i<=n;++i){
scanf("%d",&s[i]);
sum+=s[i];
}
int ans=0;
for(int i=1;i<=n;++i){
if(s[i]==0) ans++,sum++;
}
if(sum==0){
ans++;
}
printf("%d
",ans);
}
return 0;
}
B. Assigning to Classes
Description:
Reminder: the median of the array ([a_1, a_2, dots, a_{2k+1}]) of odd number of elements is defined as follows: let ([b_1, b_2, dots, 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 le t le 10^4)). The description of the test cases follows.
The first line of each test case contains a single integer (n) ((1 le n le 10^5))?— the number of students halved.
The second line of each test case contains (2n) integers (a_1, a_2, dots, a_{2 n}) ((1 le a_i le 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.
Sample Input:
3
1
1 1
3
6 5 4 1 2 3
5
13 4 20 13 2 5 8 3 17 16
Sample Output:
0
1
5
思路:
中间两个数之差
AC代码:
#include<bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5+7;
int s[maxn];
int main() {
int _,n;
scanf("%d",&_);
while(_--) {
scanf("%d", &n);
n*=2;
for (int i = 1; i <= n; ++i) {
scanf("%d", &s[i]);
}
sort(s+1,s+1+n);
printf("%d
",s[n/2+1]-s[n/2]);
}
return 0;
}
C. Anu Has a Function
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 ([a_1, a_2, dots, a_n]) is defined as (f(f(dots f(f(a_1, a_2), a_3), dots 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 le n le 10^5)).
The second line contains (n) integers (a_1, a_2, ldots, a_n) ((0 le a_i le 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.
Sample Input:
4
4 0 11 6
Sample Output:
11 6 4 0
Sample Input:
1
13
Sample Output:
13
思路:
打一下表,发现只与第一个数有关
观察后发现,一个序列的价值等于:
设(s[1]=a),其他所有数的或运算为(b)
(a-(a?)&(b)?) 所以找独一无二的最大的二进制位,因为如果这个二进制位有多个,那就没有价值.
AC代码:
#include<bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5+7;
int s[maxn],n;
#define f(x,y) (x|y)-y
int check(){
int ans=f(s[1],s[2]);
for(int i=3;i<=n;++i){
ans=f(ans,s[3]);
}
return ans;
}
int main() {
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d",&s[i]);
}
int a=-1;
for(int j=30,cnt,b=-1;j>=0;--j){
cnt=0;
for(int i=1;i<=n;++i){
if(s[i] >> j & 1){
cnt++;
b=i;
}
}
if(cnt==1){
a=b;
break;
}
}
if(a!=-1)printf("%d",s[a]);
for(int i=1;i<=n;++i) if(i!=a) printf(" %d",s[i]);
return 0;
}
D. Aerodynamic
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 (overrightarrow {(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 (overrightarrow {AB} = overrightarrow {(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 le n le 10^5))?— the number of points.
The (i)-th of the next (n) lines contains two integers (x_i, y_i) ((|x_i|, |y_i| le 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).
Sample Input:
4
1 0
4 1
3 4
0 3
Sample Output:
YES
Sample Input:
3
100 86
50 0
150 0
Sample Output:
nO
Sample Input:
8
0 0
1 0
2 1
3 3
4 6
3 6
2 5
1 3
Sample Output:
YES
思路:
首先这个题意太劝退了.
这个题就是说一个图形P不断移动,但是((0,0))点必须在图形内部或边界上,最后形成的轨迹图形T.
画一下然后感受一下就是中心对称图形(其实可以猜),(n)为偶数时才可能是中心对称图形.
简单证明一下就是对于P上点((x,y)),以一个端点((x_0,y_0)),T上一定有((x-x_0,y-y_0)),也一定有((x_0-x,y_0-y)).如果P上没有中心对称相对应的点,那肯定形状不相似....说不清楚,还是感受一下吧.
判断中心对称图形就是相对的点连线看看是否相交于一点.
AC代码:
#include<bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 2e5+7;
int a[maxn],b[maxn];
int main() {
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i]>>b[i];
if(n%2==1){
cout<<"NO";
return 0;
}
for(int i=2;i<=n/2;i++){
if(a[i]+a[n/2+i]!=a[1]+a[n/2+1]||b[i]+b[n/2+i]!=b[1]+b[n/2+1]){
cout<<"NO";
return 0;
}
}
cout<<"YES";
return 0;
}
E. Water Balance
Description:
There are (n) water tanks in a row, (i)-th of them contains (a_i) 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]) ((1le l le r le n)), and redistribute water in tanks (l, l+1, dots, r) evenly. In other words, replace each of (a_l, a_{l+1}, dots, a_r) by (frac{a_l + a_{l+1} + dots + a_r}{r-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 le n le 10^6))?— the number of water tanks.
The second line contains (n) integers (a_1, a_2, dots, a_n) ((1 le a_i le 10^6))?— 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 (a_i) does not exceed (10^{-9}).
Formally, let your answer be (a_1, a_2, dots, a_n), and the jury‘s answer be (b_1, b_2, dots, b_n). Your answer is accepted if and only if (frac{|a_i - b_i|}{max{(1, |b_i|)}} le 10^{-9}) for each (i).
Sample Input:
4
7 5 5 7
Sample Output:
5.666666667
5.666666667
5.666666667
7.000000000
Sample Input:
5
7 8 8 10 12
Sample Output:
7.000000000
8.000000000
8.000000000
10.000000000
12.000000000
Sample Input:
10
3 9 5 5 1 7 5 3 8 7
Sample Output:
3.000000000
5.000000000
5.000000000
5.000000000
5.000000000
5.000000000
5.000000000
5.000000000
7.500000000
7.500000000
思路:
贪心.相邻两项考虑.
假设分成两块(a),(b),这时有(y).如果(yleq b),(y)与(b)必须合并,因为合并后变小.
如果这时新合并的(byleq a),那么也必须合并,就这样while循环下去.
复杂度分析:感性分析能过...
AC代码:
#include<bits/stdc++.h>
const int mod = 1e9 + 7;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e6+7;
int s[maxn];
pair<ll,int>que[maxn];
int main() {
int n,pos=0;
scanf("%d",&n);
for(int i=1;i<=n;++i) {
scanf("%d",&s[i]);
}
for(int i=1;i<=n;++i){
que[++pos]=make_pair(s[i],1);
while(pos>1&&que[pos-1].first*que[pos].second>=que[pos].first*que[pos-1].second){
que[pos-1].second+=que[pos].second;
que[pos-1].first+=que[pos].first;
pos--;
}
}
for(int i=1;i<=pos;++i){
double ans=(double)que[i].first/(que[i].second*1.0);
for(int j=0;j<que[i].second;++j){
printf("%.12f
",ans);
}
}
return 0;
}
以上是关于Codeforces Round #618 (Div. 2)的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces Round #618 (Div. 2)
Codeforces Round #618 (Div. 2)
Codeforces Round #618 (Div. 2)
Codeforces Round #618 (Div. 2)题解