A. We Need the Zero
题意:
给定序列 a a a,选定 x x x,则 b i = a i ⊕ x b_i = a_i \oplus x bi=ai⊕x,是否存在 x x x 使 b 1 ⊕ b 2 ⊕ . . . ⊕ b n = 0 b_1\oplus b_2\oplus...\oplus b_n = 0 b1⊕b2⊕...⊕bn=0
解析:
异或运算满足交换律和结合律。 b 1 ⊕ b 2 ⊕ . . . ⊕ b n = ( a 1 ⊕ x ) ⊕ ( a 2 i ⊕ x ) ⊕ . . . ⊕ ( a n ⊕ x ) b_1\oplus b_2\oplus...\oplus b_n = (a_1 \oplus x)\oplus (a_2i \oplus x)\oplus...\oplus (a_n \oplus x) b1⊕b2⊕...⊕bn=(a1⊕x)⊕(a2i⊕x)⊕...⊕(an⊕x) b 1 ⊕ b 2 ⊕ . . . ⊕ b n = ( a 1 ⊕ a 2 ⊕ . . . ⊕ a n ) ⊕ ( x ⊕ x ⊕ . . . ⊕ x ) b_1\oplus b_2\oplus...\oplus b_n = (a_1\oplus a_2\oplus ... \oplus a_n) \oplus (x\oplus x\oplus...\oplus x) b1⊕b2⊕...⊕bn=(a1⊕a2⊕...⊕an)⊕(x⊕x⊕...⊕x)当 n n n 为偶数时, ( x ⊕ x ⊕ . . . ⊕ x ) = 0 (x\oplus x\oplus...\oplus x) = 0 (x⊕x⊕...⊕x)=0, b 1 ⊕ b 2 ⊕ . . . ⊕ b n = ( a 1 ⊕ a 2 ⊕ . . . ⊕ a n ) b_1\oplus b_2\oplus...\oplus b_n = (a_1\oplus a_2\oplus ... \oplus a_n) b1⊕b2⊕...⊕bn=(a1⊕a2⊕...⊕an)
当 n n n 为奇数时, b 1 ⊕ b 2 ⊕ . . . ⊕ b n = ( a 1 ⊕ a 2 ⊕ . . . ⊕ a n ) ⊕ x b_1\oplus b_2\oplus...\oplus b_n = (a_1\oplus a_2\oplus ... \oplus a_n) \oplus x b1⊕b2⊕...⊕bn=(a1⊕a2⊕...⊕an)⊕x,即 x = a 1 ⊕ a 2 ⊕ . . . ⊕ a n x = a_1\oplus a_2\oplus ... \oplus a_n x=a1⊕a2⊕...⊕an
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define fi first
#define se second
const int maxn = 1e5+10;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;
int a[maxn];
int n, x;
void solve(){
cin >> n;
int y = 0;
for(int i = 1; i <= n; i++){
cin >> a[i];
y = y ^ a[i];
}
//cout << "ans = ";
if(n%2 == 0){
if(y == 0)
cout << 0 << endl;
else
cout << -1 << endl;
}
else{
if(y == 0)
cout << 0 << endl;
else
cout << y << endl;
}
return;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T;
cin >> T;
while(T--)
solve();
return 0;
}
B. The String Has a Target
题意:
给定字符串,一次操作可以选定一个字符,将该字符移到串首。询问操作一次得到的字典序最小的字符串。
解析:
贪心。选择最小的字符中最靠后的那个字符,移到串首。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define fi first
#define se second
const int maxn = 1e5+10;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;
string str;
int n;
void solve(){
cin >> n;
cin >> str;
int minn = (int)'z';
int pos = 0;
for(int i = 0; i < str.size(); i++){
if(minn >= str[i]){
minn = (int)str[i];
pos = i;
}
}
cout << (char)minn;
for(int i = 0; i < str.length(); i++){
if(i == pos)
continue;
cout << str[i];
}
cout << endl;
return;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T;
cin >> T;
while(T--)
solve();
return 0;
}
C. Place for a Selfie
题意:
给定一些过原点的直线,给定一些 a > 0 a > 0 a>0 的抛物线。对每条抛物线,询问直线集合中是否存在一条直线与该抛物线不相交。
解析:
设抛物线为 y = a x 2 + b x + c y = ax^2+bx+c y=ax2+bx+c,直线为 y = k x y = kx y=kx,联立得 a x 2 + ( b − k ) x + c = 0 ax^2+(b-k)x+c = 0 ax2+(b−k)x+c=0 。
不相交则方程无实根: Δ = ( b − k ) 2 − 4 a c < 0 \Delta = (b-k)^2-4ac < 0 Δ=(b−k)2−4ac<0
- 当 c ≤ 0 c \le 0 c≤0 时, Δ ≥ 0 \Delta \ge 0 Δ≥0 恒成立,直线不存在
- 当 c > 0 c > 0 c>0 时, b − 2 a c < k < b + 2 a c b-2\sqrt{ac} < k < b+2\sqrt{ac} b−2ac<k<b+2ac
将所有直线按斜率排序,然后二分查找即可。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define fi first
#define se second
const int maxn = 1e5+10;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;
struct node{
db k;
int id;
node(db k, int id) : k(k), id(id){}
bool operator < (const node &b) const{
return k < b.k;
}
};
struct parabola{
db a, b, c;
}a[maxn];
bool cmp(node x, node y){
return x.k < y.k;
}
vector<node> slope;
int n, m;
void solve(){
cin >> n >> m;
slope.clear();
for(int i = 1; i <= n; i++){
db k; scanf("%lf", &k);
slope.push_back(node(k, i));
}
sort(slope.begin(), slope.end());
for(int i = 1; i <= m; i++){
db a, b, c;
scanf("%lf %lf %lf", &a, &b, &c);
if(c <= 0)
cout << "no" << endl;
else{
db mink = b-2.0*sqrt(a*c);
db maxk = b+2.0*sqrt(a*c);
auto it = upper_bound(slope.begin(), slope.end(), node(mink, 0));
if(it != slope.end()){
if((*it).k < maxk){
cout << "yes" << endl;
cout << (int)(it->k) << endl;
}
else
cout << "no" << endl;
}
else
cout << "no" << endl;
}
}
cout << endl;
return;
}
int main(){
int T;
cin >> T;
while(T--)
solve();
return 0;
}
D. A Wide, Wide Graph
题意:
给定一棵树。对于整数 k k k,如果树上两点距离大于等于 k k k ,则该两点在图 G k G_k Gk 中有边。
询问图 G 1 , G 2 , . . . G n G_1,G_2,...G_n G1,G2,...Gn 中连通块的数目。
解析:
首先可以想到,树上距离最远的两点之间的距离是树的直径。如果 k k k 大于等于直径,在图 G k G_k Gk 中,所有点都是孤立的点。
对一个点 u u u 来说,与该点距离最远的点一定是树直径的端点。设这个最远距离为 d d d ,如果 d < k d < k d<k,则 u u u 在图中为孤立的点;如果 d ≥ k d \ge k d≥k,则 u u u 一定和直径端点处于同一连通块
对图 G k G_k Gk 来说,一个是含直径的连通块,其余的点是孤立的点。
因此,找到树的直径之后,处理出每个点到树上最远点的距离 d i d_i di ,当 k = d i + 1 k = d_i+1 k=di+1 时, u u u 成为孤立点。随着 k k k 增大 i i i 一直是孤立点。所以可以得到答案的差分数组,最后求一遍前缀和即可得到答案数组。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define fi first
#define se second
const int maxn = 1e5+10;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;
struct edge{
int to, nxt;
}e[maxn << 1];
int head[maxn], tot;
void add(int a, int b){
e[++tot].nxt = head[a];
e[tot].to = b;
head[a] = tot;
}
int n;
int dep1[maxn], dep2[maxn], dis[maxn];
int ans[maxn];
void dfs1(int u, int fa){
dep1[u] = dep1[fa]+1;
for(int i = head[u]; i; i = e[i].nxt){
int v = e[i].to;
if(v == fa)
continue;
dfs1(v, u);
}
}
void dfs2(int u, int fa){
dep2[u] = dep2[fa]+1;
for(int i = head[u]; i; i = e[i].nxt){
int v = e[i].to;
if(v == fa)
continue;
dfs2(v, u);
}
}
void solve(){
cin >> n;
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
add(u, v);
add(v, u);
}
dfs1(1, 0);
int d1 = 0, d2 = 0, maxx = 0;
for(int i = 1; i <= n; i++){
if(dep1[i] > maxx){
maxx = dep1[i];
d1 = i;
}
}
dfs1(d1, 0);
maxx = 0;
for(int i = 1; i <= n; i++){
if(dep1[i] > maxx){
maxx = dep1[i];
d2 = i;
}
}
dfs2(d2, 0);
for(int i = 1; i <= n; i++){
dis[i] = max(dep1[i], dep2[i]);
ans[dis[i]] += 1;
}
for(int i = 1; i <= n; i++){
ans[i] += ans[i-1];
if(i <= dep2[d1]-1)
cout << 1+ans[i] << " ";
else
cout << ans[i] << " ";
}
cout << endl;
return;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
solve();
return 0;
}
E. There Should Be a Lot of Maximums
题意:
给定一个树。一棵子树的价值为:出现两次以上的点权最大值。一条边的价值为:删除该条边,形成的两棵子树价值的最大值。
询问每条边的价值。
解析:
树上启发式合并(DSU ON TREE)。
如果一个点权只出现一次,一定对答案没有贡献。统计出现两次的点权的次数。维护两个集合:当前子树内出现两次及以上的点权,当前子树外出现两次及以上的点权。
因为点权很大,需要离散化。
代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
#define fi first
#define se second
#define mkp(a, b) make_pair((a),(b))
const int maxn = 1e5+10;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> pii;
struct edge{
int to, nxt;
}e[maxn << 1];
int head[maxn], tot;
pii edg[maxn];
void add(int a, int b){
e[++tot].nxt = head[a];
e[tot].to = b;
head[a] = tot;
}
int dep[maxn], dfn[maxn], dfntot, pre[maxn], son[maxn], siz[maxn];
int n, a[maxn];
vector<int> d;
int cnt1[maxn], cnt2[maxn];
set<int> s1, s2;
int res[maxn];
void add(int u){
int x = a[u];
cnt1[x]++;
if(cnt1[x] == 2)
s1.insert(x);
cnt2[x]--;
if(cnt2[x] == 1)
s2.erase(x);
}
void update(int u){
for(int i = dfn[u]; i <= dfn[u]+siz[u]-1; i++)
add(pre[i]);
}
void del(int u){
int x = a[u];
cnt1[x]--;
if(cnt1[x] == 1)
s1.erase(x);
cnt2[x]++;
if(cnt2[x] == 2)
s2.insert(x);
}
void clear(int u){
for(int i = dfn[u]; i <= dfn[u]+siz[u]-1; i++)
del(pre[i]);
}
int query(){
int res = 0;
if(s1.size())
res = max(res, *s1.rbegin());
if(s2.size())
res = max(res, *s2.rbegin());
return res;
}
void dfs1(int u, int fa){
dep[u] = dep[fa]+1;
dfn[u] = ++dfntot;
pre[dfntot] = u;
siz[u] = 1;
for(int i = head[u]; i; i = e[i].nxt){
int v = e[i].to;
if(v == fa)
continue;
dfs1(v, u);
siz[u] += siz[v];
if(siz[v] > siz[son[u]])
son[u] = v;
}
}
void dfs2(int u, int fa, int flag){
for(int i = head[u]; i; i = e[i].nxt){
int v = e[i].to;
if(v == son[u] || v == fa)
continue;
dfs2(v, u, 0);
}
if(son[u])
dfs2(son[u], u, 1);
for(int i = head[u]; i; i = e[i].nxt){
int v = e[i].to;
if(v == son[u] || v == fa)
continue;
update(v);
}
add(u);
res[u] = query();
if(flag == 0)
clear(u);
}
void solve(){
cin >> n;
for(int i = 1, u, v; i < n; i++){
cin >> u >> v;
add(u, v);
add(v, u);
edg[i] = mkp(u, v);
}
for(int i = 1; i <= n; i++){
cin >> a[i];
d.push_back(a[i]);
}
d.push_back(0);
sort(d.begin(), d.end());
d.erase(unique(d.begin(), d.end()), d.end());
int m = d.size();
for(int i = 1; i <= n; i++)
a[i] = lower_bound(d.begin(), d.end(), a[i])-d.begin();
for(int i = 1; i <= n; i++){
cnt2[a[i]]++;
}
for(int i = 1; i <= n; i++){
if(cnt2[i] >= 2)
s2.insert(i);
}
dfs1(1, 0);
dfs2(1, 0, 0);
for(int i = 1; i < n; i++){
int u = edg[i].fi;
int v = edg[i].se;
if(dep[u] < dep[v])
swap(u, v);
int ans = d[res[u]];
cout << ans << endl;
}
return;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T = 1;
while(T--)
solve();
return 0;
}