4/1模拟赛

· · 个人记录

T1:美丽数(beauty)

题意简介

一个数是美丽数当且仅当这个数的相邻两位都不相同,给定每个数出现了多少次,问最小美丽数是什么。

考场思路

贪心每一位,选最小的,但这是错的,所以又在贪心时判断,如果之后每隔一位都选这一个才能完成就必选。

考场代码

#include<bits/stdc++.h>
#define ll long long
using namespace std;
int a[10];
int main(){
    freopen("beauty1.in","r",stdin);//把1去掉就AC
    freopen("beauty.out","w",stdout);
    int T;
    cin >> T;
    while (T --){
        int n = 0;
        for (int i = 0;i < 10;i ++){
            cin >> a[i];
            n += a[i];
        }
        vector<int> s;
        for (int i = 1;i <= n;i ++){  
            int x = -1;
            for (int j = 0;j < 10;j ++){
                if ((a[j] > 0)){
                    if (a[j] == n - s.size() - a[j] + 1){
                        if ((s.size() == 0 && j != 0))
                        {   
                            x = j;
                            break;
                        }
                        else if (s.size() > 0 &&  (s[s.size() - 1] != j)){
                            x = j;
                            break;
                        }
                    } 
                    if ((s.size() == 0 && j != 0) && x == -1){
                        x = j;
                    }
                    else if (s.size() > 0 && (s[s.size() - 1] != j) && x == -1){
                        x = j;
                    }
                }
            }
            if (x != -1)
            s.push_back(x);
            a[x] --;
        }
        if (s.size() != n){
            cout << "-1\n";
        } 
        else{   
            for (int i : s){
                cout << i;
            }
            cout << "\n";
        }
    }
    return 0;
}

正解

贪心每一位,选最小的,在贪心时判断,如果之后每隔一位都选一个才能完成就必选。(与考场思路一样)

T2:

题目简介

## 考场思路 没写 ## 正解 (题解没太看懂,没太改懂) 我们可以在每个节点记录上⼦节点中权值较⼩值$A_i$,和较⼤值$B_i$。 我们先要找到选⼿$i$第⼀次会在哪输,那就是第⼀个$A_x > a_i$的祖先节点$x$。 第二次在$B_y > a_i$。($y$是$x$的祖先节点) # T3:他会输出啥(output) ## 题意简介 输出形如: ```cpp ans=0 for A in range(a,b,c): for B in range(d,e,f): ans=ans+B print(ans) ``` 代码运行的结果。 ## 考场思路 暴力模拟,发现后面可以传A,没写出这种情况,只得了10分。 ## 考场代码 ```cpp #include<iostream> #include<string> #define ll long long using namespace std; string s[10]; ll a,b,c,d,e,f; ll find(ll x,ll z,ll &a){ ll i=x; ll y=1; a=0; while(s[z][i]!=','&&s[z][i]!=')'){ if(s[z][i]=='-') y=-1; else if(s[z][i]<'0'||s[z][i]>'9') a=1e6+5; else a=a*10+s[z][i]-'0'; i++; } if(a!=1e6+5) a*=y; return i; } ll sum(ll st,ll ed,ll se){ if(se==0) return 0; ll k=0,f=0,l=0; if(se>0){ if(st>=ed) return 0; ll km=(ed-1-st)/se; if(km<0) return 0; k=km+1; f=st; l=st+km*se; } else{ ll sa=-se; if(st<=ed) return 0; ll km=(st-ed-1)/sa; if(km<0) return 0; k=km+1; f=st; l=st+km*se; } return (f+l)*k/2; } int main(){ // freopen("output.in","r",stdin); // freopen("output.out","w",stdout); ll ans=0; for(ll i=1;i<=5;i++) getline(cin,s[i]); find(find(find(15,2,a)+1,2,b)+1,2,c); find(find(find(15,3,d)+1,3,e)+1,3,f); for(ll i=a;;i+=c){ if(i>=b&&c>0) break; if(i<=b&&c<0) break; ll td=d,te=e; if(d==1e6+5) d=i; if(e==1e6+5) e=i; ans+=sum(d,e,f); e=te,d=td; } cout<<ans; return 0; } ``` ## 正解 根据题意模拟,只输入a,b,c,d,e,f,二号循环用等差数列优化。 # T4:游览计划(tour) ## 题意简介 给定一张n 个点、m 条无向边的简单图(无重边、无自环),两点之间的距离为最短路径的边数。 需要选出4 个互不相同的景点,计算路径 a→b、b→c、c→d 的最短距离之和,求这个和的最大值,并输出该最大值。 ## 考场代码 无 ## 正解 bfs求出短路f,次段路g,第三短路h,我们枚举两个点$a,b$,然后经过$s→b$的最远路径⼀定是 $f_a/g_a/h_a→a→b→f_b/g_b/h_b

其中之⼀。

代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 4e3 + 5;
int n,m;
vector<int>c[N];
int d[N][N];
int f[N],g[N],h[N];

int get(int i,int ban1,int ban2){
    if(f[i]!=ban1&&f[i]!=ban2)return f[i];
    if(g[i]!=ban1&&g[i]!=ban2)return g[i];
    return h[i];
}

void bfs(int x){
    queue<int>q;
    q.push(x);
    d[x][x]=0;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(int v:c[u]){
            if(d[x][v]==-1){
                d[x][v]=d[x][u]+1;
                q.push(v);
            }
        }
    }
}

int main(){
    cin>>n>>m;
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            d[i][j]=-1;

    for(int i=1;i<=m;i++){
        int x,y;
        cin>>x>>y;
        c[x].push_back(y);
        c[y].push_back(x);
    }

    for(int i=1;i<=n;i++)bfs(i);

    for(int i=1;i<=n;i++){
        f[i]=g[i]=h[i]=i;
        for(int j=1;j<=n;j++){
            if(j==i)continue;
            if(d[i][j]>d[i][f[i]]){
                h[i]=g[i];
                g[i]=f[i];
                f[i]=j;
            }else if(d[i][j]>d[i][g[i]]){
                h[i]=g[i];
                g[i]=j;
            }else if(d[i][j]>d[i][h[i]]){
                h[i]=j;
            }
        }
    }

    long long ans=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j)continue;
            int a=get(i,i,j);
            int b=get(j,i,j);
            if(a!=b&&a!=j&&b!=i){
                ans=max(ans,1LL*d[a][i]+d[i][j]+d[j][b]);
            }
        }
    }

    cout<<ans<<endl;
    return 0;
}