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个折扣的时候,你必须 ...
随机推荐
- 自定义httpservletrequest解析参数
3.添加参数解析器 4.注册
- 一、hexo+github搭建个人博客的过程记录
前提: 1.新建一个github仓库 2.安装配置Node.js 3.安装配置Git 前提 步骤1.新建一个github仓库 打开github网站,(注册)登录账号,新建一个仓库; 注:==仓库名称要 ...
- com.fasterxml.jackson.core.JsonGenerationException: Can not write a field name, expecting a value异常
springboot对象返回,一直报生成json异常,经过检查,发现是自己在做xss防护时对出参进行了json的处理(copy代码不可取,囧) 异常信息 这里进行了出参处理了,但实际上只要对入参处理就 ...
- windows下生成ssl
1.安装git window 需要安装 git 按部就班即可 https://git-scm.com/ 2.安装完之后,打开 Git Bash 输入以下命令并执行,然后一路按“回车”即可,效果见图: ...
- .NET设计模式-观察者模式
Observer(观察者模式) 定义对象间的一种一对多的依赖关系,以便当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并自动刷新. 说白了就是事件. using System; namespa ...
- c#实现串口通信
参考文章:https://www.cnblogs.com/Zed-H/p/8651882.html 利用虚拟串口软件连接两个虚拟串口,如图连接6,7: 打开串口进行通信如下:
- C#4.0的十种语法糖
https://www.cnblogs.com/dotnet261010/p/6055092.html
- 未在本地计算机上注册“microsoft.ACE.oledb.12.0”提供程序报错的解决办法
https://www.jb51.net/article/157457.htm 下载32位版本安装即可 Microsoft Access Database Engine Redistributable ...
- [Silverlight 4] Textbox style模擬Textblock 使可以選取、複製
childwindow 做為訊息視窗,使用textblock,可是textbloc無法選取內容及複製, 就改用textbox假裝成textblock ---原本的textblock <contr ...
- Git本地有未提交文件,直接拉取远端最新版本
git pull = git fetch + git merge 1.修改不同的文件: 用户D和用户L在本地提交中修改了不同的文件,如果用户D将改动推送到服务器后,用户L再推送就会遇到非快进式推送错误 ...