20261007 CSP-J模拟赛

· · 个人记录

1 连续档案 (game) (100/100)

:::info[连续档案-题面] ::: 水题,模拟即可

#include<bits/stdc++.h>
using namespace std;
int main() {
    freopen("game.in","r",stdin);
    freopen("game.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    long long n,c[1005],sum=0;
    memset(c,0,sizeof(c));
    c[1]=1;
    string a="",b;
    cin >> n;
    for (int i=1;i<=n;i++) {
        cin >> b;
        if (a==b) {
            c[i]=c[i-1]+1;
        } else {
            c[i]=1;
        }
        sum+=c[i];
        a=b;
    }
    cout << sum << endl;
    return 0;
} 

2 星图压缩(throw) (20/100)

:::info[星图压缩-题面] ::: 显然,题目没读清,没考虑首位进位然后就挂了,其实很简单

#include<bits/stdc++.h>
using namespace std;
int t,k;
string n;
int main() {
    freopen("throw.in","r",stdin);
    freopen("throw.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> t;
    for (int i=1;i<=t;i++) {
        cin >> k;
        cin >> n;
        cout << n;
        bool f=0;
        for (int i=k-1;i>=1;i--) {
            if (n[i]>='5') {
                int cnt=i-1;
                n[cnt]++;
                while (n[cnt]>'9' && cnt>0) {
                    n[cnt]-=10;
                    cnt--;
                    n[cnt]++;
                }
                if (n[cnt]>'9' && cnt==0) {
                    n[cnt]-=10;
                    f=1;
                }
            }
            n[i]='0';
            if (i<k-1) {
                n[i+1]='0';
            }
            cout << " -> ";
            if (f==1) {
                cout << 1;
            }
            cout << n;
        }
        cout << endl;
    }
}

3 双色信标(wait) (35/100)

:::info[双色信标-题面] ::: 直接暴力可以拿35,其实暴力时就已经想到前缀和了,l是1,r是-1,但就是没想到可以直接记录每一个前缀和值最早出现的地方,如果当前前缀和值和之前某一次记录相等,就说明这个区间是0,也就是l和r一样多,直接O(n)通过

#include<bits/stdc++.h>
using namespace std;
long long n,sum[2000005],dir[4000010],ans=0,con=0;
bool ac[4000010];
int main() {
    freopen("wait.in","r",stdin);
    freopen("wait.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    string s;
    cin >> n;
    cin >> s;
    dir[2000005]=-1;
    ac[2000005]=1;
    for (int i=0;i<n;i++) {
        if (s[i]=='l') {
            sum[i+1]=sum[i]+1;
        } else {
            sum[i+1]=sum[i]-1;
        }
        if (ac[sum[i+1]+2000005]==0) {
            ac[sum[i+1]+2000005]=1;
            dir[sum[i+1]+2000005]=i;
        } else {
            con=i-dir[sum[i+1]+2000005];
            if (con>ans) {
                ans=con;
            }
        }
    } 
    cout << ans << endl;
    return 0;
}
4$ 典礼席位$(comp)$ $(35/35)

:::info[典礼席位-题面] ::: :::info[典礼席位-标程] :::