2025-11-28
CF
Problem - 1766C - Codeforces(1300)(dp)(模拟)
一笔画,要经过所有黑色,并且有且仅有一次
不能经过白色
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=2e5+10;
void solve()
{
int n;
cin >> n;
string s[2];
cin >> s[0] >> s[1];
for (int t = 0; t < 2;t++){
int x = t;
int flag = 1;
for (int i = 0; i < n;i++){
if(s[x][i]!='B')
flag = 0;
if(s[!x][i]=='B')
x ^= 1;
}
if(flag){
cout << "YES\n";
return;
}
}
cout << "NO\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--)
{
solve();
}
}
Problem - 295A - Codeforces(1400)(差分)
读题!!!读了老半天。。。
一个是对从l到r+d
还有查询是执行第x到y个操作
所以使用两次差分
第一次计算每个操作的操作次数
第二次计算操作加的数目
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=1e5+10;
LL a[N],b[N];
struct node{
int l,r,d;
}e[N];
LL add[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, m, k;
cin >> n >> m >> k;
for (int i = 1; i <= n;i++){
cin >> a[i];
}
for (int i = 1; i <= m;i++){
cin >> e[i].l >> e[i].r >> e[i].d;
}
for (int i = 1; i <= k;i++){
LL x, y;
cin >> x >> y;
b[x]++, b[y + 1]--;
}
for (int i = 1; i <= m;i++){
b[i] += b[i - 1];//算每个操作的操作数
add[e[i].l] += b[i] * e[i].d;
add[e[i].r + 1] -= b[i] * e[i].d;
}
for (int i = 1; i <= n;i++){
add[i] += add[i - 1];
cout << a[i] + add[i] << " ";
}
}
Problem - 1389B - Codeforces(dp好题)(1600)
计算k次移动最大总和
dp[i][j],移动到i位置,j次向左移的最大值
当移动次数刚好满足k时,更新ans
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=1e5+10;
int dp[N][6];
int a[N];
void solve()
{
memset(dp, 0, sizeof dp);
int n, k, z;
cin >> n >> k >> z;
for (int i = 1; i <= n;i++){
cin >> a[i];
}
int ans=0;
for (int j = 0; j <= z;j++){
for (int i = 1; i <= n;i++){
dp[i][j] = dp[i - 1][j] + a[i];
if(i&&j!=n){
dp[i][j] = max(dp[i][j], dp[i + 1][j - 1] + a[i]);
}
if(i-1+j*2==k){
ans = max(ans, dp[i][j]);
}
}
}
cout << ans << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--)
{
solve();
}
}

浙公网安备 33010602011771号