2026.8.29课程题目解析+总结
xiangrui0305 · · 个人记录
题目解析+总结
D26313 C-候船短队
题目解析
因为只有两个位置,而且是一个办理亭和一个等候座椅,而且一旦游客到来时位置满了,他就不会到来。所以我们就用两组变量来记录每个位置的进入时间和完成时间。要是前面的完成了,那么后面的会直接进来。接着再用cnt来统计一下进来的人数,就可以了。
AC代码
#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代码
#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代码
#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-灯距回声
题目解析
我们先设
然后对于每一个结尾的
- 若
i - j = 2 ,则必须a_i = a_j - 否则,必须
a_i \not = a_{i-1}
这样,我们就得到以下状态转移方程:
其中
我们处理完后,就直接求和所有的
AC代码
#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;
}