Codeforces Beta Round #2 B. The least round way dp,记录方案
来源:互联网 发布:stm32单片机arm的区别 编辑:程序博客网 时间:2024/06/05 15:55
题目链接:http://codeforces.com/contest/2/problem/B
题意:
给你一个n*n的矩阵
然后这个矩阵你需要从左上角走到右下角
只能走右或者向下。
你要使得你经过的数,乘积起来后面的0的个数最少。
解法:
要得到0实际上就需要考虑2和5的个数,所以DP。dp[i][j][0]表示到i,j位置,2的因子最少多少个
dp[i][j][1]表示到i,j位置,5的因子最少多少个
然后答案显然就是min(dp[n][n][0],dp[n][n][1])了,然后倒着dfs输出答案就好了。
但是这儿有一个hack点,就是如果有一个位置是0的话,答案就最多为1了,这个很显然。
//CF 2B#include <bits/stdc++.h>using namespace std;const int maxn = 1100;int dp[maxn][maxn][2]; ///dp[i][j][0]表示在i,j位置2的因子最少多少个,///dp[i][j][1]表示在i,j位置5的因子最少多少个int cnt[maxn][maxn][2];int g[maxn][maxn][2];///记录当前状态的上一个状态是什么int n, a[maxn][maxn];void print_path(int x, int y, int d){ if(x == 1 && y == 1) return ; if(g[x][y][d] == 1) print_path(x-1, y, d), printf("D"); else print_path(x, y-1, d), printf("R");}int main(){#ifdef just_sort freopen("zxy.txt", "r", stdin); freopen("zxy2.txt", "w", stdout);#endif scanf("%d", &n); for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ scanf("%d", &a[i][j]); } } int posx = 0, posy = 0; for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ if(a[i][j] == 0){ posx = i; posy = j; break; } while(a[i][j]%2==0) cnt[i][j][0]++, a[i][j]/=2; while(a[i][j]%5==0) cnt[i][j][1]++, a[i][j]/=5; } } memset(dp, 0x3f, sizeof(dp)); memset(g, 0, sizeof(g)); dp[1][1][0] = cnt[1][1][0]; dp[1][1][1] = cnt[1][1][1]; for(int i = 1; i <= n; i++){ for(int j = 1; j <= n; j++){ if(i==1&&j==1) continue; for(int k = 0; k < 2; k++){ dp[i][j][k] = cnt[i][j][k]+min(dp[i-1][j][k], dp[i][j-1][k]); if(dp[i-1][j][k]<dp[i][j-1][k]) g[i][j][k] = 1; } } } int ans = min(dp[n][n][0], dp[n][n][1]); int sta; if(dp[n][n][0] < dp[n][n][1]) sta = 0; else sta = 1; if(ans == 0){ printf("0\n"); } else if(posx&&posy){ puts("1"); int nowx = 1, nowy = 1; while(nowx < posx) nowx++, printf("D"); while(nowy < posy) nowy++, printf("R"); while(nowx < n) nowx++, printf("D"); while(nowy < n) nowy++, printf("R"); return 0; } else printf("%d\n", ans); print_path(n, n, sta); return 0;}
0 0
- Codeforces Beta Round #2 B. The least round way dp,记录方案
- Codeforces Beta Round #2B. The least round way
- Codeforces Beta Round #2 B. The least round way
- Codeforces Beta Round #2_B. The least round way(DP)
- codeforces 2B B. The least round way(dp+数论)
- codeforces 2B The least round way DP因子路径
- Codeforces #2B The least round way(DP)
- CodeForces - 2B -The least round way (dp math)
- codeforces 2B The least round way
- codeforces 2B The least round way
- codeforces 2B The least round way
- Codeforces#2B The least round way
- [codeforces] 2B - The least round way
- Codeforces 2B The least round way
- CodeForces B. The least round way(dp)
- 【Codeforces Beta Round 2B】【贪心 DP】The least round way 从(1,1)走到(n,n)乘积尾数0尽可能少
- Codeforces #2B B. The least round way
- Codeforces 2B The least round way 动态规划
- L1-1出生年
- php的运行原理、cgi对比fastcgi以及php-cgi和php-fpm之间的联系区别
- 数据挖掘算法
- codevs1174 靶形数独 dfs+剪枝
- 考研失败2017年3月25日21:11:03
- Codeforces Beta Round #2 B. The least round way dp,记录方案
- mac 下 redis 安装,并配置开机自启动
- 深度学习、图像分类入门,从VGG16卷积神经网络开始
- 【Tomcat】shutdown.bat 错误:java.net.ConnectException错误 找不到8005端口的解决办法
- C++ 解决 multiple definition of & previous definition here编译错误
- 大话设计模式读书笔记(一) 简单工厂模式
- 13. Roman to Integer ★
- 第二十六讲项目三 分段函数求值switch语句版
- 求自定义类型序列中的中位数