CF1174
A略
CF1174B
CF1174B题意
给一个数组a,可以交换加和是奇数的数字,问字典序最小可以是多少。
CF1174B题解
判断是否全奇或全偶,不是则排序,是则原序输出。
CF1174B代码
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int x = 0, f =1;char ch = getchar();
while(ch <'0' || ch > '9'){if(ch == '-') f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + (ch ^ 48);ch = getchar();}
return x * f;
}
const int maxn = 1e5 + 10;
int a[maxn], n;
signed main(){
n = read();bool odd = 1, even = 1;
for(int i = 1;i <= n;i++){
a[i] = read();
if(a[i] & 1)odd = 0;
if((a[i] & 1) == 0)even = 0;
}
if(odd || even){for(int i = 1;i <= n;i++)printf("%d ",a[i]);}
else {sort(a + 1,a + 1 + n);for(int i = 1;i <= n;i++)printf("%d ",a[i]);}
return 0;
}
CF1174C
CF1174C题意
你有一个数组,长度为n,现在你需要构造一个序列,使得这个序列满足如下要求:
- 对于任意一对 i , j i,j i,j,如果 gcd ( i , j ) = 1 \gcd(i,j)=1 gcd(i,j)=1,则 a [ i ] ! = a [ j ] a[i]!=a[j] a[i]!=a[j]
- 使该序列中的最大值最小化。
请输出这个序列的第二个位置到最后一个位置的值。
CF1174C题解
序列最大值就是小于n的质数的数量,然后按照每一个质数染色即可。
CF1174C代码
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int x = 0, f =1;char ch = getchar();
while(ch <'0' || ch > '9'){if(ch == '-') f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + (ch ^ 48);ch = getchar();}
return x * f;
}
const int maxn = 1e5 + 10;
int n, a[maxn], col;
int prime[maxn], tot;
int book[maxn];
signed main(){
n = read();
for(int i = 2;i <= n;i++){
if(!book[i]){prime[++tot] = i;a[i] = ++col;}
for(int j = 1;j <= tot && i * prime[j] <= n;j++){
book[i * prime[j]] = 1;
if(i % prime[j] == 0)break;
}
}
for(int i = 1;i <= tot;i++){
for(int j = 1;j * prime[i] <= n;j++){
a[j * prime[i]] = a[prime[i]];
}
}
for(int i = 2;i <= n;i++)printf("%d ",a[i]);
return 0;
}
CF1174D
CF1174D题意
给两个数 n n n 和 x x x,要你构造一个序列满足:
- 所有数字满足 1 ≤ a i < 2 n 1\le a_i<2^n 1≤ai<2n
- 所有连续子序列的异或和不等于 0 0 0 或 x x x
- 尽可能长
给出任意一种构造即可
CF1174D题解
1900题评紫题??!
但是这玩应真的不难啊
看到子序列异或和想到先构造前缀异或和,然后再改成每个数。
然后就硬做就完了。
细节看代码。(其实没细节)
CF1174D代码
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int x = 0, f =1;char ch = getchar();
while(ch <'0' || ch > '9'){if(ch == '-') f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + (ch ^ 48);ch = getchar();}
return x * f;
}
int n, x;
vector<int> vec;bool book[1 << 18];
signed main(){
n = read(); book[x = read()] = 1;
for(int i = 1;i < (1 << n);i++){if(!book[i]){vec.push_back(i);book[i] = book[i ^ x] = 1;}}
if(vec.empty()){puts("0");return 0;}
for(int i = vec.size() - 1;i;i--){vec[i] ^= vec[i - 1];}
printf("%d\n", vec.size());for(int i : vec)printf("%d ",i);
return 0;
}
CF1174F
CF1174F题意
交互题
给出一棵以1为根的树(
n
≤
2
×
1
0
5
n\le2\times10^5
n≤2×105),其中有一个节点
x
x
x,你需要找出它。
你可以进行以下两种询问:
- d u d\space u d u:交互库会回答你 x → u x\to u x→u 的距离。
- s u s\space u s u:交互库会回答你 u → x u\to x u→x 路径中从 u u u 数起第二个点的编号。不过需要注意,你需要保证 u u u 必须是 x x x 的祖先,否则直接wrong answer。
你只能问不多于 36 36 36 次。
CF1174F题解
考虑树链剖分。
首先先问一次x到根节点的距离,即x的深度(记为
d
e
p
t
h
depth
depth)。
然后考虑从根节点出发。
每次从
u
u
u 走到
u
u
u 重链最低处
v
v
v,询问
v
v
v 到节点的距离
d
i
s
t
dist
dist。
分为以下情况:
- 0 0 0 :答案就是 v v v,输出即可
- 1 1 1 :答案就是 f a v fa_v fav,输出即可
- 否则通过
d
e
p
t
h
+
d
e
p
v
−
2
×
d
e
p
L
C
A
=
d
i
s
t
depth+dep_v-2\times dep_{LCA}=dist
depth+depv−2×depLCA=dist 算出
d
e
p
L
C
A
dep_{LCA}
depLCA,同时从
v
v
v 跳
d
e
p
v
−
d
e
p
L
C
A
dep_v-dep_{LCA}
depv−depLCA 步,这就是
L
C
A
LCA
LCA ,考虑
d
e
p
L
C
A
dep_{LCA}
depLCA的大小
- d e p L C A = d e p t h dep_{LCA}=depth depLCA=depth:答案就是这个 L C A LCA LCA
- 否则询问 s L C A s\space LCA s LCA 以决定下一步向哪个方向前进
时间复杂度分析:
由于每个点最多走两次,时间复杂度就是
O
(
n
)
O(n)
O(n)
特殊条件限制:
由于特判了三种情况,覆盖了所有可能的非法询问,故不会WA。
然后就没了(
CF1174F代码
#include<bits/stdc++.h>
using namespace std;
inline int read(){
int x = 0, f =1;char ch = getchar();
while(ch <'0' || ch > '9'){if(ch == '-') f = -1;ch = getchar();}
while(ch >= '0' && ch <= '9'){x = (x << 1) + (x << 3) + (ch ^ 48);ch = getchar();}
return x * f;
}
const int maxn = 2e5 + 10;
int n;
vector<int> edg[maxn];
int fa[maxn], dep[maxn], son[maxn], siz[maxn];
void dfs(int u,int f){
fa[u] = f;dep[u] = dep[f] + 1;siz[u] = 1;
for(int v : edg[u]){
if(v != f){
dfs(v, u); siz[u] += siz[v];
son[u] = siz[son[u]] > siz[v] ? son[u] : v;
}
}
}
int depth;
void dfs1(int u){
int v = u, d = 0;
while(son[v])v = son[v];
cout << "d " << v << endl;cin >> d;
if(d == 1){cout << "! " << fa[v] << endl;exit(0);}
int deplca = (depth + dep[v] - d) / 2, tmp = v;
while(dep[v] != deplca)v = fa[v];
if(d == 0 || deplca == depth){cout << "! " << v << endl;exit(0);}
cout << "s " << v << endl;int t;cin >> t;
if(son[v] == t){
for(int i = 1;i <= d;i++){tmp = fa[tmp];}
cout << "! " << tmp << endl;exit(0);
}
dfs1(t);
}
signed main(){
cin >> n;int u, v;
for(int i = 1;i < n;i++){
cin >> u >> v;
edg[u].push_back(v);edg[v].push_back(u);
}
dep[0] = -1;dfs(1, 0);
cout << "d 1" << endl;cin >> depth;
if(!depth){cout << "! 1" << endl;return 0;}
dfs1(1);
return 0;
}