跳到正文

算法·理论

2025.7.21 S组模拟赛后反思

Strughart约 12 分钟
原文
文章信息与版本

文章信息

发布
2025年7月21日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 12 分钟
获赞
1

版本

  1. v1当前

第 11 题 - [CSP-S 2021] 廊桥分配

思路:

建立两个优先队列,把国内外的飞机开始和结束的时间放入队列,贪心分配,用前缀和记录答案,最终提取最大值即可。

AC CODE

#include<bits/stdc++.h>
#define int long long
#define reg register
#define endl '\n'
using namespace std;
using ll=long long;
#define maxn 100005
inline int read() {
	int x=0,f=1;
	char ch=getchar();
	while(!isdigit(ch)) {
		if(ch=='-') f=-1;
		ch=getchar();
	}
	while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	return x*f;
}
inline void print(int x) {
	if(x<0) putchar('-'),x=-x;
	if(x>9) print(x/10);
	putchar(x%10+'0');
}
const int N=5e5+5;
int a1[N],b1[N],f1[2*N],ans1[N],res1[N];
int a2[N],b2[N],f2[2*N],ans2[N],res2[N];
int n,m1,m2;
priority_queue<int,vector<int>,greater<int>> pq1,pq2,p1,p2;
map<int,pair<int,bool>> mp1,mp2;
signed main() {
	n=read(),m1=read(),m2=read(),n=min(n,m1+m2);
	for(reg int i=1; i<=m1; i++) {
		a1[i]=read(),b1[i]=read();
		mp1[a1[i]]= {i,0},mp1[b1[i]]= {i,1};
		pq1.push(a1[i]),pq1.push(b1[i]);
	}
	for(reg int i=1; i<=2*m1; i++)
		f1[i]=pq1.top(),pq1.pop();
	int now1=0;
	for(reg int i=1; i<=2*m1; i++) {
		if(mp1[f1[i]].second==0) {
			if(p1.empty()||p1.top()>now1+1)
				ans1[mp1[f1[i]].first]=now1+1;
			else
				ans1[mp1[f1[i]].first]=p1.top(),p1.pop();
			now1++;
		} else
			p1.push(ans1[mp1[f1[i]].first]),now1--;
	}
	for(reg int i=1; i<=m1; i++)
		res1[ans1[i]]++;
	for(reg int i=1; i<=m1; i++)
		res1[i]+=res1[i-1];
	for(reg int i=1; i<=m2; i++) {
		a2[i]=read(),b2[i]=read();
		mp2[a2[i]]= {i,0},mp2[b2[i]]= {i,1};
		pq2.push(a2[i]),pq2.push(b2[i]);
	}
	for(reg int i=1; i<=2*m2; i++)
		f2[i]=pq2.top(),pq2.pop();
	int now2=0;
	for(reg int i=1; i<=2*m2; i++) {
		if(mp2[f2[i]].second==0) {
			if(p2.empty()||p2.top()>now2+1)
				ans2[mp2[f2[i]].first]=now2+1;
			else
				ans2[mp2[f2[i]].first]=p2.top(),p2.pop();
			++now2;
		} else
			p2.push(ans2[mp2[f2[i]].first]),now2--;
	}
	for(reg int i=1; i<=m2; i++)
		res2[ans2[i]]++;
	for(reg int i=1; i<=m2; i++)
		res2[i]+=res2[i-1];
	int Ans=INT_MIN;
	for(reg int i=0; i<=n; i++)
		Ans=max(Ans,res1[i]+res2[n-i]);
	print(Ans);
	return 0;
}

第 22 题 - [CSP-S 2021] 括号序列

思路:

一眼 区间DP,设 dp[i][j][k]dp[i][j][k] 为左端点为 ii,右端点为 jj,类型为 kk 时最终的方案总数。我们可以设 66 种状态:

代码
 0 --- 两边都不是* 
   1 --- 左边是*,右边不是*
	  2 --- 左边不是*,右边是*
	    3 --- 左边和右边都是*
		  4 --- 全部都是*
		    5 --- 两边都不是*,且左边和右边对应
∗∗设出状态后,容易发现∗∗**设出状态后,容易发现**
代码
dp[l][r][0] = (dp[l][r][0] + (dp[l][i][2] + dp[l][i][0]) * dp[i + 1][r][5]) % mod;
dp[l][r][1] = (dp[l][r][1] + (dp[l][i][1] + dp[l][i][3]) * dp[i + 1][r][5]) % mod;
dp[l][r][2] = (dp[l][r][2] + (dp[l][i][0] * dp[i + 1][r][4])) % mod;
dp[l][r][3] = (dp[l][r][3] + (dp[l][i][1] * dp[i + 1][r][4])) % mod;
if (len <= k && ((s[r] == '*') || (s[r] == '?'))) {
	dp[l][r][4] = dp[l][r - 1][4]; 
}
if ((s[l] == '(' || s[l] == '?') && (s[r] == ')' || s[r] == '?')) {
	dp[l][r][5] = (dp[l + 1][r - 1][0] + dp[l + 1][r - 1][1] + dp[l + 1][r - 1][2] + dp[l + 1][r - 1][4]) % mod;
}
dp[l][r][0] = (dp[l][r][0] + dp[l][r][5]) % mod;
dp[l][r][3] = (dp[l][r][3] + dp[l][r][4]) % mod;
∗∗得出最终的结果为dp[1][n][0]∗∗ **得出最终的结果为 dp[1][n][0]**

AC CODE

C++
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

constexpr int N = 505, mod = 1e9 + 7;
int n, k;
char s[N];
ll dp[N][N][6];

int main(void) {
	cin >> n >> k;
    scanf("%s", s + 1);
	for (int i = 1; i <= n; i++) dp[i][i - 1][4] = 1;
	for (int len = 1; len <= n; len++) {
		for (int l = 1; l + len - 1 <= n; l++) {
			int r = l + len - 1;
			/* 0 --- 两边都不是* 
			   1 --- 左边是*,右边不是*
			   2 --- 左边不是*,右边是*
			   3 --- 左边和右边都是*
			   4 --- 全部都是*
			   5 --- 两边都不是*,且左边和右边对应
			   
			   4 属于 3, 5 属于 0 
			*/ 
			for (int i = l; i < r; i++) {
				dp[l][r][0] = (dp[l][r][0] + (dp[l][i][2] + dp[l][i][0]) * dp[i + 1][r][5]) % mod;
				dp[l][r][1] = (dp[l][r][1] + (dp[l][i][1] + dp[l][i][3]) * dp[i + 1][r][5]) % mod;
				dp[l][r][2] = (dp[l][r][2] + (dp[l][i][0] * dp[i + 1][r][4])) % mod;
				dp[l][r][3] = (dp[l][r][3] + (dp[l][i][1] * dp[i + 1][r][4])) % mod;
			}
			if (len <= k && ((s[r] == '*') || (s[r] == '?'))) {
				dp[l][r][4] = dp[l][r - 1][4]; 
			}
			if ((s[l] == '(' || s[l] == '?') && (s[r] == ')' || s[r] == '?')) {
				dp[l][r][5] = (dp[l + 1][r - 1][0] + dp[l + 1][r - 1][1] + dp[l + 1][r - 1][2] + dp[l + 1][r - 1][4]) % mod;
			}
			dp[l][r][0] = (dp[l][r][0] + dp[l][r][5]) % mod;
			dp[l][r][3] = (dp[l][r][3] + dp[l][r][4]) % mod;
		}
	}
	cout << dp[1][n][0] % mod;
	return 0;
}

第 33 题 - [CSP-S 2022] 假期计划

思路:

建图,用 bfsbfs 计算全源最短路,setset 维护每个点的最多贡献的 33 个点,暴力枚举 bb 和 cc,这时候使用我们所维护的答案候选人,验证其正确性即可。

AC CODE

C++
# include <bits/stdc++.h>
# define int long long
using namespace std;

typedef long long ll;

constexpr int N = 2505;
ll w[N], ans;
int n, m, k, x, y, dis[N][N];
vector<int> g[N];
set<pair<int, int>> s[N];

void bfs (int now) {
	dis[now][now] = 0;
	queue<int> q;
	q.push (now);
	while (!q.empty()) {
		int u = q.front ();
		q.pop ();
		for (int v : g[u]) {
			if (dis[now][v] > dis[now][u] + 1) {
				dis[now][v] = dis[now][u] + 1;
				q.push (v);
			}
		}
	}
}

signed main(void) {
	cin.tie(nullptr) -> ios::sync_with_stdio(false);
	cin >> n >> m >> k;
	for (int i = 2; i <= n; i++) {
		cin >> w[i];
	}
	for (int i = 1; i <= m; i++) {
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	memset (dis, 0x3f, sizeof (dis));
	for (int i = 1; i <= n; i++) {
		bfs(i);
	}
	for (int i = 2; i <= n; i++) {
		for (int j = 2; j <= n; j++) {
			if (i != j) {
				if (dis[i][j] <= k + 1 && dis[1][j] <= k + 1) {
					s[i].insert({w[j], j});
				}
				if (s[i].size() > 3) {
					s[i].erase(s[i].begin());
				}
			}
		}
	}
	for (int b = 2; b <= n; b++) {
		for (int c = 2; c <= n; c++) {
			if (!(b == c || dis[b][c] > k + 1)) {
				for (auto a : s[b]) {
					if (!(a.second == b || a.second == c)) {
						for (auto d : s[c]) {
							if (!(d.second == b || d.second == c || d.second == a.second)) {
								ans = max(ans, w[a.second] + w[b] + w[c] + w[d.second]);
							}
						}
					}
				}
			} 
		}
	}
	cout << ans;
	return 0;
}
原文