跳到正文

学习·文化课

12-28晚 - 407考试总结

Void_Trailwalker约 20 分钟
原文

这是 2026年4月20日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2026年4月20日
更新
2026年4月20日
篇幅
约 20 分钟

版本

  1. v2当前
  2. v1

T1

得分十分QAQ
10pts代码:

C++
#include<iostream>
using namespace std;
int n , l , r , ans;
struct node{
	int time , num;
}a[100005];
bool cmp(node A , node B){
	return A.time < B.time;
}
int main(){
	cin >> n;l = 1 , r = n;
	for(int i=1;i<=n;i++)cin >> a[i].num >> a[i].time;
	while(l<=r){
		a[l].num-- ,  a[r].num-- , ans = max(ans,a[l].time+a[r].time);
		if(a[l].num==0)l++;
		if(a[r].num==0)r--;
	} 
	cout << ans;
	return 0;
}

100pts代码:

C++
#include<iostream>
#include<algorithm>
using namespace std;
struct node{
	int a , b;
}a[100005];
int n , l , r , ans;
bool cmp(node A , node B){
	return A.b < B.b;
}
int main(){
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> a[i].a >> a[i].b;
	}
	sort(a+1,a+n+1,cmp);l = 1 , r = n;
	while(l<r){
		ans = max(a[l].b+a[r].b,ans);
		a[l].a-- , a[r].a--;
		if(a[l].a==0)l++;
		if(a[r].a==0)r--;
	}
	cout << ans;
	return 0;
}

总结:

死因:忘记 sort 了。
当时因为思路都集中在怎么配对奶牛,而忽略了顺序,再加上样例本来就是排好序的,所以就没写。

T2

100分!
100pts代码:

C++
#include<iostream>
#include<queue>
using namespace std;
bool a[1005][1005];int stx = 501 , sty = 501 , enx , eny;
int n , dx[] = {0,1,0,-1} , dy[] = {1,0,-1,0};
struct node{
	int x , y , step;
};
void bfs(){
	queue<node> q;
	q.push({stx,sty,0});
	while(!q.empty()){
		node tmp = q.front();q.pop();
		if(tmp.x==enx&&tmp.y==eny){
			cout << tmp.step;return;
		}
		for(int i=0;i<4;i++){
			int xx = tmp.x + dx[i] , yy = tmp.y + dy[i];
			if(xx<1||xx>1001||yy<1||yy>1001||a[xx][yy])continue;
			q.push({xx,yy,tmp.step+1});a[xx][yy] = true;
		}
	}
}
int main(){
	cin >> enx >> eny >> n;enx += 501 , eny += 501;
	for(int i=1;i<=n;i++){
		int x , y;cin >> x >> y;
		a[x+501][y+501] = 1;
	} 
	bfs();
	return 0;
}

总结:

这道题不难,要注意的就是把所有坐标变成正整数。
这道题很有特点,所以在第一次做的时候就记住了。

T3

还是100。 100pts代码:

C++
#include<iostream>
#include<queue>
using namespace std;
int n , a[205] , A , B;bool fl[205];
struct node{
	int num , step;
};
void bfs(){
	queue<node> q;
	q.push({A,0});
	while(!q.empty()){
		node tmp = q.front();q.pop();
		if(tmp.num==B){
			cout << tmp.step;return;
		}
		int ttmp = tmp.num + a[tmp.num];
		if(ttmp<=n&&!fl[ttmp])fl[ttmp] = true , q.push({ttmp,tmp.step+1});
		ttmp = tmp.num - a[tmp.num];
		if(ttmp>0&&!fl[ttmp])fl[ttmp] = true , q.push({ttmp,tmp.step+1});
	}
	cout << -1;
}
int main(){
	cin >> n >> A >> B;
	for(int i=1;i<=n;i++)cin >> a[i];
	bfs();
	return 0;
}

总结:

1维bfs而已,没什么可以总结的。
个人觉得这是最简单的bfs之一,如果错了真的不应该。这道题的数据也很小,思路也简单。

T4

100分。
100pts代码:

C++
#include<iostream>
#include<algorithm>
using namespace std;
int n , v[200005] , u[200005] , l = 1 , r = 1 , ans;
int main(){
	cin >> n;
	for(int i=1;i<=n;i++)cin >> v[i];
	for(int i=1;i<=n;i++)cin >> u[i];
	sort(v+1,v+n+1);sort(u+1,u+n+1);
	while(l<=n&&r<=n){
		while(l<=n&&v[l]<u[r])l++;
		if(l<=n)ans++ , r++ , l++;
	}
	cout << ans;
	return 0;
}

总结:

思路很简单,就是找到能够超过田忌当前马的最慢的马,而且不能重复。一个双指针就可以解决。

T5

60分。 60pts代码:

C++
#include<iostream>
#include<queue>
using namespace std;
int n , m , k , stx , sty , stz , enz , enx , eny;bool a[35][35][35];
int dx[] = {0,0,0,0,1,-1};
int dy[] = {0,0,1,-1,0,0};
int dz[] = {1,-1,0,0,0,0};
struct node{
	int x , y , z , step;
};
void bfs(){
	queue<node> q;
	q.push({stx,sty,stx,0});
	while(!q.empty()){
		node tmp = q.front();q.pop();
		if(tmp.x==enx&&tmp.y==eny&&tmp.z==enz){
			cout << "Escaped in " << tmp.step << " minute(s).";return;
		}
		for(int i=0;i<6;i++){
			int xx = tmp.x + dx[i] , yy = tmp.y + dy[i] , zz = tmp.z + dz[i];
			if(xx<1||xx>n||yy<1||yy>m||zz<1||zz>k||a[xx][yy][zz])continue;
			a[xx][yy][zz] = true;q.push({xx,yy,zz,tmp.step+1});
		}
	}
	cout << "Trapped!";
}
int main(){
	cin >> n >> m >> k;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			for(int kk=1;kk<=k;kk++){
				char c;cin >> c;
				if(c=='S')stx = i , sty = j , stz = kk;
				if(c=='E')enx = i , eny = j , enz = kk;
				if(c=='#')a[i][j][kk] = 1;
			}
		}
	}
	bfs();
	return 0;
}

100pts代码:

C++
#include<bits/stdc++.h>
using namespace std;
int n , m , l;bool a[35][35][35];
int stx , sty , stz , enx , eny , enz;
struct node{
	int x , y , z , step;
};
int dx[] = {1,0,-1,0,0,0} , dy[] = {0,1,0,-1,0,0} , dz[] = {0,0,0,0,1,-1};
void bfs(){
	queue<node> q;
	q.push({stx,sty,stz,0});a[stz][stx][sty] = 1;
	while(!q.empty()){
		node tmp = q.front();
		q.pop();
		if(tmp.x==enx&&tmp.y==eny&&tmp.z==enz){
			cout << "Escaped in " << tmp.step << " minute(s).\n";return;
		}
		for(int i=0;i<6;i++){
			int xx = tmp.x + dx[i] , yy = tmp.y + dy[i] , zz = tmp.z + dz[i];
			if(xx<1||yy<1||zz<1||xx>n||yy>m||zz>l||a[zz][xx][yy])continue;
			a[zz][xx][yy] = true;q.push({xx,yy,zz,tmp.step+1});
		}
	}
	cout << "Trapped!\n";
}
int main(){
	cin >> l >> n >> m;
	for(int i=1;i<=l;i++){
		for(int j=1;j<=n;j++){
			for(int k=1;k<=m;k++){
				char c;cin >> c;
				if(c=='#')a[i][j][k] = true;
				if(c=='S')stx = j , sty = k , stz = i;
				if(c=='E')enx = j , eny = k , enz = i;
			}
		}
	}
	bfs();
	return 0;
}

总结:

3维bfs,有一点难度,但是最坑的的是 n,m,ln,m,l 的顺序,需要注意。
我这个顺序总是搞反,所以就拿不了满分,下次一定会的。

T6

84分。 84pts代码:

C++
#include<iostream>
#include<algorithm>
using namespace std;
int map[200005][3] , n , l , r , ans = 2147483647;int numf[200005];
struct node{
	int type , num;
}a[200005];
bool cmp1(node A , node B){
	return A.num < B.num;
}
bool cmp2(node A , node B){
	return A.type < B.type;
}
int main(){
	cin >> n;
	for(int i=1;i<=n;i++)cin >> a[i].num >> a[i].type;
	sort(a+1,a+n+1,cmp2);
	int len = 1;a[1].type = 1;
	for(int i=2;i<=n;i++){
		if(a[i].type==a[i-1].type){
			a[i].type = len;
		}else a[i].type = ++len;
	}
	sort(a+1,a+n+1,cmp1);
	for(int i=1;i<=n;i++){
		map[i][1] = a[i].type , map[i][2] = a[i].num;
	}
	int l = 1 , r = 0 , tmp = 0;
	while(r<=n&&l<=n){
		while(r<=n&&tmp<len){
			r++;
			if(numf[map[r][1]]==0)tmp++;
			numf[map[r][1]]++;
		}
		if(r>n)break;
		while(r<=n&&l<r&&tmp>=len){
			if(tmp==len)ans = min(ans,map[r][2]-map[l][2]);
			numf[map[l][1]]--;
			if(numf[map[l][1]]==0)tmp--;
			l++;
		}
	}
	cout << ans;
	return 0;
}

100pts代码:

C++
#include<bits/stdc++.h>
using namespace std;
int n , ans = 2e9 , sum;
struct node{
	int x , y;
}a[500005];
map<int,int> b , b2;
bool cmp(node A , node B){
	return A.x < B.x;
}
int main(){
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> a[i].x >> a[i].y;
		if(!b2[a[i].y])b2[a[i].y] = 1 , sum++;
	}
	sort(a+1,a+n+1,cmp);
	int ti = 1 , z = 1;
	b[a[1].y] = 1;
	for(int i=1;i<=n;i++){
		while(z<sum&&ti<n){
			ti++ , b[a[ti].y]++;
			if(b[a[ti].y]==1)z++;
		}
		if(z==sum){
			ans = min(ans,a[ti].x-a[i].x);
		}
		b[a[i].y]--;
		if(b[a[i].y]==0)z--;
	}
	cout << ans;
	return 0;
}

总结:

难点就是要开个map来存储数据,这个地方没处理好。
比赛时因为觉得map太慢了,所以搞了个离散化,结果没写好,写挂了。

T7

18分。 18pts代码:

C++
#include<iostream>
#include<queue>
using namespace std;
int a[105][105];int n , m , ans , stx , sty;
struct node{
	int x , y , step;
};
int dx[] = {0,1,0,-1,1,-1,1,-1} , dy[] = {1,0,-1,0,1,-1,-1,1};
void bfs(){
	queue<node> q;
	q.push({stx,sty,1});
	while(!q.empty()){
		node tmp = q.front();
		q.pop();
		for(int i=0;i<8;i++){
			int xx = tmp.x + dx[i] , yy = tmp.y + dy[i];
			if(xx<1||xx>n||yy<1||yy>m||a[xx][yy])continue;
			a[xx][yy] = tmp.step + 1;q.push({xx,yy,tmp.step+1});
		}
	} 
} 
int main(){
	cin >> n >> m >> stx >> sty;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			char c;cin >> c;
			if(c=='*')a[i][j] = -1;
		}
	}
	bfs();
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			ans = max(ans,a[i][j]);
		} 
	}
	cout << ans;
	return 0;
}

100pts代码:

C++
#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef long long ll;
const int N=2e6+10;
struct node{
	int x,y,step;
};
int n,m,sx,sy,cnt=0;
int vis[500][500];
char mp[500][500];
int dx[]={0,0,-1,1,1,1,-1,-1};
int dy[]={-1,1,0,0,-1,1,-1,1};
bool flag;
queue<node> q;
void bfs(int sx,int sy){
	while(q.size()) q.pop();
	q.push({sx,sy,0});
	vis[sx][sy]=1;
	while(q.size()){
		node f=q.front();
		q.pop();
		for(int i=0;i<8;i++){
			int nx=dx[i]+f.x;
			int ny=dy[i]+f.y;
			if(nx>=1&&nx<=n&&ny>=1&&ny<=m&&vis[nx][ny]==0&&mp[nx][ny]!='*'){
				vis[nx][ny]=1; cnt=f.step+1;
				q.push({nx,ny,f.step+1});
			}
		}
	}
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	cin>>m>>n>>sy>>sx;
	sx=n-sx+1;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>mp[i][j];
		}
	}
	bfs(sx,sy);
	cout<<cnt<<endl;
	return 0;
}

总结:

这道题最大的难点就是要把初始坐标改变一下。
因为题目中说“也就是左下角的格”,所以要在读入时改变一下坐标。比赛时太着急了,没有仔细读题。

T8

20分 20pts代码:

C++
#include<iostream>
#include<queue>
using namespace std;
int a[1005][1005];
int n , dx[] = {-1,0,1,0} , dy[] = {0,-1,0,1};
struct node{
	int x , y , step;
};
void bfs(){
	queue<node> q;
	q.push({1,1,0});a[1][1] = 0;
	while(!q.empty()){
		node tmp = q.front();q.pop();
		if(tmp.x==n&&tmp.y==n){
			cout << "Yes\n";return;
		}
		for(int i=0;i<4;i++){
			int xx = tmp.x + dx[i] , yy = tmp.y + dy[i];
			if(xx<1||xx>n||yy<1||yy>n||tmp.step+1>a[xx][yy])continue;
			a[xx][yy] = tmp.step + 1 , q.push({xx,yy,tmp.step+1});
		}
	}
	cout << "No\n";
}
int main(){
	int T;
	cin >> T;
	while(T--){
		cin >> n;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++)a[i][j] = 2147483647;
		}
		for(int i=1;i<=2*n-2;i++){
			int b , c;cin >> b >> c;a[b][c] = i;
		}
		bfs();
	}
	return 0;
}

100pts代码:

C++
#include<iostream>
#include<queue>
using namespace std;
int a[1005][1005];
int n , dx[] = {-1,0,1,0} , dy[] = {0,-1,0,1};
struct node{
	int x , y , step;
};
void bfs(){
	queue<node> q;
	q.push({1,1,0});a[1][1] = 0;
	while(!q.empty()){
		node tmp = q.front();q.pop();
		if(tmp.x==n&&tmp.y==n){
			cout << "Yes\n";return;
		}
		for(int i=0;i<4;i++){
			int xx = tmp.x + dx[i] , yy = tmp.y + dy[i];
			if(xx<1||xx>n||yy<1||yy>n||a[xx][yy]<=(tmp.step+1))continue;
			a[xx][yy] = 1 , q.push({xx,yy,tmp.step+1});
		}
	}
	cout << "No\n";
}
int main(){
	int T;
	cin >> T;
	while(T--){
		cin >> n;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++)a[i][j] = 2147483647;
		}
		for(int i=1;i<=2*n-2;i++){
			int b , c;cin >> b >> c;a[b][c] = min(a[b][c],i+1);
		}
		bfs();
	}
	return 0;
}

总结:

没想到一个格子竟然可以放多个路障,这整整坑掉了我80分啊!
比赛时只剩下一点时间了,也就没仔细看题,没注意到一个格子可以放多个障碍。其他的还是很简单。