Codeforces Beta Round #2 B. The least round way dp
B. The least round way
题目连接:
http://www.codeforces.com/contest/2/problem/B
Description
There is a square matrix n × n, consisting of non-negative integer numbers. You should find such a way on it that
starts in the upper left cell of the matrix;
each following cell is to the right or down from the current cell;
the way ends in the bottom right cell.
Moreover, if we multiply together all the numbers along the way, the result should be the least "round". In other words, it should end in the least possible number of zeros.
Input
The first line contains an integer number n (2 ≤ n ≤ 1000), n is the size of the matrix. Then follow n lines containing the matrix elements (non-negative integer numbers not exceeding 109).
Output
In the first line print the least number of trailing zeros. In the second line print the correspondent way itself.
Sample Input
3
1 2 3
4 5 6
7 8 9
Sample Output
0
DDRR
Hint
题意
给你一个n*n的矩阵
然后这个矩阵你需要从左上角走到右下角
只能走右或者向下。
你要使得你经过的数,乘积起来后面的0的个数最少。
题解:
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了,这个是显然的嘛。
然后就没了。
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 1100;
const int inf = 1e9;
int dp[maxn][maxn][2];
int cnt[maxn][maxn][2];
int g[maxn][maxn][2];
int n,a[maxn][maxn];
void solve(int x,int y,int now)
{
if(x==1&&y==1)return;
if(g[x][y][now]==1)solve(x-1,y,now),printf("D");
else solve(x,y-1,now),printf("R");
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&a[i][j]);
int flagx=0,flagy=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(a[i][j]==0)
{
flagx=i,flagy=j;
break;
}
int pre = a[i][j];
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 st;
if(dp[n][n][0]<dp[n][n][1])st=0;
else st=1;
if(ans==0)puts("0");
else if(flagx&&flagy)
{
puts("1");
int nowx=1,nowy=1;
while(nowx<flagx)nowx++,printf("D");
while(nowy<flagy)nowy++,printf("R");
while(nowx<n)nowx++,printf("D");
while(nowy<n)nowy++,printf("R");
return 0;
}
else printf("%d\n",ans);
solve(n,n,st);
}
Codeforces Beta Round #2 B. The least round way dp的更多相关文章
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #62 题解【ABCD】
Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
- Codeforces Beta Round #13 C. Sequence (DP)
题目大意 给一个数列,长度不超过 5000,每次可以将其中的一个数加 1 或者减 1,问,最少需要多少次操作,才能使得这个数列单调不降 数列中每个数为 -109-109 中的一个数 做法分析 先这样考 ...
- Codeforces Beta Round #79 (Div. 2 Only)
Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...
- Codeforces Beta Round #77 (Div. 2 Only)
Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #75 (Div. 2 Only)
Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...
- Codeforces Beta Round #74 (Div. 2 Only)
Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...
- Codeforces Beta Round #73 (Div. 2 Only)
Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...
随机推荐
- DIV+CSS综合实例【传智PHP首页】
1.首页结构 2.准备工作 所有素材放到与当前网页同级的目录下: 网页背景色.背景图: 主页宽度:1000px: 创建CSS文件,将CSS文件引入到当前的HTML文件中. 3.实现 效果图: HTML ...
- python实战===itchat
import itchat itchat.login() friends=itchat.get_friends(update=True)[0:] male=female=other=0 for i i ...
- C基础 内存越界和内存监测的简单处理
引言 突然感觉要出去走走了, 醒了后 刷完牙就在联系coding, 不知不觉到了 黄昏. 看看天, 打开灯. 又感觉到了 夜夜夜夜 . 13年到北京务工, 遇到一批批NB的同龄人物. 一块工作, 一块 ...
- 在Caffe中使用 DIGITS(Deep Learning GPU Training System)自定义Python层
注意:包含Python层的网络只支持单个GPU训练!!!!! Caffe 使得我们有了使用Python自定义层的能力,而不是通常的C++/CUDA.这是一个非常有用的特性,但它的文档记录不足,难以正确 ...
- dos命令连接mysql并且查看编码方式
打开cmd: 输入:mysql -hlocalhost -uroot -p 然后: show variables like 'char%';
- js cookies的使用及介绍 (非常详细)
设置cookie 每个cookie都是一个名/值对,可以把下面这样一个字符串赋值给document.cookie:document.cookie="userId=828";如果要一 ...
- Leetcode 之Binary Tree Postorder Traversal(45)
层序遍历,使用队列将每层压入,定义两个队列来区分不同的层. vector<vector<int>> levelorderTraversal(TreeNode *root) { ...
- 测试php单例模式和静态访问,实例化访问的效率
// 测试的类class Memory { private static $a= null; public function __construct() { return self::$a; } pu ...
- VS Code使用
VS Code使用了有一段时间了,感觉各方面表现蛮好的,当然主要还是基于electron开发的,(有源代码,想改啥就改啥,当然现在也没有改什么,没那么时间,也没有那么多精力),性能不错,其实中间主要还 ...
- centos7yum安装tomcat
执行安装命令 [root@localhost ~]# yum -y install tomcat 查询tomcat是否安装成功 [root@localhost ~]# rpm -q tomcat to ...