【dp】 Codeforces 2B
来源:互联网 发布:电力定额预算软件 编辑:程序博客网 时间:2024/05/16 01:42
这题目序号是2B,也确实挺让我犯2B的
错误思想
记录到达某点(i,j)的路径上,0最少的个数,同时记录了2,5的个数
看似是可做了的,但实际上有很强的后效性
比如这个数据
3
2 2 2
5 2 2
1 1 25
正确答案是1,我的答案是2
因为之前选取的2,5会影响到之后能不能走某条路,后效性在此
那么应该如何解决这个问题呢
即做两遍dp
第一遍dp记录到某点2最少的个数
第二遍dp记录到某点5最少的个数
分开记录时则不必担心后效性了!(想一想)
然后取较少路径即可
但是!
如果有个数字是0,那就得分情况了
1、答案大于1,则输出0, 再随意构造一个为0的方案
2、答案等于0,则输出0与方案
code
#include <algorithm>#include <ctime>#include <string>#include <memory>#include <cstdio>#include <climits>#include <cstring>#include <cstdlib>#include <iostream>using namespace std;const int maxn = 1024 + 5;typedef unsigned int uint;typedef long long int64;typedef unsigned long long uint64;template <class T> inline T Sqr(T x) { return x * x; }template <class T> inline T Abs(T x) { return x > 0 ? x : -x; }template <class T> inline T Min(T a, T b) { return a < b ? a : b; }template <class T> inline T Max(T a, T b) { return a > b ? a : b; }struct node { int x, y; } way[2] = { { 1, 0 }, { 0, 1 } };int x0, y0, ans0;int n, g[maxn][maxn][2];int f2[maxn][maxn], f5[maxn][maxn];int p2[maxn][maxn][2], p5[maxn][maxn][2];void calc(int i, int j, int x){ if (x == 0) { g[i][j][0] = g[i][j][1] = 1; ans0 = INT_MAX, x0 = i, y0 = j; return; } int mix = 0; while (x % 2 == 0) x /= 2, ++mix; g[i][j][0] = mix; mix = 0; while (x % 5 == 0) x /= 5, ++mix; g[i][j][1] = mix;}void init(){ scanf("%d", &n); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { int x; scanf("%d", &x); calc(i, j, x); }}void dp2(){ memset(f2, 127, sizeof(f2)); f2[1][1] = g[1][1][0]; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) for (int k = 0; k < 2; ++k) if (i + way[k].x <= n && j + way[k].y <= n) if (f2[i + way[k].x][j + way[k].y] > f2[i][j] + g[i + way[k].x][j + way[k].y][0]) { f2[i + way[k].x][j + way[k].y] = f2[i][j] + g[i + way[k].x][j + way[k].y][0]; p2[i + way[k].x][j + way[k].y][0] = i; p2[i + way[k].x][j + way[k].y][1] = j; }}void dp5(){ memset(f5, 127, sizeof(f5)); f5[1][1] = g[1][1][1]; for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) for (int k = 0; k < 2; ++k) if (i + way[k].x <= n && j + way[k].y <= n) if (f5[i + way[k].x][j + way[k].y] > f5[i][j] + g[i + way[k].x][j + way[k].y][1]) { f5[i + way[k].x][j + way[k].y] = f5[i][j] + g[i + way[k].x][j + way[k].y][1]; p5[i + way[k].x][j + way[k].y][0] = i; p5[i + way[k].x][j + way[k].y][1] = j; }}void print(){ string ss(""); int chk(0), ans(0); f2[n][n] < f5[n][n] ? ans = f2[n][n], chk = 1 : ans = f5[n][n]; if (ans != 0 && ans0 == INT_MAX) { puts("1"); for (int i = 1; i <= x0 - 1; ++i) putchar('D'); for (int i = 1; i <= y0 - 1; ++i) putchar('R'); for (int i = x0; i <= n - 1; ++i) putchar('D'); for (int i = y0; i <= n - 1; ++i) putchar('R'); return; } printf("%d\n", ans); if (chk) for (int u(n), v(n), uu, vv; ; ) { uu = u, vv = v; if (p2[u][v][0] == u) ss = "R" + ss; else ss = "D" + ss; u = p2[uu][vv][0], v = p2[uu][vv][1]; if (u == 1 && v == 1) break; } else for (int u(n), v(n), uu, vv; ; ) { uu = u, vv = v; if (p5[u][v][0] == u) ss = "R" + ss; else ss = "D" + ss; u = p5[uu][vv][0], v = p5[uu][vv][1]; if (u == 1 && v == 1) break; } cout << ss;}int main(){ freopen("2B.in", "r", stdin); freopen("2B.out", "w", stdout); init(), dp2(), dp5(), print(); return 0;}
想要更规范的题解?
http://pan.baidu.com/share/link?shareid=119928&uk=2652110486
- 【dp】 Codeforces 2B
- Codeforces 2B (DP)
- 【dp】Codeforces Beta Round #2 B
- Codeforces Beta Round #2--B题 (DP)
- CodeForces 407B DP
- Codeforces - 577B dp
- codeforces 587B(dp)
- codeforces 414B (DP)
- Codeforces 679B - dp
- Codeforces 577B(dp)
- codeforces 855B DP
- 字符串dp codeforces B
- codeforces 2B B. The least round way(dp+数论)
- codeforces 375B B. Maximum Submatrix 2(dp)
- codeforces 240B - Fence DP
- Codeforces 258B 数位DP
- codeforces 414b(数位DP)
- DP 之 codeforces 416B
- ActiveReport在.net中的简单应用
- ubuntun 安装myql
- Struts2中的s:if判断字符串是否相等要注意单引号和双引号
- Users not mapped!!!
- fullCalendar在C#下的应用
- 【dp】 Codeforces 2B
- HttpServletRequestt的应用
- 该怎么说,爱到最后成了路人甲,陌路天涯
- C++ 对象的内存布局(上)
- Win8开发从入门到项目 - 视频下载列表
- TextView 处理 超链接
- 使用Aspose.Words如何将图像转换为PDF
- flex fx,mx,s的区别
- Windows Phone 8应用开发工具特性详解