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

写在前面

几个家长要求我写一些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;
}

总结

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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

努力的老周

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

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

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

打赏作者

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

抵扣说明:

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

余额充值