Codeforces Round #618 (Div. 2)
题库链接
https://codeforces.ml/contest/1300
A. Non-zero
一个数组,每次操作可以给某个数加1,让这个数组的积和和不为0的最小操作数
显然如果有0的话,必须操作一次,最后如果和还是为0的话,再操作一次
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 201110;
const int M = 1e9+7;
int n,m;
int a[maxn];
signed main()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in", "r", stdin);
#endif
int T;
cin>>T;
while(T--)
{
cin>>n;
int sum = 0;
int ans = 0;
for(int i = 1; i <= n; i++)
{
cin>>a[i];
if(a[i] == 0)
{
ans++;
a[i] = 1;
}
sum += a[i];
}
if(sum == 0) ans++;
cout<<ans<<endl;
}
return 0;
}
B. Assigning to Classes
有一个数组有2*n个元素,要分成两个数组,每个数组必须是奇数个元素,求两个数组的中位数之差的最小值
假设左边的中位数会小于右边的中位数,显然,左边越大越好,右边越小越好,大能大到什么程度呢?最大就是原数组的中位数
所以答案就是原数组的中间两个数的差值
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 201110;
const int M = 1e9+7;
int n,m;
int a[maxn];
signed main()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in", "r", stdin);
#endif
int T;
cin>>T;
while(T--)
{
cin>>n;
for(int i = 1; i <= 2*n; i++)
{
cin>>a[i];
}
sort(a+1,a+1+2*n);
cout<<abs(a[n]-a[n+1])<<endl;
}
return 0;
}
C. Anu Has a Function
\(f(x, y) = (x | y) - y\)
要求重新排列数组,使得\(f(f(\dots f(f(a_1, a_2), a_3), \dots a_{n-1}), a_n)\)最大
我们先看f函数,x或y减去y,把它拆分成二进制,对于某一位,如果x,y都是1,f(x, y)之后x的这一位会被减去
如果x是1,y是0,或者x是0,y是1,那么x的这一位不变
所以我们只需要求出某一位只有x是1,其余数组元素都是0的最高位,让它在最前面就好了,其它元素随便排列
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 201110;
const int M = 1e9+7;
int n,m;
int a[maxn];
int vis[33];
int b[33];
signed main()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in", "r", stdin);
#endif
cin>>n;
for(int i = 1; i <= n; i++)
{
cin>>a[i];
for(int j = 30; j >= 0; j--)
{
if((1<<j)&a[i])
{
vis[j]++;
b[j] = i;
}
}
}
int ans = 1;
for(int i = 0; i <= 30; i++)
{
if(vis[i] == 1) ans = b[i];
}
cout<<a[ans]<<' ';
for(int i = 1; i <= n; i++)
{
if(i != ans) cout<<a[i]<<' ';
}
return 0;
}
D. Aerodynamic
就是问你是否中心对称
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 201110;
const int M = 1e9+7;
int n,m;
vector<pii> v;
signed main()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in", "r", stdin);
#endif
cin>>n;
v.push_back({0,0});
for(int i = 1,x,y; i <= n; i++)
{
cin>>x>>y;
v.push_back({x,y});
}
if(n%2) {puts("NO");return 0;}
int t = n/2;
double midx = (v[1].first + v[t+1].first)*1.0/2;
double midy = (v[1].second + v[t+1].second)*1.0/2;
for(int i = 2; i <= t; i++)
{
double tmpx = (v[i].first + v[t+i].first)*1.0/2;
double tmpy = (v[i].second + v[t+i].second)*1.0/2;
if(midx != tmpx || midy != tmpy)
{
puts("NO");return 0;
}
}
puts("YES");
return 0;
}
E. Water Balance
有n个水桶,每次可以选择一些区间,使得区间内的水桶的水量等于区间水量之和除以区间长度
问你可以得到的水量的字典序的最小是什么
单调栈可以做,凸包也可以做,凸包我不会,说一下单调栈的做法
#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define int long long
#define gcd __gcd
const int inf = 0x3f3f3f3f;
const int maxn = 1001110;
const int M = 1e9+7;
int n,m;
int a[maxn];
int sa[maxn];
int la[maxn];
signed main()
{
#ifdef ONLINE_JUDGE
#else
freopen("data.in", "r", stdin);
#endif
cin>>n;
for(int i = 1; i <= n; i++)
{
cin>>a[i];
}
int top = 0;
for(int i = 1; i <= n; i++)
{
int tsa = a[i],tla = 1;
for(;top && sa[top]*tla >= tsa*la[top]; top--)
{
tsa += sa[top];
tla += la[top];
}
++top;
sa[top] = tsa;
la[top] = tla;
}
for(int i = 1,cur = 1; i <= top; i++,cur += la[top])
{
for(int j = cur;j < cur + la[i]; j++)
{
printf("%.9f\n",sa[i]*1.0/la[i]);
}
}
return 0;
}
Codeforces Round #618 (Div. 2)的更多相关文章
- Codeforces Round #618 (Div. 1)C(贪心)
把所有数看作N块,后面的块比前面的块小的话就合并,这个过程可能会有很多次,因为后面合并后会把前面的块均摊地更小,可能会影响更前面地块,像是多米诺骨牌效应,从后向前推 #define HAVE_STRU ...
- Codeforces Round #618 (Div. 1)B(几何,观察规律)
观察猜测这个图形是中心对称图形是则YES,否则NO #define HAVE_STRUCT_TIMESPEC #include<bits/stdc++.h> using namespace ...
- Codeforces Round #618 (Div. 1)A(观察规律)
实际上函数值为x&(-y) 答案仅和第一个数字放谁有关 #define HAVE_STRUCT_TIMESPEC #include <bits/stdc++.h> using na ...
- Codeforces Round #618 (Div. 2)A. Non-zero
Guy-Manuel and Thomas have an array aa of nn integers [a1,a2,…,an ]. In one step they can add 11 to ...
- Codeforces Round #618 (Div. 2)C. Anu Has a Function
Anu has created her own function ff : f(x,y)=(x|y)−y where || denotes the bitwise OR operation. For ...
- Codeforces Round #618 (Div. 2) 小号上紫之路
这一场涨了不少,题也比较偏思维,正好适合我 A. Non-zero 我们记录这些数字的总和sum,并且记录0的个数zero,显然答案应该是这些0的个数,注意如果sum+zero==0的话答案要额外加一 ...
- [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, \d ...
- Codeforces Round #618 (Div. 2)-B. Assigning to Classes
Reminder: the median of the array [a1,a2,-,a2k+1] of odd number of elements is defined as follows: l ...
- Codeforces Round #618 (Div. 2)-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 ...
随机推荐
- node多进程的创建与守护
node是单线程运行,我们的node项目如何利用多核CPU的资源,同时提高node服务的稳定性呢? 1. node的单线程 进程是一个具有一定独立功能的程序在一个数据集上的一次动态执行的过程,是操作系 ...
- Could not write JSON: Infinite recursion (StackOverflowError);
转自:https://blog.csdn.net/east123321/article/details/80435051 在controller返回数据到统一json转换的时候,出现了json inf ...
- 从头学pytorch(十九):批量归一化batch normalization
批量归一化 论文地址:https://arxiv.org/abs/1502.03167 批量归一化基本上是现在模型的标配了. 说实在的,到今天我也没搞明白batch normalize能够使得模型训练 ...
- 重拾c++第一天(3):数据处理
1.short至少16位:int至少与short一样长:long至少32位,且至少与int一样长:long long至少64位,且至少与long一样长 2.sizeof 变量 返回变量长度 或者s ...
- 常见基本数据结构——树,二叉树,二叉查找树,AVL树
常见数据结构——树 处理大量的数据时,链表的线性时间太慢了,不宜使用.在树的数据结构中,其大部分的运行时间平均为O(logN).并且通过对树结构的修改,我们能够保证它的最坏情形下上述的时间界. 树的定 ...
- 2D地图擦除算法
. 关于2D地图擦除算法,去年我写过一个实现,勉强实现了地形擦除,但跟最终效果还相差甚远,这次我写了一个完整的实现,在此记录,留个印象. . 去年的版本<<算法 & 数据结构--裁 ...
- 线性基 - 寻找异或第K大
XOR is a kind of bit operator, we define that as follow: for two binary base number A and B, let C=A ...
- 测试工具Fiddler(二)—— 入门使用
Fiddler设置与安装证书 一.Fiddler常见设置 Options位置:Tools->Options 二.移动端连上Fiddler作为代理 注意:因为Charles也是默认8888端口,小 ...
- threding.local
作用:为每一个线程开辟一个独立的内存空间 示例 from threading import Thread, local import time obj = local() def test(i): o ...
- 如何高效实用 Git
Git 工作流 只要项目是多人参与的,那么就需要使用正确的 Git 工作流程. 下面介绍一个简单有效的工作流程. 场景 假设有一个项目,要开发下一代的 Facebook,你就是这个项目的技术 lead ...