2025-11-21
CF
Problem - 1234C - Codeforces(贪心)
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=2e5+10;
string s[2];
void solve()
{
int n;
cin >> n;
cin >> s[0] >> s[1];
int x = 0;
for (int i = 0; i < n;i++){
if(s[x][i]>'2'){
x ^= 1;
if(s[x][i]<='2'){
cout << "NO\n";
return;
}
}
}
if(x==0){
cout << "NO\n";
}else{
cout << "YES\n";
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--)
{
solve();
}
}
Problem - 1689C - Codeforces(树形dp)(1500)
一道很板的树形dp还看了好久
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=3e5+10;
vector<int> e[N];
int cnt[N],dp[N];
//dp[i]:表示 i 被感染,最多能保留多少结点
void dfs(int u,int fa){
dp[u] = 0, cnt[u] = 1;
int sum = 0;
for (int i = 0; i < e[u].size();i++){
int v = e[u][i];
if(v==fa)
continue;
dfs(v, u);
sum += dp[v];//sum存u的两个子结点都被感染,最多保存的结点数
cnt[u] += cnt[v];
}
for (int i = 0; i < e[u].size();i++){
int v = e[u][i];
if(v==fa)
continue;
dp[u] = max(dp[u], sum - dp[v] + cnt[v] - 1);
//dp[v1]+cnt[v2]-1,相当于保留 v2结点,加上v1被感染后最多能保留的结点数
}
}
void solve()
{
int n;
cin >> n;
for (int i = 0; i <= n;i++){
e[i].clear();
}
for (int i = 1; i < n; i++)
{
int u, v;
cin >> u >> v;
e[u].push_back(v);
e[v].push_back(u);
}
dfs(1, 0);
cout << dp[1] << endl;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int T;
cin >> T;
while (T--)
{
solve();
}
}
Problem - 44H - Codeforces(dp)(1700虚高)
要注意看数组有没有越界
#include <bits/stdc++.h>
using namespace std;
#define LL long long
const LL mod = 998244353;
const int N=2e5+10;
LL dp[55][10];//记得开LL!!!
int a[55];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
for (int i = 0; i < s.size();i++)
a[i] = s[i] - '0';
for (int i = 0; i <= 9;i++){
dp[0][i] = 1;
}
for (int i = 0; i < s.size()-1;i++){
for (int j = 0; j <= 9;j++){
if((j+a[i+1])%2==0){
dp[i + 1][(j + a[i + 1]) / 2] += dp[i][j];
}
else{
dp[i + 1][(j + a[i + 1]) / 2] += dp[i][j];
dp[i + 1][(j + a[i + 1]) / 2 + 1] += dp[i][j];
}
}
}
LL ans = 0;
for (int i = 0; i<= 9;i++){
ans += dp[s.size() - 1][i];
}
int flag = 1;
for (int i = 0; i < s.size()-1;i++){
if(abs(a[i]-a[i+1])>1)
flag = 0;
}
ans -= flag;
cout << ans << endl;
}
dfs解法!
要分清局部变量和全局变量
如果是递归的话,要用局部变量!!!
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+10;
int a[N];
int cnt,res;
int f[N][15];
int n;
int dfs(int x,int lst){
if(x==n)
return 1;
if(~f[x][lst])
return f[x][lst];
int cnt = a[x + 1] + lst;//注意这里
f[x][lst] = dfs(x + 1, cnt / 2);
if(cnt&1)
f[x][lst] += dfs(x + 1, cnt / 2 + 1);
return f[x][lst];
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
cin >> s;
n = s.size();
s = " " + s;
for (int i = 1; i <= n;i++){
a[i] = s[i] - '0';
}
memset(f, -1, sizeof f);
for (int i = 0; i <= 9;i++){
res += dfs(1, i);
}
int flag = 1;
for (int i = 1; i < n;i++){
if(abs(a[i]-a[i+1])>1){
flag = 0;
break;
}
}
res -= flag;
cout << res << endl;
}

浙公网安备 33010602011771号