当前位置:网站首页>2022年湖南工学院ACM集训第二次周测题解
2022年湖南工学院ACM集训第二次周测题解
2022-07-03 10:59:00 【qq_51034140】
A题 Will的失踪
来源:改编自解救
考察:约瑟夫问题的掌握程度
难度:橙题
本题是经典得约瑟夫问题,区别在于报数是固定为 2,且只需要输出最后一个人的编号,不懂的去搜约瑟夫问题。
B题 圣诞灯光
来源:改编自表达式括号匹配
考察:栈的基本运用
难度:橙题
本题用一个stack去存括号,若当前括号为 { ,则放入stack,否则检查stack是否有 { 或为空即可。
C题 霍金斯镇的地底
来源:改编自[JLOI2009]二叉树问题
考察:二叉树的基本运用
难度:橙题
本题可以用 dfs 实现对深度的求解,并在dfs的过程中记录每层的节点数量,最后找出最大的即为宽度。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int n,m,fa[1005],de[1005],tot,head[1005],s,kuan,shen,pika[1005];
struct node
{
int to,next;
}edge[1005];
void add(int u,int v)
{
edge[++tot].to=v;
edge[tot].next=head[u];
head[u]=tot;
}
void dfs(int x,int fath)
{
fa[x]=fath;
de[x]=de[fath]+1;
for(int i=head[x];i;i=edge[i].next)
{
if(edge[i].to!=fath)
{
dfs(edge[i].to,x);
}
}
}
int main()
{
cin>>n;
for(int i=1;i<n;i++)
{
int x,y;
cin>>x>>y;
add(x,y);
add(y,x);
}
dfs(1,0);
for(int i=1;i<=n;i++)
{
shen=max(de[i],shen);
pika[de[i]]++;
}
for(int i=1;i<=shen;i++)
{
kuan=max(kuan,pika[i]);
}
cout<<shen<<" "<<kuan<<endl;
return 0;
}D题 最佳保姆
来源:牛客小白月赛6 H题-挖沟
考察:图论,最小生成树
难度:黄题
本题为最小生成树Kruskal板子题,不懂的可以去搜一下。
#include<bits/stdc++.h>
using namespace std;
struct edge
{
int s,t,d;
bool operator<(const edge& y)const
{
return d<y.d;
}
}e[500005];
int n,m,i,f[100005],ans;
int get(int x)
{
if(f[x]==x)return x;
return f[x]=get(f[x]);
}
int main()
{
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++)f[i]=i;
for(i=1;i<=m;i++)scanf("%d%d%d",&e[i].s,&e[i].t,&e[i].d);
sort(e+1,e+m+1);
for(i=1;i<=m;i++)if(get(e[i].s)!=get(e[i].t))
{
ans+=e[i].d;
f[get(e[i].s)]=get(e[i].t);
}
cout<<ans<<endl;
return 0;
}E题 冬日舞会
来源:周末舞会
考察:队列的基本运用
难度:橙题
本题为用queue存一下编号,若当前此人跳完了舞则出队列并重新加入队列即可,以此类推。
F题 星城之战(Easy Version)
来源:改编自牛客练习赛101 B题-荒神在此
考察:dp(状态机)
难度:黄题
对于每一位而言,他的总和奇偶性跟上一位是相关的,若上一位贡献为偶数,那么我们当前位贡献也得是偶数,否则就为奇数。若当前位 i 为偶数则 0-i 之间恰好只有 i - i / 2 + 1 个偶数使得当前位贡献为偶数,i / 2 个奇数使得当前位贡献为奇数,因为偶数 + 奇数 = 奇数。对于 i 为奇数时,不难发现偶数和奇数个数均为 (i + 1) / 2。算出奇数偶数个数后直接dp转移即可。
#include<bits/stdc++.h>
#define endl '\n'
typedef long long ll;
typedef unsigned long long ull;
const ll mod = 998244353;
using namespace std;
ll dp[20202000][3];//第二维的0代表偶数,1为奇数
int main() {
int n;
cin >> n;
dp[1][0] = 1, dp[1][1] = 1;
for (ll i = 2; i <= n; i++) {
ll x, y; //x 为当前位贡献为奇数的个数,y 为偶数
if (i % 2 == 0) {
x = i / 2, y = i + 1 - i / 2;
}
else {
x = (i + 1) / 2, y = (i + 1) / 2;
}
dp[i][1] = dp[i - 1][0] * x % mod;
dp[i][1] = (dp[i][1] + dp[i - 1][1] * y) % mod;
dp[i][0] = dp[i - 1][1] * x % mod;
dp[i][0] = (dp[i][0] + dp[i - 1][0] * y) % mod;
}
cout << dp[n][0] % mod;
return 0;
}G题 星城之战(Hard Version)
来源:改编自牛客练习赛101 B题-荒神在此
考察:dp(状态机),数论(整除分块)
难度:绿题
dp的方程跟 F 题一致,唯一的区别在于如何求出奇数偶数个数,本题由于数据范围较大,若暴力求解各 i 所拥有的奇数偶数个数,那么时间复杂度高达
,而数据范围为 1e5,极限情况为 1e10,显然会超时。因此本题需要使用整除分块的思想,时间复杂度可以优化至
,极限情况为 3e7 左右,不会超时。关于整除分块的做法可以参考【数论】整除分块(数论分块)_辞树c的博客-CSDN博客_整除分块题目。
FG两题也可以用自己枚举一些数,然后找规律,不难发现。
边栏推荐
- R language uses grid of gridextra package The array function combines multiple visual images of the lattice package horizontally, and the ncol parameter defines the number of columns of the combined g
- 2022 东北四省赛 VP记录/补题
- 活动预告 | 直播行业“内卷”,以产品力拉动新的数据增长点
- PHP Basics
- Leetcode 46: full arrangement
- Machine learning 3.2 decision tree model learning notes (to be supplemented)
- Solve undefined reference to`__ aeabi_ Uidivmod 'and undefined reference to`__ aeabi_ Uidiv 'error
- mysql使用update联表更新的方法
- Nestjs配置服务,配置Cookie和Session
- The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities
猜你喜欢

(数据库提权——Redis)Redis未授权访问漏洞总结

Application of high-precision indoor positioning technology in safety management of smart factory

How to clean up v$rman_ backup_ job_ Details view reports error ora-02030

Web安全总结

Excel快速跨表复制粘贴

外插散点数据

Leetcode 46: full arrangement

Numpy np. Max and np Maximum implements the relu function

同事写了一个责任链模式,bug无数...

Arctangent entropy: the latest SCI paper in July 2022
随机推荐
2022 northeast four provinces match VP record / supplementary questions
FL Studio 20 unlimited trial fruit arranger Download
Matlab extracts numerical data from irregular txt files (simple and practical)
C language utf8toutf16 (UTF-8 characters are converted to hexadecimal encoding)
Spl06-007 air pressure sensor (example of barometer)
ftp登录时,报错“530 Login incorrect.Login failed”
2022年中南大学夏令营面试经验
[vtk] interpretation of source code comments of vtkwindowedsincpolydatafilter
MATLAB提取不規則txt文件中的數值數據(簡單且實用)
Some common terms
Leetcode 46: full arrangement
Viewing binary bin files with notepad++ editor
软考中级软件设计师该怎么备考
R language uses the aggregate function to calculate the mean value (sum) of dataframe data grouping aggregation without setting na The result of RM calculation. If the group contains the missing value
Numpy np. Max and np Maximum implements the relu function
抓包整理外篇fiddler———— 会话栏与过滤器[二]
How to become a senior digital IC Design Engineer (1-5) Verilog coding syntax: operand
uniapp scroll view 解决高度自适应、弹框滚动穿透等问题。
LeetCode 46:全排列
STL教程8-map