10-3模拟赛总结

· · 个人记录

算法题代码汇总

以下是4道不同算法题的完整可直接复制运行的C++代码:

::::info[T1] :::info[problem]

还原零一串(restore)

题目描述

小A最初有一个01串,长度为n,他执行了以下操作数次(可能没有进行操作):

不巧的是,小A忘了自己进行了几次操作,他只记得最终这个01串是什么。 小A想知道,他的原始字符串的最短长度是多少?

输入格式

从文件restore.in中读入数据。 输入第一行包含一个正整数T,表示测试用例的数量。 每个测试用例的第一行包含一个正整数n,表示这个01串最终的长度。 每个测试用例的第二行包含一个长度为n的字符串,表示这个01串最终是什么。

输出格式

输出到文件restore.out中。 对每个测试用例,输出一个非负整数,表示这个01串的原始字符串的最短长度,用换行隔开。

样例1输入

1
9
2 3 3 100
4 4 5 0111
6 5 7 10101
8 6 9 101010
10 7 11 1010110
12 1 13 1
14 2
15 10
16 2
17 11
18 10
19 1011011010

数据范围

对于100%的数据,满足1\le T\le 100,1\le n\le 2000。

:::

:::success[AC code]

#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    freopen("restore.in","r",stdin);
    freopen("restore.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0); 
    int T;
    cin>>T;
    vector<int> ans;
    while(T--){
        int len;string s;
        cin>>len>>s;
        int l=0,r=len-1;
        while(s[l]!=s[r] and l<=r) l++,r--;
        ans.push_back(r-l+1);
    }
    for(int i:ans) cout<<i<<endl;
    return 0;
}
/*
9
3
100
4
0111
5
10101
6
101010
7
1010110
1
1
2
10
2
11
10
1011011010*/
思路:双指针贪心

::: :::error[WA code]

无

::: :::::warning[代码问题] 没有问题 ::::: ::::info[T2] :::info[problem]

cyw 的学习计划(study)

题目描述

cyw 最近收到了一笔奖学金,于是她准备报一些课程来提升自己。

但是 cyw 想报的三门课开课时间是完全相同的 n 天,那么也就是说 cyw 每天只能选择其中一门课去上。

而对于第 i 天的课程,第一门课的价格为 a_i,第二门课的价格为 b_i,第三门课的价格为 c_i。

同时 cyw 不希望连续两天上同样的课程,这会让她觉得无聊。

现在她想知道,在保证每天都上一门课的情况下,学完这 n 天最少需要花多少钱?

输入格式

从文件 study.in 中读入数据。

第一行只有一个整数 n,表示课程有 n 天。

第二行有 n 个正整数 a_i,表示第一门课每天的价格。

第三行有 n 个正整数 b_i,表示第二门课每天的价格。

第四行有 n 个正整数 c_i,表示第三门课每天的价格。

输出格式

输出到文件 study.out 中。

输出仅有一行,该行只有一个整数,表示最小花费。

样例 1 输入

5
1 3 1 2 2
1 2 3 4 3
4 2 1 5 3

样例 1 输出

9

数据范围

::: :::success[AC code]

#include<bits/stdc++.h>
using namespace std;
#define int long long   
int dp[4]={0};
int clas[4][100001]={0};
int n,dp1,dp2,dp3;
signed main(){
    freopen("study.in","r",stdin);
    freopen("study.out","w",stdout);
    cin>>n;
    for(int j=1;j<=3;j++)
    for(int i=1;i<=n;i++){
        cin>>clas[j][i];
    } 
    for(int day=1;day<=n;day++){
        dp1=min(dp[2],dp[3])+clas[1][day];
        dp2=min(dp[1],dp[3])+clas[2][day];
        dp3=min(dp[2],dp[1])+clas[3][day];
        dp[1]=dp1;
        dp[2]=dp2;
        dp[3]=dp3;
    }
    cout<<min(dp1,min(dp2,dp3));
    return 0;
}/*
5
1 3 1 2 2
1 2 3 4 3
4 2 1 5 3
*/
思路:DP
:::
:::error[WA code]

无

:::
:::warning[代码问题]

无

:::
::::
::::info[T3]
:::info[problem]

# 迷宫(maze)

## 题目描述
给你一个 n 行 n 列的包含 0,1 的矩阵,0 表示空地,1 表示障碍。起点为左上角 (1,1) 位置,终点为右下角 (n,n) 位置,每次只能向右走、向下走、向左走、向上走;假如当前在位置 $(i,j)$,你只能走到 $(i,j+1)$、$(i+1,j)$、$(i,j-1)$、$(i-1,j)$ 位置。

每个位置每个单位时间都会变换状态,例如 $(i,j)$ 在第 $k$ 个单位时间是空地,则在第 $k+1$ 个单位时间是障碍。$(i,j)$ 在第 $k$ 个单位时间是障碍,则在第 $k+1$ 个单位时间是空地。

保证起点的初始状态是空地。保证起点出发可以到达终点。

问起点到终点的最短时间。

注意:
1、行走过程中,不能在某个点上逗留。
2、不论自身所在位置是空地还是障碍,只要下一步到达的位置是空地,那么这个位置就能走。

## 输入格式
从文件 `maze.in` 中读入数据。

第一行一个正整数 n。

接下来 n 行,每行一个长度为 n 的整数序列,描述这个矩阵。

## 输出格式
输出到文件 `maze.out` 中。

一行一个数,表示答案。

## 样例 1 输入

3 000 101 010

## 样例 1 输出

4

## 样例 2 输入

3 000 100 010

## 样例 2 输出

4

## 数据范围
- 对于测试点 1~4:$1 \le n \le 10$;
- 对于测试点 5~8:$1 \le n \le 100$;
- 对于测试点 9~10:$1 \le n \le 1000$。

:::
:::success[AC code]

include<bits/stdc++.h>

using namespace std;

define int long long

struct Node{ int x,y,t=0; }; queue<Node> q; int mp[1001][1001],n; int dx[]={1,-1,0,0},dy[]={0,0,1,-1}; bool vis[1001][1001]; signed main(){ freopen("maze.in","r",stdin); freopen("maze.out","w",stdout); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>mp[i][j]; if(n==1) cout<<0; Node start; start.t=0; start.x=1; start.y=1; q.push(start); vis[1][1]=1; while(!q.empty()){ Node sc=q.front(); q.pop(); int x1,y1,t1; Node n1; for(int i=0;i<=3;i++){ x1=sc.x+dx[i]; y1=sc.y+dy[i]; t1=sc.t+1; if(x1==n and y1==n and mp[x1][y1]==t1%2){ cout<<t1; return 0; } if(x1>=1 and x1<=n and y1>=1 and y1<=n and mp[x1][y1]==t1%2 and vis[x1][y1]==0){ vis[x1][y1]=1; n1.t=t1; n1.x=x1; n1.y=y1; q.push(n1); } } } / 3 0 0 0 1 0 0 0 1 0 / return 0; } 思路:BFS ::: :::error[WA code]

#include<bits/stdc++.h>
using namespace std;
#define int long long
struct Node{
    int x,y,t=0;
};
queue<Node> q;
int mp[1001][1001],n;
int dx[]={1,-1,0,0},dy[]={0,0,1,-1};
bool vis[1001][1001];
signed main(){
    freopen("maze.in","r",stdin);
    freopen("maze.out","w",stdout);
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>mp[i][j];
    Node start;
    start.t=0;
    start.x=1;
    start.y=1;
    q.push(start);
    vis[1][1]=1;
    while(!q.empty()){
        Node sc=q.front();
        q.pop();
        int x1,y1,t1;
        Node n1;
        for(int i=0;i<=3;i++){
            x1=sc.x+dx[i];
            y1=sc.y+dy[i];
            t1=sc.t+1;
            if(x1==n and y1==n and mp[x1][y1]==t1%2){
                cout<<t1;
                return 0;
            }
            if(x1>=1 and x1<=n and y1>=1 and y1<=n and mp[x1][y1]==t1%2 and vis[x1][y1]==0){
                vis[x1][y1]=1;
                n1.t=t1;
                n1.x=x1;
                n1.y=y1;
                q.push(n1);
            }
        }
    }
/*
    3
    0 0 0 
    1 0 0
    0 1 0
    */
    return 0;
}

::: :::warning[代码问题] 忘记特判边长为一输出0 ::: :::: ::::info[T4] :::info[problem]

异或平方和(origen)

题目描述

给定 n 个整数 a_1,a_2,\dots,a_n,求

\sum_{i=1}^{n}\sum_{j=i}^{n}\left(\bigoplus_{k=i}^{j}a_k\right)^2 \bmod 998244353

其中 \bigoplus 表示按位异或运算。

注:\bigoplus_{k=i}^{j}a_k = a_i \oplus a_{i+1}\oplus \dots \oplus a_j。

输入格式

从文件 origen.in 中读入数据。

第一行,一个整数 n。

第二行,n 个整数,第 i 个整数表示 a_i。

输出格式

输出到文件 origen.out 中。

一行一个整数,表示上式对 998244353 取模的结果。

样例 1 输入

3
1 2 3

样例 1 输出

24

样例 2 输入

6
1 1 4 5 1 4

样例 2 输出

202

数据范围