跳到正文

个人记录

2026.8.29课程题目解析+总结

xiangrui0305约 8 分钟
原文

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

文章信息与版本

文章信息

发布
2026年8月29日
收录
2026年8月29日
更新
2026年11月13日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

题目解析+总结

D26313 C-候船短队

题目解析

因为只有两个位置,而且是一个办理亭和一个等候座椅,而且一旦游客到来时位置满了,他就不会到来。所以我们就用两组变量来记录每个位置的进入时间和完成时间。要是前面的完成了,那么后面的会直接进来。接着再用cnt来统计一下进来的人数,就可以了。

AC代码

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define pait pair<int,int>
const int N = 1e5+7;
const int Mod = 998244353;
int n,t[N],s[N],a[N]; 
int jinlai1 = -1,jinlai2 = -1;
int chulai1 = -1,chulai2 = -1;
bool chuli(int t,int s){
	if(jinlai1 == -1){
		jinlai1 = t,chulai1 = t+s;
		return 1; 
	}
	if(jinlai2 == -1){
		jinlai2 = t,chulai2 = chulai1+s;
		return 1; 
	}
	if(chulai2 <= t){
		jinlai1 = s,chulai1 = t+s;
		jinlai2 = chulai2 = -1;
		return 1;
	}
	if(chulai1 <= t){
		jinlai1 = jinlai2;
		chulai1 = chulai2;
		jinlai2 = t,chulai2 = chulai1+s;
		return 1;
	}
	return 0;
}
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n; 
	int cnt = 0;
	for(int i=1;i<=n;++i){
		int t,s; cin>>t>>s;
		cnt += chuli(t,s);
	}
	cout<<cnt<<endl;
	return 0;
}

D26312 B-缓冲卸货

题目解析

AC代码

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define pait pair<int,int>
const int N = 2e5+7;
const int Mod = 998244353;
int n,k,a[N],d[N];
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n>>k;
	for(int i=1;i<=n;++i) cin>>a[i];
	int ans = 0,tmp = 0;
	for(int i=2;i<=n;++i){
		d[i] = a[i] - a[i-1];
		if(d[i] > k) tmp++;
	}
	for(int i=1;i<=n;++i){
		int num = tmp;
		if(i == 1 && d[2] > k) num--;
		if(i == n && d[n] > k) num--; 
		if(1 < i && i < n){
			if(d[i] > k) num--;
			if(d[i+1] > k) num--;
			if(a[i+1]-a[i-1] > k) num++;
		}
		if(num == 0) ans++;
	}
	cout<<ans<<endl;
	return 0;
}

D26314 D-直角转运

题目解析

AC代码

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define pait pair<int,int>
const int N = 2005;
const int Mod = 998244353;
int n,m; 
int L[N][N],R[N][N];
int U[N][N],D[N][N];
char c[N][N];
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j){
			cin>>c[i][j];
		}
	}
	for(int i=1;i<=n;++i){
		int lastS = 0,lastX = 0;
		for(int j=1;j<=m;++j){
			if(c[i][j] == 'S') lastS = j;
			else if(c[i][j] == 'X') lastX = j;
			else L[i][j] = (lastS > lastX) ? 1 : 0;
 		}
 		lastS = 0,lastX = 0;
		for(int j=m;j>=1;--j){
			if(c[i][j] == 'S') lastS = j;
			else if(c[i][j] == 'X') lastX = j;
			else R[i][j] = (lastS && (lastX == 0 || lastS < lastX)) ? 1 : 0;
 		}
	}
	for(int j=1;j<=m;++j){
		int lastS = 0,lastX = 0;
		for(int i=1;i<=n;++i){
			if(c[i][j] == 'S') lastS = i;
			else if(c[i][j] == 'X') lastX = i;
			else U[i][j] = (lastS > lastX) ? 1 : 0;
 		}
 		lastS = 0,lastX = 0;
		for(int i=n;i>=1;--i){
			if(c[i][j] == 'S') lastS = i;
			else if(c[i][j] == 'X') lastX = i;
			else D[i][j] = (lastS && (lastX == 0 || lastS < lastX)) ? 1 : 0;
 		}
	}
	int ans = 0;
	for(int i=1;i<=n;++i){
		for(int j=1;j<=m;++j){
			if(c[i][j] == '.'){
				int h = L[i][j] + R[i][j];
				int v = U[i][j] + D[i][j];
				ans += 1ll * h * v;
			}
		}
	}
	cout<<ans<<endl;
	return 0;
}

D26316 F-灯距回声

题目解析

AC代码

C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
#define pait pair<int,int>
const int N = 1e6+7;
const int mod = 998244353;
int n,a[N],dp[N];
map<int,int>cnt; 
signed main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n; int pre = 0;
	for(int i=1;i<=n;++i) cin>>a[i];
	for(int i=1;i<=n;++i){
		if(i - 3 >= 1){
			pre = (pre + dp[i-3]) % mod;
			cnt[a[i-3]] = (cnt[a[i-3]] + dp[i-3]) % mod;
		}
		dp[i] = 1;
		if(i >= 2 && a[i-1] != a[i]) dp[i] = (dp[i] + dp[i-1]) % mod;
		if(i >= 3 && a[i-2] == a[i]) dp[i] = (dp[i] + dp[i-2]) % mod;
		dp[i] = (dp[i] + (pre - cnt[a[i]] + mod) % mod) % mod;
	}
	int ans = 0;
	for(int i=1;i<=n;++i) ans = (ans + dp[i]) % mod;
	cout<<ans<<endl; 
	return 0;
}