2026秋季YL第四周新课+习题课总结
周三习题课
-
比赛日期:
2026/9/30 -
比赛时长:
3h -
比赛得分:
308pts -
比赛排名:
No.1 比赛整体回顾
也是非常的激动好吧,第一次断网拿
No.1 ,做题顺序为:B \to C \to A \to D ,自认为难度为(由低到高):B \to A \to C \to D ,A 题花费了亿点点时间和耐心……前言
可以观看我写的“小作文”(建议翻到题目复盘
D 题的错因分析,无语了🤦)题目复盘
A - Family Tree B
题面戳这儿
分数
### 题意 给定一个家谱树,每父亲和叶子节点的连线代表一对母女关系,对于 $x,y$ 有如下定义: -
若
x,y 的母亲是同一个,则称他们俩是SIBLINGS的关系 -
若
x,y 为直系关系:-
- 母亲:称 `x is the mother of y` - 母亲的母亲(外祖母):称 `x is the grand-mother of y` - 母亲的母亲的母亲(外曾外祖母):称 `x is the great-grand-mother of y` - 母亲的母亲的母亲的母亲(外曾外曾外祖母):称 `x is the great-great-grand-mother of y` - $\dots$ $\dots$,以此类推 -
-
-
若
x 和y 不是直系关系,但是是姨母关系:-
- $x$ 是 $y$ 的外祖母的孩子:称 `x is the aunt of y` - $x$ 是 $y$ 的外曾外祖母的孩子:称 `x is the great-aunt of y` - $x$ 是 $y$ 的外曾外曾外祖母的孩子:称 `x is the great-great-aunt of y` - $\dots$ $\dots$,以此类推 -
-
-
若
x 和y 不是直系关系也不是姨母关系,但是有亲戚关系,就称x 和y 是COUSINS关系 -
如果
x 和y 没有亲戚关系,就称x 和y 是NOT RELATED关系
给定两个人
考场思路
考虑到名字是一个字符串,字符串不太好处理,所以我们可以给这些奶牛的名字编一个号,
我们可以把这个家谱树就想象成我们的祖宗十八代,我们维护一个 num_ge(int num, int r) 函数(num_generation),代表
直系关系其实很好求,这个关系他都是有规律的,从第二代开始,他的关系就是 great-grand-mother,所以我们只需要从
姨母关系其实也差不多,就是看一下 great-aunt。
接着就是处理是否有亲戚关系,可以使用并查集做。
考场代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 5;
int n, cnt = 2, fa[N];
string b, s, x, y;
map <string, int> id;
int num_ge(int num, int r){
for(int i = 1; i <= num; i++)
r = fa[r];
return r;
}
int find(int u){
if(fa[u] == u) return fa[u];
return find(fa[u]);
}
int main(){
cin >> n >> b >> s;
id[b] = 1, id[s] = 2;
for(int i = 1; i <= n; i++){
cin >> x >> y;
if(!id[x]) id[x] = ++cnt;
if(!id[y]) id[y] = ++cnt;
fa[id[y]] = id[x];
}
if(fa[1] == fa[2] && fa[1]){
cout << "SIBLINGS";
return 0;
}
if(fa[1] == 2){
cout << s << " is the mother of " << b;
return 0;
}
if(fa[2] == 1){
cout << b << " is the mother of " << s;
return 0;
}
for(int i = 2; i <= n; i++){
if(num_ge(i, 1) == 2){
cout << s << " is the ";
for(int j = 1; j <= i - 2; j++)
cout << "great-";
cout << "grand-mother of " << b;
return 0;
}
if(num_ge(i, 2) == 1){
cout << b << " is the ";
for(int j = 1; j <= i - 2; j++)
cout << "great-";
cout << "grand-mother of " << s;
return 0;
}
if(num_ge(i, 1) == fa[2] && fa[2]){
cout << s << " is the ";
for(int j = 1; j <= i - 2; j++)
cout << "great-";
cout << "aunt of " << b;
return 0;
}
if(num_ge(i, 2) == fa[1] && fa[1]){
cout << b << " is the ";
for(int j = 1; j <= i - 2; j++)
cout << "great-";
cout << "aunt of " << s;
return 0;
}
}
if(find(1) == find(2) && find(1)) cout << "COUSINS";
else cout << "NOT RELATED";
return 0;
}
错因
在处理 COUSINS 和 NOT RELATED 关系时,我用了一个并查集看看他们有没有公共祖先,但布兹道哪里出问题了,就
补题思路
借鉴了一下同桌大佬的思路,前面部分都是对的,但是在处理 COUSINS 和 NOT RELATED 关系时,我们可以考虑维护一个 f 数组,如果 f[i] = true,就代表 f[j] = true,那么就说明 COUSINS,否则输出 NO RELATED。
标准思路
-
-
-
### 补题代码 ```cpp line-numbers #include <bits/stdc++.h> using namespace std; const int N = 1e2 + 5; int n, cnt = 2, fa[N]; map <string, int> id; string b, s, x, y; bool f[N]; int num_ge(int num, int r){ for(int i = 1; i <= num; i++) r = fa[r]; return r; } int main(){ cin >> n >> b >> s; id[b] = 1, id[s] = 2; for(int i = 1; i <= n; i++){ cin >> x >> y; if(!id[x]) id[x] = ++cnt; if(!id[y]) id[y] = ++cnt; fa[id[y]] = id[x]; } if(fa[1] == fa[2] && fa[1]){ cout << "SIBLINGS"; return 0; } if(fa[1] == 2){ cout << s << " is the mother of " << b; return 0; } if(fa[2] == 1){ cout << b << " is the mother of " << s; return 0; } for(int i = 2; i <= n; i++){ if(num_ge(i, 1) == 2){ cout << s << " is the "; for(int j = 1; j <= i - 2; j++) cout << "great-"; cout << "grand-mother of " << b; return 0; } if(num_ge(i, 2) == 1){ cout << b << " is the "; for(int j = 1; j <= i - 2; j++) cout << "great-"; cout << "grand-mother of " << s; return 0; } if(num_ge(i, 1) == fa[2] && fa[2]){ cout << s << " is the "; for(int j = 1; j <= i - 2; j++) cout << "great-"; cout << "aunt of " << b; return 0; } if(num_ge(i, 2) == fa[1] && fa[1]){ cout << b << " is the "; for(int j = 1; j <= i - 2; j++) cout << "great-"; cout << "aunt of " << s; return 0; } } int a = 1, b = 2; while(a){ f[a] = true; a = fa[a]; } while(b){ if(f[b]){ cout << "COUSINS"; return 0; } b = fa[b]; } cout << "NOT RELATED"; return 0; } ``` ### 收获 花了 $1h$ 干出了 $\color{green} \text{88}$ $pts$,收获颇满。首先呢是这种家谱树类的问题其实就是可以把它转化成现实生活中我们所说的“族谱”,然后就很好做了。关于我是怎么想到的呢……(我把奶牛看成了人,然后就误打误撞的做出来了 $\color{green} \text{88}$ $pts$,也是悲喜交加好吧) ### 题目回顾 这道题有亿点点废手~~总结也很废手~~,不过还是想想想把它磕出来了,还是比较满意的,就是现在还没有搞清楚为啥并查集会错。 ## B - The Great Revegetation B #### [题面戳这儿](https://www.luogu.com.cn/problem/P1694) ### 分数 $\color{green} \text{100}$ $pts$,$\color{green} \text{Accepted} 题意
给定
m 对x,y 位置,要求求出一个字典序最小的由1,2,3,4 构成的序列,使得每对x,y 位置在序列中代表的数都不同考场思路
这题目能是个黄题,也是斐长的惊讶好吧,就是暴力,搞一个三重循环,应为他只要找字典序最小的满足要求的,所以对于序列上的每一个位置,直接枚举
1 ~4 ,如果满足要求就直接break掉(类似于dfs 里的最优性剪枝),然后对于每一个位置枚举到的数,再从1 循环到m ,检查一下每一组是否不同即可。考场代码
#include <bits/stdc++.h> using namespace std; const int N = 2e2 + 5; int n, m, g[N], a[N], b[N]; int main(){ cin >> n >> m; for(int i = 1; i <= m; i++) cin >> a[i] >> b[i]; for(int i = 1; i <= n; i++) for(int j = 1; j <= 4; j++){ bool ok = true; g[i] = j; for(int k = 1; k <= m; k++){ if(a[k] > i || b[k] > i) continue; if(g[a[k]] == g[b[k]]){ ok = false; break; } } if(ok){ cout << j; break; } else g[i] = 0; } return 0; }标准思路
基本一致
其实是一模一样收获
就是一个
brute force,没有什么收获,主要就是要仔细,不然就会像很惨的伍*泽一样错失20 分。题目回顾
再说一次:这道题能是黄题,也是斐长的惊讶,自认为这道题是
\color{red} \text{入门} C - Swapity Swapity Swap S
题面戳这儿
分数
### 题意 给定一个序列 $1,2,3,\dots,n$,对这个序列进行 $k$ 组操作:每一组又分为 $m$ 次,每一次可以将对应的 $[l_i,r_i]$ 进行翻转,问 $k$ 组操作完之后的序列长什么样 ### 考场思路 暴力~~也是对龙杞瑞的999ms卡过去了心生佩服~~(注意到全班只有一个人没用暴力做) ### 考场代码 ```cpp line-numbers #include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, m, k, a[N], l[N], r[N]; int main(){ ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n >> m >> k; for(int i = 1; i <= n; i++) a[i] = i; for(int i = 1; i <= m; i++) cin >> l[i] >> r[i]; for(int i = 1; i <= k; i++) for(int j = 1; j <= m; j++) reverse(a + l[j], a + r[j] + 1); for(int i = 1; i <= n; i++) cout << a[i] << '\n'; return 0; } ``` ### 错因 这还要讲嘛,肯定过不去呀……(绷住) ### 标准思路 不难发现,在操作过程中不同位置之间是互不影响的,所以可以逐个位置进行分析。
对于每个位置,我们可以计算出一组操作后它的位置,考虑到这些操作是可逆的,所以经过若干次操作之后,肯定是会回到一开始的位置,这些位置便构成了一个环。
所以我们只需要找到每一个环,然后利用同余就可以快速求出
补题代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
int n, m, k, p, l[N], r[N], a[N], ans[N];
void find(int goal, int now, int cnt){
if(goal == now && cnt){
p = cnt;
return;
}
find(goal, a[now], cnt + 1);
}
int store(int now, int cnt){
if(!cnt) return now;
return store(a[now], cnt - 1);
}
void work(int goal, int now1, int now2, int cnt){
if(now1 == goal && cnt) return;
if(now1 != goal) ans[now1] = a[ans[now2]];
work(goal, a[now1], now1, cnt + 1);
}
int main(){
cin >> n >> m >> k;
for(int i = 1; i <= m; i++)
cin >> l[i] >> r[i];
for(int i = 1; i <= n; i++)
a[i] = i;
for(int i = 1; i <= m; i++)
while(l[i] < r[i]){
swap(a[l[i]], a[r[i]]);
l[i]++, r[i]--;
}
for(int i = 1; i <= n; i++){
if(!ans[i]){
find(i, i, 0);
ans[i] = store(i, k % p);
work(i, i, i, 0);
}
cout << ans[i] << '\n';
}
return 0;
}
收获
这道题是个绿题,也有点惊讶,感觉可以降黄,想到不难想,就是代码稍稍有那么一点点难实现
题目回顾
D - Triangles S
题面戳这儿
分数
考场思路
首先因为有负数,所以考虑坐标偏移,给每个数都加上
考场代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n, mod = 1e9 + 7, ans, x[N], y[N];
vector <int> f[N], g[N];
signed main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> x[i] >> y[i];
x[i] += 10000, y[i] += 10000;
f[x[i]].push_back(i);
g[y[i]].push_back(i);
}
for(int i = 1; i <= n; i++){
int sumx = 0, sumy = 0;
for(auto j: g[y[i]])
sumx += abs(x[i] - x[j]);
for(auto j: f[x[i]])
sumy += abs(y[i] - y[j]);
ans = (ans + sumx * sumy) % mod;
}
cout << ans;
return 0;
}
错因
如果