Codeforces Round 563 (Div. 2) 题解
自己开了场镜像玩。
前三题大水题。D有点意思。E完全不会。F被题意杀了……然而还是不会。
不过看过(且看懂)了官方题解,所以这里是六题题解齐全的。
A
水题。给原序列排序,如果此时合法则直接输出,否则说明所有数相同,一定无解。
时间复杂度 $O(n\log n)$。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
char ch=getchar();ll x=,f=;
while(ch<'' || ch>'') f|=ch=='-',ch=getchar();
while(ch>='' && ch<='') x=x*+ch-'',ch=getchar();
return f?-x:x;
}
int n,a[maxn],s1,s2;
int main(){
n=read();
FOR(i,,*n) a[i]=read();
sort(a+,a+*n+);
FOR(i,,n) s1+=a[i];
FOR(i,n+,*n) s2+=a[i];
if(s1==s2) printf("-1\n");
else FOR(i,,*n) printf("%d ",a[i]);
}
B
水题。
首先,如果所有数的奇偶性都相同,那我们什么都不能做。
否则,如果两个数奇偶性不同,可以直接交换;如果两个数奇偶性相同,可以用一个奇偶性不同的作为中介交换。那么实际上就是任意两个数都能交换,排个序即可。
时间复杂度 $O(n\log n)$。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
char ch=getchar();ll x=,f=;
while(ch<'' || ch>'') f|=ch=='-',ch=getchar();
while(ch>='' && ch<='') x=x*+ch-'',ch=getchar();
return f?-x:x;
}
int n,a[maxn];
bool hhh[];
int main(){
n=read();
FOR(i,,n) a[i]=read(),hhh[a[i]&]=true;
if(hhh[] && hhh[]) sort(a+,a+n+);
FOR(i,,n) printf("%d ",a[i]);
}
C
水题。
首先质数位置的值互不相同。一一编号即可。
对于合数(设为 $x$),可以任取它的一个质因子(设为 $p$),令 $a_x=a_p$。此时序列最大值不变,而两个互质的数的位置的值不可能相同。
我取的是最小质因子。
时间复杂度 $O(n)$。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
char ch=getchar();ll x=,f=;
while(ch<'' || ch>'') f|=ch=='-',ch=getchar();
while(ch>='' && ch<='') x=x*+ch-'',ch=getchar();
return f?-x:x;
}
int n,ans[maxn],pr[maxn],mn[maxn],pl,cnt;
bool vis[maxn];
int main(){
n=read();
FOR(i,,n){
if(!vis[i]) pr[++pl]=i,ans[i]=++cnt;
FOR(j,,pl){
if(i*pr[j]>n) break;
vis[i*pr[j]]=true;
mn[i*pr[j]]=pr[j];
if(i%pr[j]==) break;
}
}
FOR(i,,n) if(mn[i]) ans[i]=ans[mn[i]];
FOR(i,,n) printf("%d ",ans[i]);
}
D
有一点点小难度,但还是很水。
首先考虑这个序列的前缀异或和 $S$。那么对于任意的 $i$,有 $S_i\ne x$;对于任意的 $i,j$($i\ne j$),有 $S_i\oplus x\ne S_j$。
看第二条限制,发现如果选了 $a$,那么不能选 $a\oplus x$;如果选了 $a\oplus x$,那么不能选 $a$。对其它数没有影响。
所以可以在 $a,a\oplus x$ 中任选一个作为前缀异或和。
最后用选出来的前缀异或和算出原序列。此时答案一定是最优。
时间复杂度 $O(2^n)$。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int maxn=;
#define MP make_pair
#define PB push_back
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
#define FOR(i,a,b) for(int i=(a);i<=(b);i++)
#define ROF(i,a,b) for(int i=(a);i>=(b);i--)
#define MEM(x,v) memset(x,v,sizeof(x))
inline ll read(){
char ch=getchar();ll x=,f=;
while(ch<'' || ch>'') f|=ch=='-',ch=getchar();
while(ch>='' && ch<='') x=x*+ch-'',ch=getchar();
return f?-x:x;
}
int n,x,lim,ans[maxn],al;
bool vis[maxn];
int main(){
n=read();x=read();
lim=<<n;
FOR(i,,lim-){
if(vis[i^x] || i==x) continue;
vis[i]=true;
ans[++al]=i;
}
printf("%d\n",al);
FOR(i,,al) printf("%d ",ans[i]^ans[i-]);
}
E
太难了……难度 2500……
为此专门另开了篇博客:传送门
F
一开始以为不会给树的形态,一直在想……
这里给出轻重链剖分(俗称树剖)的做法。
首先一开始肯定要查询 $1$ 到 $x$ 的距离。这个就是 $x$ 的深度 $dep_x$。(定义 $1$ 的深度为 $0$)
现在假设我们已经确定 $x$ 在 $u$ 的子树中。一开始 $u=1$。
我们沿着 $u$ 的重儿子一直跳,跳到叶子结点 $v$。(实际上不用真跳,可以实现成 $O(1)$)
接下来,再考虑 $y=lca(x,v)$。(从官方题解盗张图)

首先,询问一次 $dis(x,v)$。由于 $dis(x,v)=dep_x+dep_v-2dep_y$,而 $dis(x,v)$ 已知,$dep_x$ 已知,$dep_v$ 已知,可以算出 $dep_y$。那么 $y$ 也已知。
如果 $dep_x=dep_y$ 那么 $x=y$。 否则我们再询问 $y$ 到 $x$ 路径上的第二个点 $www$(没名字了),然后接下来就可以在 $u=www$ 的子树中寻找答案了。
对于最后一次查找,只用 $1$ 次询问,其它要 $2$ 次询问。预处理 $dep_x$ 要 $1$ 次询问。由于 $sz[www]\le sz[y]/2\le sz[u]$,所以每次 $u$ 的大小都会缩小一半。
那么总共要 $2\log n$ 次询问。时间复杂度 $O(n)$。
代码咕着,会来补的。
Codeforces Round 563 (Div. 2) 题解的更多相关文章
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
- Codeforces Round #528 (Div. 2)题解
Codeforces Round #528 (Div. 2)题解 A. Right-Left Cipher 很明显这道题按题意逆序解码即可 Code: # include <bits/stdc+ ...
- Codeforces Round #563 (Div. 2)/CF1174
Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i=1}^n a_i ...
- Codeforces Round #466 (Div. 2) 题解940A 940B 940C 940D 940E 940F
Codeforces Round #466 (Div. 2) 题解 A.Points on the line 题目大意: 给你一个数列,定义数列的权值为最大值减去最小值,问最少删除几个数,使得数列的权 ...
- Codeforces Round #677 (Div. 3) 题解
Codeforces Round #677 (Div. 3) 题解 A. Boring Apartments 题目 题解 简单签到题,直接数,小于这个数的\(+10\). 代码 #include &l ...
- Codeforces Round #665 (Div. 2) 题解
Codeforces Round #665 (Div. 2) 题解 写得有点晚了,估计都官方题解看完切掉了,没人看我的了qaq. 目录 Codeforces Round #665 (Div. 2) 题 ...
- Codeforces Round #160 (Div. 1) 题解【ABCD】
Codeforces Round #160 (Div. 1) A - Maxim and Discounts 题意 给你n个折扣,m个物品,每个折扣都可以使用无限次,每次你使用第i个折扣的时候,你必须 ...
随机推荐
- SQL ------------ avg() 与 sum 函数
AVG() 函数返回数字列的平均值 注意是数字的平均数, 语法: select avg(字段) from 表名 建个表,弄点数据 使用 select avg(字段) as 平均数 from 表名 与w ...
- Idea java 中导包路径分析
工具类所在包: 查看工具类详情: 调用这个工具类时,导入的包路径为: 路径就是第1张图片中的包名utils+类名NumberUtils组成的utils.NumberUtils
- .net core+topshelf+quartz创建windows定时任务服务
.net core+topshelf+quartz创建windows定时任务服务 准备工作 创建.net core 控制台应用程序,这里不做过多介绍 添加TopShelf包:TopShelf: 添加Q ...
- knockout.js绑定(enable,disable,visable)
<input type="text" data-bind="disable:IsNew" /> enable :是否可用,为true时,可编辑 di ...
- asp.net MVC中使用EasyUI Treegrid 树形网格
引入CSS和JS <link href="~/Content/plugins/jquery-easyui-1.7.0/themes/default/easyui.css" r ...
- python 进程和线程-简介及进程
进程和线程的关系及应用 参考链接:https://www.liaoxuefeng.com/wiki/1016959663602400/1017627212385376 多任务: 什么叫“多任务”呢?简 ...
- 架构师小跟班:如何高效又安全的清理Linux服务器上的缓存?
操作服务器上的生产环境,一定要慎之又慎,安全第一,优化第二! 一些基本原理 说到清理内存,那么不得不提到/proc这一个虚拟文件系统,这里面的数据和文件都是内存中的实时数据,很多参数的获取都可以从下面 ...
- 设计模式之(十二)享元模式(Flyweight)
享元模式思想 就以小时候导锅来说吧(导锅是我家乡的方言,就是用细沙把一个模型锅的形状拓下来,然后把铝水倒进模型中,就导好一个锅了.小时候很喜欢看的,可惜现在看不到了.上个图片回忆下)了解了这个过程后就 ...
- java--String与int相互转换
字符串与int类型的互相转换 String ---> int //方式一:Integer(String s) //demo: Integer i = new Integer("10&q ...
- Python——EM(期望极大算法)教学(附详细代码与注解)
今天,我们详细的讲一下EM算法. 前提准备 Jupyter notebook 或 Pycharm 火狐浏览器或谷歌浏览器 win7或win10电脑一台 网盘提取csv数据 需求分析 实现高斯混合模型的 ...