Codeforces Round #618 Div2
https://codeforces.com/contest/1300
只做了前三题…太弱了,第二第三题我的代码太丑了,就放了大佬的代码
A. Non-zero
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
inputCopy
4
3
2 -1 -1
4
-1 0 0 1
2
-1 2
3
0 -2 1
outputCopy
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.
#include<bits/stdc++.h>
using namespace std;
int a[105];
int main(){
int t,n;
cin>>t;
while(t--)
{
cin>>n;
int ans=0,sum=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(a[i]==0)
{
ans++;
a[i]++;
}
sum+=a[i];
}
if(sum!=0)
cout<<ans<<endl;
else
{
cout<<ans+1<<endl;
}
}
return 0;
}
B. Assigning to Classes
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.
下面这个是一个大佬的代码
#include<bits/stdc++.h>
using namespace std;
const int N=2e5+1;
int t,n,a[N];
int main()
{
cin>>t;
while(t--&&cin>>n)
{
for(int i=1;i<=2*n;i++)
cin>>a[i];
sort(a+1,a+2*n+1);
cout<<abs(a[n+1]-a[n])<<'\n';
}
return 0;
}
C. Anu Has a Function
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
inputCopy
4
4 0 11 6
outputCopy
11 6 4 0
inputCopy
1
13
outputCopy
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.
下面这个是一个大佬的代码
include<bits/stdc++.h>
using namespace std;
const int N=1e5+1;
int n,a[N],c[N],t,A;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=n;i++)
for(int j=0;j<30;j++)
if((1<<j)&a[i])
c[j]++;
for(int i=1;i<=n;i++)
{
t=0;
for(int j=0;j<30;j++)
if(((1<<j)&a[i])&&c[j]==1)
t+=(1<<j);
if(t>A)
swap(a[1],a[i]),A=t;
}
for(int i=1;i<=n;i++)
cout<<a[i]<<' ';
return 0;