跳到正文

个人记录

10-3模拟赛总结

NothingIsImpossible约 15 分钟
原文
文章信息与版本

文章信息

发布
2026年10月3日
收录
2026年10月3日
更新
2026年11月13日
篇幅
约 15 分钟

版本

  1. v1当前

算法题代码汇总

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

T1
problem

还原零一串(restore)

题目描述

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

  • 将0添加到字符串的一端,并将1添加到字符串的另一端。例如,从字符串1011开始,可以得到010111或110110。

不巧的是,小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≤T≤100,1≤n≤20001\le T\le 100,1\le n\le 2000。

AC code
C++
#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*/
思路:双指针贪心
WA code
C++
无
代码问题

没有问题

T2
problem

cyw 的学习计划(study)

题目描述

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

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

而对于第 i 天的课程,第一门课的价格为 aia_i,第二门课的价格为 bib_i,第三门课的价格为 cic_i。

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

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

输入格式

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

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

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

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

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

输出格式

输出到文件 study.out 中。

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

样例 1 输入

代码
5
1 3 1 2 2
1 2 3 4 3
4 2 1 5 3

样例 1 输出

代码
9

数据范围

  • 对于 30% 的数据,1≤n≤101 \le n \le 10;
  • 对于 60% 的数据,1≤n≤5001 \le n \le 500;
  • 对于 100% 的数据,1≤n≤100000,1≤ai,bi,ci≤1001 \le n \le 100000,1 \le a_i,b_i,c_i \le 100。
AC code
C++
#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
WA code
代码
无
代码问题
代码
无
T3
problem

迷宫(maze)

题目描述

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

每个位置每个单位时间都会变换状态,例如 (i,j)(i,j) 在第 kk 个单位时间是空地,则在第 k+1k+1 个单位时间是障碍。(i,j)(i,j) 在第 kk 个单位时间是障碍,则在第 k+1k+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≤n≤101 \le n \le 10;
  • 对于测试点 5~8:1≤n≤1001 \le n \le 100;
  • 对于测试点 9~10:1≤n≤10001 \le n \le 1000。
AC code
C++
#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
WA code
C++
#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;
}
代码问题

忘记特判边长为一输出0

T4
problem

异或平方和(origen)

题目描述

给定 nn 个整数 a1,a2,…,ana_1,a_2,\dots,a_n,求

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

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

注:⨁k=ijak=ai⊕ai+1⊕⋯⊕aj\bigoplus_{k=i}^{j}a_k = a_i \oplus a_{i+1}\oplus \dots \oplus a_j。

输入格式

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

第一行,一个整数 nn。

第二行,nn 个整数,第 ii 个整数表示 aia_i。

输出格式

输出到文件 origen.out 中。

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

样例 1 输入

代码
3
1 2 3

样例 1 输出

代码
24

样例 2 输入

代码
6
1 1 4 5 1 4

样例 2 输出

代码
202

数据范围

  • 对于 10%10\% 的数据,n≤400n \le 400;
  • 对于 30%30\% 的数据,n≤3000n \le 3000;
  • 对于另外 10%10\% 的数据,ai∈{0,1}a_i \in \{0,1\};
  • 对于 100%100\% 的数据,n≤2×105, 0≤ai≤2×105n \le 2\times 10^5,\ 0 \le a_i \le 2\times 10^5。
AC code
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MOD=998244353;
signed main(){
	freopen("origen.in","r",stdin);
	freopen("origen.out","w",stdout);
    int n;
    cin>>n;
    vector<int> pre(n+1);
    for(int i=1;i<=n;++i){
        int x;
        cin>>x;
        pre[i]=pre[i-1]^x;
    }
    vector<int> pm(37,1);
    for(int i=1;i<=36;++i){
        pm[i]=pm[i-1]*2%MOD;
    }
    int ans=0;
    for(int i=0;i<18;i++){
        for(int j=0;j<18;j++){
            int m=pm[i+j];
            int num[3][3]={0};
            num[0][0]=1;
            for(int k=1;k<=n;k++){
                int x=(pre[k]>>i)&1;
                int y=(pre[k]>>j)&1;
                ans=(ans+num[x^1][y^1]*m)%MOD;
                num[x][y]++;
            }
        }
    }
    cout<<ans;
    return 0;
}
思路:二进制乘法+前缀和
WA code
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
	freopen("origen.in","r",stdin);
	freopen("origen.out","w",stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int a[200001]={0};
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){cin>>a[i];a[i]^=a[i-1];}
	int sum=0;
	for(int i=1;i<=n;i++)
		for(int j=i;j<=n;j++)
			sum+=(a[j]^a[i-1])%998244353*(a[j]^a[i-1])%998244353;
			sum%=998244353;
	cout<<sum%998244353;
	return 0;
}
错误原因
代码
不会推导公式
原文