跳到正文

个人记录

25年-夏-第七次慧友中学互助赛 S7 题解

keep_on约 8 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月15日
篇幅
约 8 分钟

版本

  1. v1当前

25年-夏-第七次慧友中学互助赛 S7 题解

比赛传送门 在视频讲评之前请先把此题解看完,我在讲评时会默认你已经通读全文。 本文阅读时间约为 4545 分钟。 文章右侧有小标题,可以根据需求阅览。

  • T1 签到题,very easy 。
  • T2 三分,看出来就没问题了。
  • T3 图论,不超纲。
  • T4 DP 很有意思。

T1:解密

看这个解密过程,用队列模拟密码准没错:

C++
#include <bits/stdc++.h>
using namespace std;
queue<char> q;
queue<char> x;
char s;
int main()
{
    //freopen("declassify.in","r",stdin);
    //freopen("declassify.out","w",stdout);
    s=getchar();
    while(s>='0' && s<='9') {
        q.push(s);
        s=getchar();
    }
    while(!q.empty()) {
        x.push(q.front());
        q.pop();
        if(!q.empty()) {
            q.push(q.front());
            q.pop();
        }
    }
    while(!x.empty()) {
        putchar(x.front());
        x.pop();
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

其中 getchar() 可以读入一个字符,putchar() 可以输出一个字符,这两个语句读写速度很快,但风险高,具体可以参见深基第六章。 本题使用 char 型比 int 要好一些,亲测 int + scanf 卡常失败,#20 跑了 974974 ms

T2:不加糖咖啡

两种思路,第一种考虑二分不满意度的最大值,但是太麻烦;第二种考虑三分 mm 的值。 本题数据范围较大,不适合暴力枚举或打表。

C++
#include <bits/stdc++.h>
using namespace std;
#define d double
int n;
d a[1000005],x[1000005],m,inf=1e16+5,eps=1e-5;
d calc(d num) {  //计算
    d s=0;
    for(int i=1;i<=n;i++)
        s=max(s,x[i]*abs(a[i]-num));
    return s;
}
int main()
{
    //freopen("coffee.in","r",stdin);
    //freopen("coffee.out","w",stdout);
    cin>>n;
    for(int i=1;i<=n;i++)
        scanf("%lf %lf",&a[i],&x[i]);
    d l=0,r=inf;
    while(r-l>=eps) {  //三分
        d mid1=(2*l+r)/3,mid2=(l+2*r)/3;
        d f1=calc(mid1),f2=calc(mid2);
        if(f1>f2) l=mid1;
        else r=mid2;
    }
    printf("%.3lf",(l+r)/2);
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

T3:溶洞

C++
#include <bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
const int maxm=2e6+5;
int n,m,k,cnt;
long long wt[maxn];
struct cave {
    int pos,t;
};
int h[maxn];
queue<cave> Q;
bool book[maxn];
struct _edge {
    int to,nxt;
}edge[maxm];
void add(int u,int v)
{
    edge[++cnt].to=v;
    edge[cnt].nxt=h[u];
    h[u]=cnt;
}
void clear()
{
    for(int i=1;i<=n;i++)
        book[i]=false;
}
void bfs(int pos,int dis,int rain)
{
    Q.push((cave){pos,0});
    book[pos]=true;
    while(!Q.empty()) {
        bool flag=false;
        cave r=Q.front();
        wt[r.pos]+=rain;
        Q.pop();
        for(int i=h[r.pos];i!=0;i=edge[i].nxt) {
            if(!book[edge[i].to]) {
                if(r.t==dis) {
                    break;
                }
                Q.push((cave){edge[i].to,r.t+1});
                book[edge[i].to]=true;
            }
        }
    }
}
int main()
{
    //freopen("cave.in","r",stdin);
    //freopen("cave.out","w",stdout);
    scanf("%d %d %d",&n,&m,&k);
    for(int i=1;i<=n;i++)
        scanf("%d",&wt[i]);
    for(int i=1,u,v;i<=m;i++) {
        scanf("%d %d",&u,&v);
        add(u,v);
        add(v,u);
    }
    for(int i=1,w,a,x;i<=k;i++) {
        scanf("%d %d %d",&w,&a,&x);
        bfs(w,a,x);
        clear();
    }
    for(int i=1;i<=n;i++)
        printf("%lld ",wt[i]);
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

T4:幸运指数 II

C++
#include <bits/stdc++.h>
using namespace std;
const int maxn=1005;
int n,m;
int dp[maxn][maxn][2],a[maxn][maxn][2];
bool is_zero,zero[maxn][maxn];
int main()
{
    //freopen("luck.in","r",stdin);
    //freopen("luck.out","w",stdout);
    scanf("%d %d",&n,&m);
    for(int i=1;i<=n;i++) {
        for(int j=1,x,y;j<=m;j++) {
            scanf("%d",&x);
            if(x==0) {
                is_zero=true;
                zero[i][j]=true;
                continue;
            }
            y=x;
            for(;y%2==0;y/=2) a[i][j][0]++;
            y=x;
            for(;y%5==0;y/=5) a[i][j][1]++;
        }
    }
    for(int i=1;i<=n;i++) {
        for(int j=1;j<=m;j++) {
            for(int k=0;k<2;k++) {
                dp[i][j][k]=max(dp[i-1][j][k],dp[i][j-1][k])+a[i][j][k];
            }
        }
    }
    int ans=min(dp[n][m][0],dp[n][m][1]);
    if(is_zero && ans==0) ans=1;
    printf("%d\n",ans);
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

难度:橙黄黄绿

原文