跳到正文

个人记录

有趣的 C++ 团队 挑战赛&四月赛G03 题解

wangyuel约 9 分钟
原文
文章信息与版本

文章信息

发布
2026年3月21日
收录
2026年4月23日
更新
2026年11月16日
篇幅
约 9 分钟

版本

  1. v2当前
  2. v1

A.\bold{A}. 深渊,希望

题意非常明确,几乎类似于模拟。

每走一步前,都进行 BFS,找出最短路,再按照这个方向走就行。

最后,不要忘记输出 We arrived!。

题目中给出的算法有一个名字,叫做泛洪算法。

C++
#include<bits/stdc++.h>
using namespace std;
const int N=20;
int n, nowx, nowy, tx, ty;
char mp[N][N];
bool vis[N][N];
int X[4] = {-1, 0, 1, 0};
int Y[4] = {0, 1, 0, -1};
string dir = "URDL";
map<char, int>id = {//方位名转方向编号
	{'U', 0}, {'R', 1}, {'D', 2}, {'L', 3}
};
struct stt{
	int x, y, dis;
	char firstStep;
};
queue<stt>q;
char get(){
	memset(vis, 0, sizeof vis);
	while(!q.empty()) q.pop();
	for(int i=0; i<4; i++)
		q.push({nowx + X[i], nowy + Y[i], 1, dir[i]});
	while(!q.empty())
	{
		stt now = q.front();
		q.pop();
		if(now.x == tx && now.y == ty) return now.firstStep;
		if(mp[now.x][now.y] != '.') continue;
		if(vis[now.x][now.y]) continue;
		vis[now.x][now.y] = 1;
		for(int i=0; i<4; i++)
			q.push({now.x + X[i], now.y + Y[i], now.dis + 1, now.firstStep});
	}
}
int main()
{
	cin >>nowx >>nowy >>tx >>ty >>n;
	for(int i=1; i<=n; i++)
		for(int j=1; j<=n; j++)
			mp[i][j] = '.';
	while(nowx != tx || nowy != ty){
		char res = get();
		cout <<res <<endl <<flush;
		char x;
		cin >>x;
		int nx = nowx + X[id[res]], ny = nowy + Y[id[res]];
		if(x == '#') mp[nx][ny] = '#';
		else nowx = nx, nowy = ny;
	}
	cout <<"We arrived!" <<endl <<flush;
	return 0;
}

B.\bold{B}. 路与风之哲

无论如何,总会有 nn 步向下,mm 步向右。

于是直接枚举出所有步数的排列,再计算消耗的体力即可。

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=12;
int n, m, k, c;
int h[N][N], d[N][N];
int pay(string way){
	int x = 1, y = 1, res = 0;
	for(char step : way){
		int nx = x, ny = y, add;
		if(step == 'R') ny ++;
		else nx ++;
		int ha = h[x][y], hb = h[nx][ny];
		if(ha > hb) add = max(k - 2*(ha - hb), c);
		else if(ha == hb) add = k;
		else add = k + 3*(hb - ha);
		res += add * d[x][y];
		x = nx, y = ny;
	}
	return res;
}
signed main()
{
	cin >>n >>m >>k >>c;
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++)
			cin >>h[i][j];
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++)
			cin >>d[i][j];
	string way = string(m-1, 'R') + string(n-1, 'D');
	int ans1 = pay(way);
	string ans2 = way;
	while(next_permutation(way.begin(), way.end(), greater<char>())){
		int tmp = pay(way);
		if(tmp < ans1) ans1 = tmp, ans2 = way;
	}
	cout <<ans1 <<endl <<ans2 <<endl;
	return 0;
}

C.\bold{C.} 导角

就是一道裸的,需要维护集合大小的并查集。C 指令视为合并,Q 指令视为查询。

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
int p[N], sz[N];
int find(int s){
	if(p[s] != s){
		p[s] = find(p[s]);
		sz[s] = sz[p[s]];
	}
	return p[s];
}
void merge(int x, int y){
	int px = find(x), py = find(y);
	if(px != py)
		p[px] = py, sz[py] += sz[px];
}
int main()
{
	for(int i=1; i<N; i++)
		p[i] = i, sz[i] = 1;
	int q;
	cin >>q;
	while(q--){
		char op;
		int i, j;
		cin >>op >>i >>j;
		if(op == 'C') merge(i, j);
		else if(op == 'Q'){
			int px = find(i);
			int py = find(j);
			if(px == py) puts("YES");
			else puts("NO");
			cout <<sz[px]-1 <<endl;
		}
	}
	return 0;
}

D.\bold{D.} 海盗分金

注:下文中的除号都指地板除法。

假设最后一个海盗的编号为 11,第一个海盗的编号为 nn。设对于 ii 号海盗,在剩下 jj 个人时能分得的金币数为 fi,jf_{i,j}。如果该名海盗必死,设 fi,jf_{i,j} 为 −1-1。

对于一共有 kk 个人的情况,如果分金币的人想要拉拢 xx 号海盗,他就需要给 xx 号海盗 fx,k−1+1f_{x,k-1}+1 个金币,来确保这一轮这名海盗的收益多于下轮,此时这名海盗才会同意此方案。而对于不需要拉拢的海盗,则无需给他任何金币。

显然,当有 nn 个海盗时,分金币的海盗为了自保,需要除自己外再拉拢 n÷2n\div 2 个人。显然最好的方法是以最小的金币拉拢其中的 n÷2n\div 2 人。根据上一段的推论,拉拢的人一定是下一轮分配中将获得金币最少的 n÷2n\div 2 人。

在拉拢的过程中,计算拉拢这些人所需要的金币总数。如果拉拢所需的金币总数大于 mm,就意味着无法拉拢其它至少 n÷2n\div 2 人,那么 MUST DIE。

C++
#include<bits/stdc++.h>
using namespace std;
const int N=210;
int way[N];//分法
int newway[N];
bool vis[N];//是否已经要到了第 i 个海盗的选票
int main()
{
	int n, m;
	cin >>n >>m;
	memset(way, -1, sizeof way);//-1代表必死
	way[1] = m;//倒序存储,最后分金的是 1 号
	for(int i=2; i<=n; i++){
		//back_up存上一个人的方案
		memset(vis, 0, sizeof vis);
		memset(newway, 0, sizeof newway);
		int needCoin=0, needVote=i/2;
		//needCoin:至少需要将多少金币分出去才能不死
		//needVote:需要的选票(除了自己的票)
		for(int j=1; j<=needVote; j++){
			//贪心策略,获取代价最少的人的选票
			int smallestId = 0, smallest = 0x3f3f3f3f;
			for(int k=i-1; k>=1; k--){
				if(!vis[k] && way[k] <= smallest)
					smallestId = k, smallest = way[k];
			}
			vis[smallestId] = 1;
			//cout <<smallestId <<endl;
			newway[smallestId] = way[smallestId] + 1;
			needCoin += newway[smallestId];
		}
		if(needCoin > m)
			way[i] = -1;//必死
		else memcpy(way, newway, sizeof way), way[i] = m - needCoin;
	}
	if(way[n] == -1) puts("MUST DIE");
	else for(int i=n; i>=1; i--)
		cout <<way[i] <<' ';
	puts("");
}
原文