DP训练总结
前言
嗯……大概就是一道题没做对……但是得了第三名
具体怎么样……自己看吧……
| 名字 | A | B | C | D | E |
|---|---|---|---|---|---|
| zzilin | 100 | 100 | 100 | 100 | 0 |
| yzz_sc03 | 100 | 0 | 40 | 0 | 11 |
| yyc0929(我) | 60 | 60 | 0 | ||
| daneil2014 | 100 | 20 | 0 | ||
| chong1010 | 40 | 20 | 40 | 0 | 0 |
| SCX2014 | 0 | 10 | 40 | 0 | 0 |
| liuyilun123 | 0 | 20 | 0 | 0 | 22 |
| guanhangyu | 0 | 20 | 20 |
第一次放成绩表……
不得不说,第一挺牛的
P1002
原题……
题目传送门
题意
棋盘上
错误代码
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
int n, m, cx, cy;
int ans;
int dp[N][N];
bool vis[N][N];
int dx[] = {-1, 1, -2, 2, -1, 1, -2, 2};
int dy[] = {2, -2, 1, -1, -2, 2, -1, 1};
int main()
{
cin >> n >> m >> cx >> cy;
n ++, m ++, cx ++, cy ++;
vis[cx][cy] = 1;
for(int i = 0; i < 8; i ++)
{
int nx = cx + dx[i];
int ny = cy + dy[i];
if(nx <= 0 || ny <= 0 || nx > n || ny > m)
continue;
vis[nx][ny] = 1;
}
dp[1][1] = 1;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
{
if(i == 1 && j == 1)
continue;
if(vis[i][j] == 1)
continue;
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
// for(int i = 1; i <= n; i ++)
// {
// for(int j = 1; j <= m; j ++)
// cout << vis[i][j] << " ";
// cout << "\n";
// }
cout << dp[n][m];
return 0;
}
思路
首先,我们不用DFS做哈。
那我们直接跳到四步法。
- 状态:
dp[i][j] 表示到达(i,j) 的路径数量。 - 答案:dp[n][m]
- 状态转移:继承上一步的路径数就可以了。
for(int i = 1; i <= n; i ++) for(int j = 1; j <= m; j ++) { if(i == 1 && j == 1) continue; if(vis[i][j] == 1) continue; dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; } - 初始:dp[1][1] = 1
注意!!!!!
不开long long见祖宗!
输入点不加1见祖宗!
正确代码‘
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
int n, m, cx, cy;
int ans;
long long dp[N][N];
bool vis[N][N];
int dx[] = {-1, 1, -2, 2, -1, 1, -2, 2};
int dy[] = {2, -2, 1, -1, -2, 2, -1, 1};
int main()
{
cin >> n >> m >> cx >> cy;
n ++, m ++, cx ++, cy ++;
vis[cx][cy] = 1;
for(int i = 0; i < 8; i ++)
{
int nx = cx + dx[i];
int ny = cy + dy[i];
if(nx <= 0 || ny <= 0 || nx > n || ny > m)
continue;
vis[nx][ny] = 1;
}
dp[1][1] = 1;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
{
if(i == 1 && j == 1)
continue;
if(vis[i][j] == 1)
continue;
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
}
// for(int i = 1; i <= n; i ++)
// {
// for(int j = 1; j <= m; j ++)
// cout << vis[i][j] << " ";
// cout << "\n";
// }
cout << dp[n][m];
return 0;
}
错误原因
因为我忘记方案数大多数要开long long了,所以。。。嗯。。。应该不用讲了。。。
P2049
题目传送门
好难啊。 。 。 。 。 。 。 。 。
题意
有一个
思路
这题理解还是很容易的,对吧?就是求所有路径上的数值乘积模
“那就定义
很抱歉,这个方法不行,会MLE的。
所以我们换一种思路:
初始:
那么答案就很好确定了:枚举余数,若dp[n][m][p] == true,答案就多一种。接着输出答案就可以了。
状态转移:既然我们自然是枚举余数,那p是按这一步还是按上一步呢?啊,这一步?那你告诉我,怎么干?所以嘛,按上一步。
初始想都不用想,直接dp[1][1][a[1][1]%k] = 1。
正确代码
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int n, m, k;
int ans;
int a[N][N];
bool dp[N][N][N];
int main()
{
cin >> n >> m >> k;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
{
cin >> a[i][j];
a[i][j] %= k;
}
dp[1][1][a[1][1]] = 1;
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
for(int p = 0; p < k; p ++)
if(dp[i - 1][j][p] == 1 || dp[i][j - 1][p] == 1)
dp[i][j][p * a[i][j] % k] = 1;
for(int p = 0; p < k; p ++)
if(dp[n][m][p] == 1)
ans ++;
cout << ans << "\n";
for(int p = 0; p < k; p ++)
if(dp[n][m][p] == 1)
cout << p << " ";
return 0;
}
P2196
题目传送门
题意
有
错误代码
也不知道怎么骗到60分的。。。
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
int n;
int ans, pos;
int a[N], dp[N];
vector<int> g[N], box[N];
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
cin >> a[i];
for(int i = 1; i < n; i ++)
for(int j = i + 1; j <= n; j ++)
{
int x;
cin >> x;
if(x == 1)
g[j].push_back(i);
}
for(int i = 1; i <= n; i ++)
{
dp[i] = a[i];
box[i].push_back(i);
}
for(int i = 1; i <= n; i ++)
for(auto x:g[i])
if(dp[i] < dp[x] + a[i])
{
dp[i] = dp[x] + a[i];
box[i].push_back(x);
}
for(int i = 1; i <= n; i ++)
if(ans < dp[i])
{
ans = dp[i];
pos = i;
}
sort(box[pos].begin(), box[pos].end());
for(auto x:box[pos])
cout << x << " ";
cout << "\n";
cout << ans;
return 0;
}
思路
我们显然先解决相对更简单的第二小题吧。
开始吧,四步法一步步来。
状态:这个很简单,
答案:同样枚举,ans = max(dp[i])。
状态转移肯定就是枚举上一步。输入连通的时候,维护一个
初始:dp[i] = a[i],怎么,难道你一开始把这a[i]个扔了?
第二问就解决了。“啊,才第二问?”
其实我们把最终我们的终点是哪一个存起来,过程用数组存,用递归把过程深搜出来就行了。
正确代码
#include <bits/stdc++.h>
using namespace std;
const int N = 25;
int n;
int ans, pos;
int a[N], dp[N], pre[N];
bool g[N][N];
void print(int x)
{
if(pre[x] == 0)
{
cout << x;
return;
}
print(pre[x]);
cout << " " << x;
}
int main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
cin >> a[i];
for(int i = 1; i < n; i ++)
for(int j = i + 1; j <= n; j ++)
cin >> g[i][j];
for(int i = 1; i <= n; i ++)
dp[i] = a[i];
for(int i = 1; i <= n; i ++)
for(int j = 1; j < i; j ++)
if(g[j][i] == 1)
if(dp[j] + a[i] > dp[i])
dp[i] = dp[j] + a[i], pre[i] = j;
for(int i = 1; i <= n; i ++)
if(ans < dp[i])
{
ans = dp[i];
pos = i;
}
print(pos);
cout << "\n" << ans;
return 0;
}
错误原因
嗯……
细思极恐
差距。 。 。 还挺。 。 。 大的。 。 。
第二问思路差不太多,但第一问想复杂了。 。 。
B4135
题目传送门
题意
有
思路
出题人你怎么想的,暴力的分都不给(听说的)
“不止吧!”
嗯???
“定义都不给道。”
不可能,绝对不可能!只不过是你想不到罢了。
你要么不选,要么选2个,很奇怪,是不是?
那也跟B题一样嘛,也定一个bool的
那状态就是
答案很简单,就是max(dp[n][1], dp[n][0])。
状态转移:第一个,嗯对,初始化。第二个,嗯对,也是初始化。从第三个开始。
如果第
如果不选,那么第
初始前面也已经提示你了,dp[1][0] = 0, dp[2][0] = 0, dp[2][1] = a[1] + a[2]。
代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int n;
int a[N], dp[N][2];
signed main()
{
cin >> n;
for(int i = 1; i <= n; i ++)
cin >> a[i];
dp[2][1] = a[1] + a[2];
for(int i = 3; i <= n; i ++)
{
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]);
dp[i][1] = dp[i - 2][0] + a[i - 1] + a[i];
}
cout << max(dp[n][0], dp[n][1]);
return 0;
}
P2758
题目传送门
可惜考试来不及了
题意
有
- 删除
- 插入
- 改字符
问把
思路
四步法,来吧!
- 状态:
dp[i][j] 表示把A的前i 位变成B的前j 位花费的最少次数。 - 答案:dp[lena][lenb]。
- 状态转移:
那我们就不得不谈这三个东西了:
- 删除:就等于让A的前
i - 1 位和B的前j 位相等嘛,那就是dp[i - 1][j] + 1 - 插入:这个就是让A的前
i 位和B的前j - 1 位相等,dp[i][j - 1] + 1 。 - 替换:让A的前
i - 1 位和B的前j - 1 位相等,dp[i - 1][j - 1] + 1 。 - 如果都一样,那就是dp[i][j] = dp[i - 1][j - 1]啦!
很显然,再把它们取min即可。
- 初始:要是把A的前i位和B的前0位比较,只能全删,要是把A的前0位和B的前i位比较,只能全增,所以就是dp[i][0] = dp[0][i] = i。
代码
#include <bits/stdc++.h> #define int long long using namespace std;
const int N = 2e3 + 5; string a, b; int dp[N][N]; signed main() { cin >> a >> b; a = "#" + a; b = "#" + b; for(int i = 0; i <= a.size(); i ++) dp[i][0] = i; for(int i = 0; i <= b.size(); i ++) dp[0][i] = i; for(int i = 1; i <= a.size(); i ++) for(int j = 1; j <= b.size(); j ++) if(a[i] != b[j]) dp[i][j] = min(dp[i - 1][j], min(dp[i][j - 1], dp[i - 1][j - 1])) + 1; else dp[i][j] = min(dp[i - 1][j - 1], min(dp[i][j - 1], dp[i - 1][j]) + 1); cout << dp[a.size()][b.size()]; return 0; }