CF2B
非常恶心的dp题,测试数据也恶心,坑多,特别是0的坑
#include <iostream>
#include <utility>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
#define fi(i, a, b) for (int i = a; i <= b; ++i)
#define fr(i, a, b) for (int i = a; i >= b; --i)
#define x first
#define y second
#define sz(x) ((int)(x).size())
#define pb push_back
using pii = pair<int, int>;
//#define DEBUG
int s[1005][1005];
int dp[1005][1005][2];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
int flag = 0;
int fx, fy;
fi(i, 1, n) fi(j, 1, n)
{
cin >> s[i][j];
if (s[i][j] == 0)
{
if (!flag)
{
flag = 1;
fx = i;
fy = j;
}
continue;
}
while (s[i][j] % 2 == 0)
{
s[i][j] /= 2;
dp[i][j][0]++;
}
while (s[i][j] % 5 == 0)
{
s[i][j] /= 5;
dp[i][j][1]++;
}
}
fi(i, 1, n) fi(j, 1, n)
{
if (i == 1 && j == 1)
continue;
int p, q, r, ss, res1, res2;
p = q = r = ss = 0x3f3f3f3f;
if (s[i - 1][j] != 0)
{
p = dp[i][j][0] + dp[i - 1][j][0];
q = dp[i][j][1] + dp[i - 1][j][1];
}
if (s[i][j - 1] != 0)
{
r = dp[i][j][1] + dp[i][j - 1][1];
ss = dp[i][j][0] + dp[i][j - 1][0];
}
res1 = min(p, ss);
res2 = min(q, r);
if (res1 == 0x3f3f3f3f && res2 == 0x3f3f3f3f)
continue;
dp[i][j][0] = res1;
dp[i][j][1] = res2;
}
int count = 0;
// fi(i, 1, n) fi(j, 1, n)
// {
// count++;
// cout << dp[i][j][0] << " " << dp[i][j][1] << " " << count << endl;
// }
string ans = "";
int res = dp[n][n][0] > dp[n][n][1] ? 1 : 0;
int i = n, j = n;
while (1)
{
if (dp[i - 1][j][res] <= dp[i][j - 1][res])
{
if (s[i - 1][j] != 0)
{
i--;
ans += "D";
}
else if (s[i][j - 1] != 0)
{
j--;
ans += "R";
}
}
else if (dp[i - 1][j][res] >= dp[i][j - 1][res])
{
if (s[i][j - 1] != 0)
{
j--;
ans += "R";
}
else if (s[i - 1][j] != 0)
{
i--;
ans += "D";
}
}
if (s[i - 1][j] == 0 && s[i][j - 1] == 0)
break;
//走到第一行或第一列
if (i == 1)
{
for (int x = 1; x < j; x++)
ans += "R";
break;
}
if (j == 1)
{
for (int x = 1; x < i; x++)
ans += "D";
break;
}
}
reverse(ans.begin(), ans.end());
// fi(i, 1, n) fi(j, 1, n)
// {
// cout << dp[i][j][0] << " " << dp[i][j][1] << endl;
// }
if (ans.size() < 2 * n - 2)
{
cout << 1 << endl;
string temp = "";
fi(i, 1, fx - 1) temp += 'D';
fi(i, 1, fy - 1) temp += 'R';
fi(i, fx, n - 1)
{
temp += 'R';
}
fi(j, fy, n - 1)
{
temp += 'D';
}
cout << temp << endl;
return 0;
}
if (flag)
{
if (1 <= min(dp[n][n][0], dp[n][n][1]))
{
cout << 1 << endl;
string temp = "";
fi(i, 1, fx - 1) temp += 'D';
fi(i, 1, fy - 1) temp += 'R';
fi(i, fx, n - 1)
{
temp += 'R';
}
fi(j, fy, n - 1)
{
temp += 'D';
}
cout << temp << endl;
return 0;
}
else
{
cout << 0 << endl;
cout << ans << endl;
return 0;
}
}
else
{
cout << min(dp[n][n][0], dp[n][n][1]) << endl;
cout << ans << endl;
}
#ifdef DEBUG
//freopen(D:\in.txt,r,stdin);
#endif
return 0;
}
CF2B的更多相关文章
- CF2B The least round way(贪心+动规)
题目 CF2B The least round way 做法 后面\(0\)的个数,\(2\)和\(5\)是\(10\)分解质因数 则把方格中的每个数分解成\(2\)和\(5\),对\(2\)和\(5 ...
- CF2B The least round way(dp+记录路径)
B. The least round way time limit per test 2 seconds memory limit per test 64 megabytes input standa ...
- CF2B The least round way 题解
都是泪呀...↑ 题目传送门 题意(直接复制了QWQ) 题目描述 给定由非负整数组成的\(n \times n\)的正方形矩阵,你需要寻找一条路径: 以左上角为起点, 每次只能向右或向下走, 以右下角 ...
- CF2B The least round way
[题解] 可以发现10的因数除了1和10之外只有2和5了,那么走过的路径上各个数字的2的因数个数之和.5的因数个数之和中较小的一个即是答案.这样的话DP即可.同时需要注意有0的情况,有0的时候有一个答 ...
- [CF2B] The least round way - dp
给定由非负整数组成的n×n 的正方形矩阵,你需要寻找一条路径: 以左上角为起点 每次只能向右或向下走 以右下角为终点 并且,如果我们把沿路遇到的数进行相乘,积应当是最小"round" ...
- C# 操作的时候接收用户输入密码进行确认
首先新建一个原始窗体,如下:
- dp入门30题
前言:本文章主要记录一些 \(dp\) 入门题,都是我做过的,希望读者能从这些基础题中打好 \(dp\) 扎实的基础,有不足的地方也欢迎指出.大部分是 \(CodeFoces\) 和 \(Atcode ...
随机推荐
- WPF之单例模式
项目 2019/10/09 问题 2019年10月9日星期三 上午2:46 1.为了实现单例模式,在App类中添加了如下代码,使用了信号量,但是为什么返回;isNew一直为true public ...
- windows下IPv4通信(C++、MFC)
Cilect #include <stdio.h> #include <Ws2tcpip.h> #include <winsock2.h> #define HELL ...
- 用.NET代码生成JSON Schema 验证器
问题 对于验证复杂JSON数据是否合法的需求,通常的解决方式是标准JSON Schema,.Net下有对应的JSON Schema实现库.应用程序通常需要将标准JSON schema传入实现库,来做后 ...
- swift_slowAlloc Crash 分析
一.Crash详情 Crash类型 exception EXC_BREAKPOINT (SIGTRAP) reason EXC_BREAKPOINT EXC_ARM_BREAKPOINT fault_ ...
- Windows 11提示“无法枚举容器中的对象。”
*为什么会出现这一错误提示?* 在Windows系统当中,对文件或文件夹的权限进行设置可以有效地保护隐私内容.登录管理员账户可以对权限进行更改,并且有权决定是否将内容共享给多个用户使用.但是在某些情况 ...
- fabric compose文件解读(Orderer篇)
orderer在fabric中的作用是排序,另外orderer有点像是管理节点一样,通道之类的都是在orderer的基础之上建立的,有点像比特币,以太坊上面的全节点一样,不过责任比全节点少很多,甚至都 ...
- 8.4考试总结(NOIP模拟30)[毛一琛·毛二琛·毛三琛]
最有名的莫过于想死一次吗. 前言 至今都不知道题目是个啥... T1 毛一琛 解题思路 \(\mathcal{Meet\;In\;The\;Middle}\) 其实就是一个爆搜... 把整个区间分为两 ...
- Python3基本语法(新)
目录 基本语法 输出print() 格式化输出 标识符 import关键字 保留字(关键字) 注释 多行注释 1.单引号(''') 2.双引号(""") 缩进 空行 同一 ...
- 微信实名认证申请单报错:请求中含有未在API文档中定义的参数
完整错误: {"code":"PARAM_ERROR","detail":{"location":null," ...
- 10位,13位时间戳转为C#.NET格式时间 DateTime
10位,13位时间戳转为C#.NET格式时间 DateTime - public static DateTime ToDateTime( string timestamp) { var tz = Ti ...