2021百度之星初赛第一场部分题解

本文解析了2021年百度之星初赛的部分题目,包括使用01背包解决鸽子问题、数学方法解答萌新问题、双向链表实现毒瘤的数据结构问题以及模拟猎人杀游戏。

写在前面

几个家长要求我写一些2021百度之星初赛第一场的题解。

1003 鸽子

原题链接

https://acm.hdu.edu.cn/showproblem.php?pid=6998
http://47.110.135.197/problem.php?id=5977

简单题解

就是有些操作可做可不做。自然想到了01背包。因此可以使用动态规划。

AC 代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PLL;
const LL INF=4e18;

const int MAXN=1e5+10;
LL dp[MAXN];//dp[j]为当前枚举的操作(含之前枚举过的操作)上,对于第j个位置的最小暗箱操作次数。

void solve(){
    memset(dp, -1, sizeof(dp));

    LL n, m, K, u, v;
    cin>>n>>m>>K;
    dp[K]=0;
    while(m--){
        LL u,v;
        cin>>u>>v;
        if (dp[u]==-1&&dp[v]==-1) {
            //什么都不用做
            continue;
        } else if (dp[u]==-1&&dp[v]!=-1) {
            dp[u]=dp[v];
            dp[v]+=1;
        } else if(dp[u]!=-1&&dp[v]==-1){
            dp[v]=dp[u];
            dp[u]+=1;
        } else{
            LL t1=dp[v], t2=dp[u];
            dp[v]=min(dp[v]+1, t2);
            dp[u]=min(dp[u]+1, t1); 
        }
    }
    for(LL i=1;i<=n;++i){
        //这里要特别注意,本题会卡最后一个空格。艹,PE了n次。
        cout<<dp[i];
        if (i!=n) {
            cout<<" ";
        }
    }
    cout<<"\n";
}

int main(){
#if 1
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#endif
    LL T;
    cin>>T;
    while (T--) {
        solve();
    }
    return 0;
}

1004 萌新

题目链接

https://acm.hdu.edu.cn/showproblem.php?pid=6999
http://47.110.135.197/problem.php?id=5974

题解

本题是一个数学题。
a   m o d   c = b   m o d   c a\ mod\ c=b\ mod\ c a mod c=b mod c,移项可得 ( a − b )   m o d   c = 0 (a-b)\ mod\ c=0 (ab) mod c=0
为了不减少通用性,我们假设 a > b a>b a>b
因此最大的 c c c 就是 a − b a-b ab。下面我们需要对 c c c 的最小值进行讨论。
a = = b a==b a==b 时候,最小的 c c c 2 2 2
a − b = = 1 a-b==1 ab==1 时候,也就是 a a a b b b 互质,不存在最大和最小的 c c c
a − b > 1 a-b>1 ab>1 时候,暴力枚举即可最小的 c c c 即可。如果找不到最小的 c c c,不存在最大和最小的 c c c
注意我们需要将时间复杂度控制在 O ( T × l o g ( a − b ) ) O(T \times log(a-b)) O(T×log(ab))

AC 代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PLL;
const LL INF=4e18;

int main() {
#if 1
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#endif
    //freopen("00.in", "r", stdin);
    //freopen("00.out", "w", stdout);
    LL T;
    cin>>T;
    while (T--) {
        LL a,b;
        cin>>a>>b;

        //a%c=b%c -> (a-b)%c=0
        LL maxx=abs(a-b);
        if (0==maxx) {
            //a==b
            if (a>1) {
                cout<<"2 "<<a<<"\n";
            } else {
                cout<<"-1 -1\n";
            }
        } else if (1==maxx) {
            cout<<"-1 -1\n";
        } else {
            //暴力枚举
            bool flag=true;
            for (LL i=2; i*i<=maxx; i++) {
                if (maxx%i==0) {
                    flag=false;
                    cout<<i<<" "<<maxx<<"\n";
                    break;
                }
            }
            if (flag) {
                cout<<maxx<<" "<<maxx<<"\n";
            }
        }

    }

    return 0;
}

1006 毒瘤的数据结构

题目链接

https://acm.hdu.edu.cn/showproblem.php?pid=7001
http://47.110.135.197/problem.php?id=5976

题解

考点数据结构的双向链表。
由于本题的数据比较大,5e6。自然不能去一一遍历。因此本题可以考虑使用双向链表或者并查集来完成。
道歉目前该题处于 TLE 状态,应该是 5e6 数据读取问题。已经解决
我算服了,HDU 服务器只能使用 fread() 方式通过,其他方式读取,全部都是 TLE。
为了这个 TLE,搞了几天,都要怀疑人生了。不过还是自己对这样海量数据读取不了解问题。

AC代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PLL;
const LL INF=4e18;

const int MAXN=5e6+10;
bool aa[MAXN];
LL nex[MAXN];
LL pre[MAXN];

namespace fastIO {
    static char buf[100000],*h=buf,*d=buf;//缓存开大可减少读入时间,看题目给的空间
    #define gc h==d&&(d=(h=buf)+fread(buf,1,100000,stdin),h==d)?EOF:*h++//不能用fread则换成getchar
    template<typename T>
    inline void read(T&x) {
        int f = 1;x = 0;
        register char c(gc);
        while(c>'9'||c<'0'){
            if(c == '-') f = -1;
            c=gc;
        }
        while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+(c^48),c=gc;
        x *= f;
    }
    template<typename T>
    void output(T x)
    {
        if(x<0){putchar('-');x=~(x-1);}
        static int s[20],top=0;
        while(x){s[++top]=x%10;x/=10;}
        if(!top)s[++top]=0;
        while(top)putchar(s[top--]+'0');
    }
}
using namespace fastIO;

int main(){
    LL n,op,x;
    read(n);
    pre[n+1]=n;nex[0]=1;
    for (LL i=1;i<=n;++i) {
        nex[i]=i+1;
        pre[i]=i-1;
    }
    for (LL i=1;i<=n;++i){
        LL op,x;
        read(op);
        read(x);
        if(op==1){
            //1操作
            if(aa[x]) {
                continue;
            }
            nex[pre[x]]=nex[x];
            pre[nex[x]]=pre[x];
            aa[x]=1;
        } else {
            if(x==nex[0]){
                cout<<nex[nex[0]]<<"\n";
            } else {
                cout<<nex[0]<<"\n";
            }
        }
    }

    return 0;
}

下面代码是使用并查集。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <utility>

using namespace std;
typedef long long LL;
typedef pair<LL, LL> PLL;
const LL INF=4e18;

namespace fastIO {
    static char buf[100000],*h=buf,*d=buf;//缓存开大可减少读入时间,看题目给的空间
    #define gc h==d&&(d=(h=buf)+fread(buf,1,100000,stdin),h==d)?EOF:*h++//不能用fread则换成getchar
    template<typename T>
    inline void read(T&x) {
        int f = 1;x = 0;
        register char c(gc);
        while(c>'9'||c<'0'){
            if(c == '-') f = -1;
            c=gc;
        }
        while(c<='9'&&c>='0')x=(x<<1)+(x<<3)+(c^48),c=gc;
        x *= f;
    }
    template<typename T>
    void output(T x)
    {
        if(x<0){putchar('-');x=~(x-1);}
        static int s[20],top=0;
        while(x){s[++top]=x%10;x/=10;}
        if(!top)s[++top]=0;
        while(top)putchar(s[top--]+'0');
    }
}
using namespace fastIO;

const int MAXN=5e6+10;
LL rel[MAXN];
//并查集
LL fa[MAXN];
LL sa[MAXN];

void init(LL n) {
    for (LL i=0; i<=n; i++) {
        fa[i]=i;
        sa[i]=1;
    }
}

LL find(LL x) {
    if (fa[x]!=x) {
        fa[x]=find(fa[x]);
    }
    return fa[x];
}

void merge(LL x, LL y) {
    x=find(x);
    y=find(y);
    if (x==y) {
        return;
    }
    fa[x]=y;
    sa[y]+=sa[x];
}

int main() {
#if 0
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#endif
    //freopen("00.in", "r", stdin);
    //freopen("00.out", "w", stdout);
    int n;
    read(n);
    init(n);
    rel[0]=1;

    for (int i=1; i<=n; i++) {
        int opt, x;
        read(opt);
        read(x);
        if (1==opt) {
            rel[x]=1;
            if (rel[x-1]==1) {
                merge(x-1, x);
            }
            if (rel[x]==rel[x+1]) {
                merge(x, x+1);
            }
        } else {
            int ans=find(1);
            if (ans>=x-1) {
                ans=find(x+1);
                if (ans<=n && rel[ans]==1) {
                    ans++;
                }
            } else {
                ans=ans+rel[ans];
            }
            printf("%d\n", ans);
        }
    }

    return 0;
}

1008 猎人杀

题目链接

https://acm.hdu.edu.cn/showproblem.php?pid=7003
http://47.110.135.197/problem.php?id=5975

题解

模拟题。应该是这次初赛最简单的一题。见鬼,竟然放在了最后一题。

AC 代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL, LL> PLL;
const LL INF=4e18;

const int MAXN = 50+10;
LL a[MAXN];
LL b[MAXN][MAXN];
bool vis[MAXN];
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    LL T;
    cin >> T;
    while(T--){
        LL n;
        cin>>n;

        //清数据
        memset(vis,0,sizeof(vis));

        LL cnt = n;
        LL lr = -1;//狼人
        bool flag = 0;
        for(LL i = 1; i <= n; i++){
            cin >> a[i];
            b[i][1] = a[i]; 
            if(a[i] == 1){
                lr = i;//找到狼的位置
            }
        }
        for(LL i = 1; i <= n; i++){
            for(LL j = 2; j <= n + 1; j++){
                cin >> b[i][j];
            }
        }

        LL k = 2;
        LL wz = b[lr][k];//第一晚 狼人杀人(可自刀) 
        while(lr != -1 && cnt > 2){
            if(lr == wz){//狼自杀 
                lr = -1;
                if(lr == -1){
                    cout <<"lieren\n";
                    break;
                }
            } else{//杀猎人 
                while(!vis[wz]){//如果猎人还活着 
                    vis[wz] = 1;//猎人死亡 
                    if(wz == lr){//如果下个死亡的是狼的话 
                        cout << "lieren\n";
                        flag = 1;
                        break;
                    }
                    cnt--;
                    if(cnt <= 2){//如果只剩下两个玩家的话 
                        cout <<"langren\n";
                        flag = 1;
                        break;
                    }
                    while(vis[b[wz][k]] == 1){//直到找出第wz行暗杀名单中最靠前且还活着的玩家。 
                        k++;
                    }
                    wz = b[wz][k];//更新 
                    k = 2;//将每个玩家的暗杀名单遍历下标重置。 
                }
                if(flag == 1){//游戏结束,跳出循环 
                    break;
                }
            }
        }
    }

    return 0;
}

总结

还是太水了,还有些题需要进一步考虑一下。

### 关于2024百度初赛第一场的题目解析 目前尚未有官方发布的关于2024百度初赛第一场的具体题目解析文档被广泛传播或公开引用。然而,基于以往的比赛惯例以及社区讨论的内容[^1],可以推测该比赛通常涉及算法设计、数据结构应用以及编程技巧等方面的知识。 #### 题目类型分析 根据过往几百度竞赛的特点,其题目可能涵盖但不限于以下几个方面: - **字符串处理**:此类问题经常考察参赛者对于复杂模式匹配的理解能力,例如KMP算法的应用场景或者正则表达式的高效实现方法。 - **动态规划 (Dynamic Programming)**:这是解决最优化问题的一种重要手段,在许多比赛中都会出现。它通过把原问题分解成相对简单的子问题来求解复杂问题的方法[^2]。 - **图论(Graph Theory)**:包括但不限于最小生成树(MST),单源最短路径(SSSP)等问题都是常见考点之一。这些都需要扎实的基础理论支持才能快速找到最优解答方案[^3]。 以下是针对假设性的几类典型问题提供一些通用思路和技术要点说明: --- #### 字符串处理案例解析 如果遇到需要频繁查询某个特定字符序列是否存在的情况,则可考虑构建AC自动机来进行批量检索操作;而对于仅需判断两个字符串之间关系的任务来说,双指针技术往往能够满足需求并保持较低时间复杂度O(n)[^4]。 ```python def is_subsequence(s, t): it = iter(t) return all(char in it for char in s) # Example Usage: print(is_subsequence("abc", "ahbgdc")) # Output: True ``` --- #### 动态规划实例讲解 当面临背包问题变种或是区间覆盖等相关挑战时,定义状态转移方程至关重要。比如经典的一维数组形式dp[i]=max(dp[i], dp[j]+w(i,j))表示从前j项选取若干物品放入容量不超过i的情况下所能获得的最大价值总和[^5]。 ```python def knapsack(weights, values, capacity): n = len(values) dp = [0]*(capacity+1) for i in range(1,n+1): w,v=weights[i-1],values[i-1] for j in reversed(range(capacity+1)): if j >=w : dp[j]= max(dp[j], dp[j-w]+v ) return dp[-1] # Example Usage: weights=[2,3,4] values =[3,4,5] capacity=5 result=knapsack(weights,values,capacity) print(result) # Output:7 ``` --- #### 图论基础概念复习 在面对连通性验证或者是寻找关键节点这样的任务时,BFS/DFS遍历配合并查集Union-Find的数据结构通常是首选策略。它们能够在几乎线性时间内完成大规模网络拓扑属性计算工作[^6]。 ```python from collections import defaultdict, deque class Graph: def __init__(self): self.graph = defaultdict(list) def addEdge(self,u,v): self.graph[u].append(v) self.graph[v].append(u) def BFS(self,start_vertex): visited=set() queue=deque([start_vertex]) while(queue): vertex=queue.popleft() if(vertex not in visited): visited.add(vertex) queue.extend(set(self.graph[vertex])-visited) return list(visited) g=Graph() edges=[[0,1],[0,2],[1,2],[2,3]] for u,v in edges:g.addEdge(u,v) res=g.BFS(2) print(res) #[2, 0, 1, 3] ``` 尽管以上只是部分可能涉及到的技术领域概述及其简单示例展示,但对于准备参加类似赛事的学习者而言已经具备相当指导意义了。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

努力的老周

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值