2025.3.4 赛后总结
__DragonFly · · 个人记录
T1 P9502 『MGOI』Simple Round I | A. 魔法数字
错误代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int mu(int n, int m)
{
int sum = 1;
for (int i = 1; i <= m; ++i) sum *= n;
return sum;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, maxx = -1;
cin >> n;
for (int i = 2; ; i += 2)
{
if (mu(2, i) < n)
{
maxx = max(maxx, i);
}
else
{
break;
}
}
cout << maxx;
return 0;
}
错误原因
如果 maxx 初始值因该为
正确代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
int mu(int n, int m)
{
int sum = 1;
for (int i = 1; i <= m; ++i) sum *= n;
return sum;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, maxx = 0;
cin >> n;
for (int i = 2; ; i += 2)
{
if (mu(2, i) < n)
{
maxx = max(maxx, i);
}
else
{
break;
}
}
cout << maxx;
return 0;
}
T3 P1091 [NOIP 2004 提高组] 合唱队形
错误代码
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int dp[N][2], a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) // 以i结尾的最长上升子序列
{
dp[i][0] = 1;
for (int j = 1; j < i; ++j)
{
if (a[i] > a[j])
{
dp[i][0] = max(dp[i][0], dp[j][0] + 1);
}
}
}
for (int i = 1; i <= n; ++i) // 以i开头的最长不下降子序列
{
dp[i][1] = 1;
for (int j = i + 1; i <= n; ++i)
{
if (a[i] > a[j])
{
dp[i][1] = max(dp[i][1], dp[j][1] + 1);
}
}
}
int ans = -1e9;
for (int i = 1; i <= n; ++i) ans = max(ans, dp[i][0] + dp[i][1] - 1);
cout << n - ans;
return 0;
}
错误原因
第
for (int i = n; i >= 1; --i)
{
dp[i][1] = 1;
for (int j = i + 1; j <= n; ++j)
{
if (a[i] > a[j])
{
dp[i][1] = max(dp[i][1], dp[j][1] + 1);
}
}
}
因为是后效性的问题,如果是求 dp[1][1]的答案,会询问到 2~n的dp[j][1],dp[j][1] 还没有求出来
正确代码
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int dp[N][2], a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) // 以i结尾的最长上升子序列
{
dp[i][0] = 1;
for (int j = 1; j < i; ++j)
{
if (a[i] > a[j])
{
dp[i][0] = max(dp[i][0], dp[j][0] + 1);
}
}
}
for (int i = n; i >= 1; i--) // 以i开头的最长不下降子序列
{
dp[i][1] = 1;
for (int j = i + 1; j <= n; ++j)
{
if (a[i] > a[j])
{
dp[i][1] = max(dp[i][1], dp[j][1] + 1);
}
}
}
int ans = -1e9;
for (int i = 1; i <= n; ++i) ans = max(ans, dp[i][0] + dp[i][1] - 1);
cout << n - ans;
return 0;
}
T4 P1657 选书
错误代码
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
bool vis[N];
int b[N], box[N];
struct node
{
int x, y;
}a[N];
int n, cnt;
bool check()
{
for (int i = 1; i <= n; ++i)
{
if (!(box[i] == a[i].x || box[i] == a[i].y)) return 0;
}
return 1;
}
void dfs(int cur)
{
if (cur == n + 1)
{
if (check())
{
cnt++;
// for (int i = 1; i <= n; ++i) cout << box[i] << ' ';
// cout << "\n";
}
return ;
}
for (int i = 1; i <= n; ++i)
{
if (!vis[i])
{
box[cur] = i;
vis[i] = 1;
dfs(cur + 1);
vis[i] = 0;
}
}
return ;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i].x >> a[i].y;
b[i] = i;
}
dfs(1);
cout << cnt;
return 0;
}
错误原因
时间问题,因为题目中要求每个人二本书至少有一本领到,所以没有必要每次都遍了每本书,只需要遍历每个人喜欢的而本书就行了
正确代码
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
bool vis[N];
int b[N], box[N];
struct node
{
int x, y;
}a[N];
int n, cnt;
void dfs(int cur)
{
if (cur == n + 1)
{
cnt++;
}
for (int i = 1; i <= n; ++i)
{
if (!vis[i] && (i == a[cur].x || i == a[cur].y))
{
box[cur] = i;
vis[i] = 1;
dfs(cur + 1);
vis[i] = 0;
}
}
return ;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i)
{
cin >> a[i].x >> a[i].y;
b[i] = i;
}
dfs(1);
cout << cnt;
return 0;
}
T5 P1466 [USACO2.2] 集合 Subset Sums
题目思路
状态表示:dp[i][j]表示前i个数字任意选择,和为j的方案数
动态转移方程:dp[i][j] = dp[i - 1][j] + dp[i - 1][j - a[i]],因为如果前i-1个可以平凑成j那么前i个肯定可以平凑成j,所以加上方案数,因为如果前i-1个可以平凑成j-a[i]那么前i个肯定可以平凑成j
答案:dp[n][(a[1]+a[2]+a[3]...+a[n])/2]
初始化和边界:dp[i][0]初始化为1
正确代码
#include <bits/stdc++.h>
using namespace std;
const int N = 45, M = 1500;
int dp[N][M], a[N];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n, sum = 0;
cin >> n;
for (int i = 1; i <= n; ++i) a[i] = i, sum += a[i];
if (sum % 2)
{
cout << 0;
return 0;
}
dp[0][0] = 0;
for (int i = 1; i <= n; ++i)
{
dp[i][0] = 1;
for (int j = 1; j <= sum; ++j)
{
if (j < a[i]) dp[i][j] = dp[i - 1][j];
else dp[i][j] = dp[i - 1][j] + dp[i - 1][j - a[i]];
}
}
cout << dp[n][sum / 2];
return 0;
}
T6 P1475 [USACO2.3] 控制公司 Controlling Companies
题目思路
那么我们把题目中控制公司看做一个图,再dfs看
正确代码
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int maxx, n;
int a[N][N];
int cnt[N];
bool ops[N], vis[N];
// ops[j]表示公司j是否被i公司控制
// vis[cur]表示公司cur是否来过
// cnt[j]表示公司i一共(加上已占有公司对公司j的股份)占有公司j的股份是多少
// a[i][j]表示公司i占有公司j的股份
void dfs(int cur)
{
if (vis[cur]) return ;
vis[cur] = 1;
for (int i = 1; i <= maxx; ++i)
{
cnt[i] += a[cur][i]; // 加上公司cur占有公司j的股份
if (cnt[i] > 50) // 占有公司i
{
ops[i] = 1; // 标记为占有
dfs(i); // 看占有公司是否可以占有其他公司
}
}
return ;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; ++i)
{
int x, y, p;
cin >> x >> y >> p;
a[x][y] = p;
maxx = max(maxx, max(x, y)); // 求最大公司编号
}
for (int i = 1; i <= maxx; ++i)
{
memset(ops, 0, sizeof ops);
memset(cnt, 0, sizeof cnt);
memset(vis, 0, sizeof vis);
dfs(i);
for (int j = 1; j <= maxx; ++j)
{
if (ops[j] && i != j) cout << i << ' ' << j << "\n"; // 公司j是否被占有,且不包括i公司
}
}
return 0;
}