[CF百场计划]#2 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 \ne 0\) and \(a_1 \cdot a_2 \cdot\) \(\dots\) \(\cdot a_n \ne 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\n",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\n",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]\) (\(1\le 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\).如果\(y\leq b\),\(y\)与\(b\)必须合并,因为合并后变小.
如果这时新合并的\(by\leq 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\n",ans);
}
}
return 0;
}
[CF百场计划]#2 Codeforces Round #618 (Div. 2)的更多相关文章
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) A. Jeff and Rounding
http://codeforces.com/problemset/problem/351/A 题意: 2*n个数,选n个数上取整,n个数下取整 最小化 abs(取整之后数的和-原来数的和) 先使所有的 ...
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) A. Bits
http://codeforces.com/contest/484/problem/A 题意: 询问[a,b]中二进制位1最多且最小的数 贪心,假设开始每一位都是1 从高位i开始枚举, 如果当前数&g ...
- CF&&CC百套计划4 Codeforces Round #276 (Div. 1) E. Sign on Fence
http://codeforces.com/contest/484/problem/E 题意: 给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值 第i棵线段树表示>=i的数 维护 ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) E. Jeff and Permutation
http://codeforces.com/contest/351/problem/E 题意: 给出一些数,可以改变任意数的正负,使序列的逆序对数量最少 因为可以任意加负号,所以可以先把所有数看作正数 ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) B. Jeff and Furik
http://codeforces.com/contest/351/problem/B 题意: 给出一个n的排列 第一个人任选两个相邻数交换位置 第二个人有一半的概率交换相邻的第一个数>第二个数 ...
- CF&&CC百套计划3 Codeforces Round #204 (Div. 1) D. Jeff and Removing Periods
http://codeforces.com/problemset/problem/351/D 题意: n个数的一个序列,m个操作 给出操作区间[l,r], 首先可以删除下标为等差数列且数值相等的一些数 ...
- [CF百场计划]#3 Educational Codeforces Round 82 (Rated for Div. 2)
A. Erasing Zeroes Description You are given a string \(s\). Each character is either 0 or 1. You wan ...
- [CF百场计划]Codeforces Round #617 (Div. 3)
A. Array with Odd Sum Description You are given an array \(a\) consisting of \(n\) integers. In one ...
- CF&&CC百套计划1 Codeforces Round #449 B. Ithea Plays With Chtholly
http://codeforces.com/contest/896/problem/B 题意: 交互题 n张卡片填m个1到c之间的数,1<=n*ceil(c/2)<=m 最后填出一个单调非 ...
随机推荐
- 洛谷P1002——过河卒
又是洛谷题,要不是有小姐姐不会,我才不想动脑子.先贴一下题目地址https://www.luogu.org/problem/P1002 再贴一下题目: 我们读一下题目,这可不比学校的**算法题,读完一 ...
- Arduino 的读串口与写串口
//准备一下 while(Serial.available()>0) WifiSerial.write(Serial.read()); wh ...
- cf 498 B. Name That Tune
不会不会,,,%%http://hzwer.com/5813.html #include<cstdio> #include<iostream> #include<algo ...
- [转载]Jquery Chosen 插件动态生成option或重新绑定
$(".chosen—select").find("option[value='1']").attr("selected", "s ...
- 【转】美团 MySQL 数据实时同步到 Hive 的架构与实践
文章转载自公众号 美团技术团队 , 作者 萌萌 背景 在数据仓库建模中,未经任何加工处理的原始业务层数据,我们称之为ODS(Operational Data Store)数据.在互联网企业中,常见的 ...
- 吴裕雄--天生自然C++语言学习笔记:C++ 多线程
多线程是多任务处理的一种特殊形式,多任务处理允许让电脑同时运行两个或两个以上的程序.一般情况下,两种类型的多任务处理:基于进程和基于线程. 基于进程的多任务处理是程序的并发执行. 基于线程的多任务处理 ...
- 11 ~ express ~ 解决 cookie 中文报错的问题
使用cookies包需要注意:1,cookie中是不能有中文的,一旦有中文,就会报错2,cookie是通过 中间件的形式直接挂载到 req对象上的,那么cookies有的方法,req.cookies就 ...
- 【数据结构】二叉树的遍历(前、中、后序及层次遍历)及leetcode107题python实现
文章目录 二叉树及遍历 二叉树概念 二叉树的遍历及python实现 二叉树的遍历 python实现 leetcode107题python实现 题目描述 python实现 二叉树及遍历 二叉树概念 二叉 ...
- gentoo 修改键盘映射
gentoo 上面修改键盘映射分为两种,一种是终端环境,一种是X环境. 终端环境 https://www.emacswiki.org/emacs/MovingTheCtrlKey https://wi ...
- 理解Spring Boot Actuator
Spring Boot Actuator 用于监控和管理spring应用,可通过HTTP Endpoint或JMX Bean与其交互.