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个折扣的时候,你必须 ...
随机推荐
- C# Thread was being aborted
先重现问题 1.新建一个aspx页面项目,插入两个页面WebForm1.aspx,WebForm2.aspx, WebForm1代码修改如下 protected void Page_Load(obje ...
- history路由模式下的nginx配置
路由模式 众所周知,浏览器下的单页面应用的路由模式有下面两种: hash 模式和 history 模式.hash 模式通用性好,而且不依赖服务器的配置,省心省力,但是缺点是不够优雅.相比于 hash ...
- SublimeText3安装Markdown插件
由于Webstrom对md文件的预览效果并不理想(与实际网页编译效果差别较大),所以我又改用Sublime进行本地编辑,下面介绍一下怎么搭建环境. 插件安装 整套环境我们就需要两个插件:Markdow ...
- C# foreach循环
一.简介 foreach循环可以迭代数组或者一个集合对象 二.语法 foreach(数据类型 变量名 in 数组名) { //语句块: } 循环运行的过程:每一次循环时,从集合中取出一个 ...
- 【转载】C#里怎么把string类型转换成double
在C#的数字计算过程中,有很多的方法可以将字符串String类型的变量转换为double类型,double.Parse方法.Convert.ToDouble方法.double.TryParse方法等都 ...
- ES6-面向对象
1.老版的面向对象: function User(name,pass){ this.name=name; this.pass=pass; } User.prototype.showName=funct ...
- uni-app采坑记录
1. uni-app采坑记录 1.1. 前言 这里记录下uni-app实践中踩的坑 1.2. 坑点 1.2.1. 触发事件@longTap和@longpress 这两个都表示长按触发事件,那么这两个有 ...
- Android源码分析(九)-----如何修改Android系统默认时间
一 : 修改Android系统默认时间 源码路径:frameworks/base/services/java/com/android/server/SystemServer.java 主要变量EARL ...
- 【学习笔记】PYTHON网络爬虫与信息提取(北理工 嵩天)
学习目的:掌握定向网络数据爬取和网页解析的基本能力the Website is the API- 1 python ide 文本ide:IDLE,Sublime Text集成ide:Pychar ...
- 使用gacutil把COM组件注册到全局缓存GAC中
我们在编写软件的时候,有时候需要调用COM组件,那就需要注册了,注册有两种,一种是使用regasm 在程序运行的时候注册,参考“pb调用C#编写的DLL类库“,不过受路径的限制.还有一种注册方式,使用 ...