周三习题课
- 比赛日期:
- 比赛时长:
- 比赛得分:
- 比赛排名:
比赛整体回顾
也是非常的激动好吧,第一次断网拿 ,做题顺序为:,自认为难度为(由低到高):, 题花费了亿点点时间和耐心……
题目复盘
A - Family Tree B
题面戳这儿
分数
, 个点
题意
给定一个家谱树,每父亲和叶子节点的连线代表一对母女关系,对于 有如下定义:
-
若 的母亲是同一个,则称他们俩是
SIBLINGS的关系 -
若 为直系关系:
-
是 的祖先:
- 母亲:称
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 - ,以此类推
- 母亲:称
-
是 的祖先:同理,将 位置调换即可
-
-
若 和 不是直系关系,但是是姨母关系:
-
是 的祖先:
- 是 的外祖母的孩子:称
x is the aunt of y - 是 的外曾外祖母的孩子:称
x is the great-aunt of y - 是 的外曾外曾外祖母的孩子:称
x is the great-great-aunt of y - ,以此类推
- 是 的外祖母的孩子:称
-
是 的祖先:同理,将 位置调换即可
-
-
若 和 不是直系关系也不是姨母关系,但是有亲戚关系,就称 和 是
COUSINS关系 -
如果 和 没有亲戚关系,就称 和 是
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。
标准思路
- :将奶牛名字编号
- :按顺序求出 的祖先列表
- :找到最近公共祖先,根据最近公共祖先,判断每一种情况
补题代码
#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;
}收获
花了 干出了 ,收获颇满。首先呢是这种家谱树类的问题其实就是可以把它转化成现实生活中我们所说的“族谱”,然后就很好做了。关于我是怎么想到的呢……(我把奶牛看成了人,然后就误打误撞的做出来了 ,也是悲喜交加好吧)
题目回顾
这道题有亿点点废手总结也很废手,不过还是想想想把它磕出来了,还是比较满意的,就是现在还没有搞清楚为啥并查集会错。
B - The Great Revegetation B
题面戳这儿
分数
,
题意
给定 对 位置,要求求出一个字典序最小的由 构成的序列,使得每对 位置在序列中代表的数都不同
考场思路
这题目能是个黄题,也是斐长的惊讶好吧,就是暴力,搞一个三重循环,应为他只要找字典序最小的满足要求的,所以对于序列上的每一个位置,直接枚举 ~,如果满足要求就直接 break 掉(类似于 里的最优性剪枝),然后对于每一个位置枚举到的数,再从 循环到 ,检查一下每一组是否不同即可。
考场代码
#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,没有什么收获,主要就是要仔细,不然就会像很惨的伍*泽一样错失 分。
题目回顾
再说一次:这道题能是黄题,也是斐长的惊讶,自认为这道题是
C - Swapity Swapity Swap S
题面戳这儿
分数
, 个点
题意
给定一个序列 ,对这个序列进行 组操作:每一组又分为 次,每一次可以将对应的 进行翻转,问 组操作完之后的序列长什么样
考场思路
暴力也是对龙杞瑞的999ms卡过去了心生佩服(注意到全班只有一个人没用暴力做)
考场代码
#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;
}错因
如果 里的数存的过多(稠密图),那么时间复杂度就会被降到 ,就会炸。