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+ ...
随机推荐
- 一个简单爬免费代理IP的脚本
- DTW 算法(转)
DTW为(Dynamic Time Warping,动态时间归准)的简称.应用很广,主要是在模板匹配中,比如说用在孤立词语音识别,计算机视觉中的行为识别,信息检索等中.可能大家学过这些类似的课程都看到 ...
- linux运维记
nmap 127.0.0.1 命令查看当前服务器对外有多少端口,用于检查漏洞 vim ctrl+z ,jobs,fg 切换控制应用程序 vim 执行命令 #!sh aa.sh执行命令 运维系统监控开源 ...
- 【bzoj4373】算术天才⑨与等差数列
同之前那道由乃题,可以认为由乃题是这题的特殊情况…… 维护方法是同样的,维护区间和,区间平方和即可. 注意特判一个数(其实没有必要) #include<bits/stdc++.h> ; u ...
- 一个真正的客户端非阻塞的 connect
前言 - 一个简短开场白 winds 的 select 和 linux 的 select 是两个完全不同的东西. 然而凡人喜欢把它们揉在一起. 非阻塞的connect业务是个自带超时机制的 conn ...
- grep常见操作整理(更新)
提取邮箱和URL [root@test88 ~]# cat url_email.txt root@gmail.com,http://blog.peter.com,peter@qq.com [root@ ...
- JS对象转化为JSON字符串
js方法: JSON.stringify 把一个对象转换成json字符串 JSON.parse 把一个json字符串解析成对象. 实例: var jsObj = {}; jsObj.testArray ...
- linux命令(37):locate命令
1.命令格式: Locate [选择参数] [样式] 2.命令功能: locate命令可以在搜寻数据库时快速找到档案,数据库由updatedb程序来更新,updatedb是由cron daemon周期 ...
- BZOJ1898: [Zjoi2004]Swamp 沼泽鳄鱼
1898: [Zjoi2004]Swamp 沼泽鳄鱼 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 478 Solved: 286[Submit][St ...
- (1)go 环境搭建
1 .下载安装 https://golang.org/ 2.环境变量配置 安装后会自动配置三个环境变量 (1) GOROOT: (2) PATH: (3) GOPATH GOPATH 从1.8开始,w ...