Luogu 1415-拆分数列-动态规划
Solution
首先要找到使得最后一个数最小, 只需定义一个数组$pre[i]$ 从区间$[pre[i], i]$表示的数, 是最小的能使前面的数递增的方案。
$[ pre[n], n]$即为最小的最后一个数。
接着我们依据这找出的最后一个数, 向前dp, 找出使得每个数都最大的方案。
前导0是非常坑的
我觉得我也不怎么懂这道dp, 看得有点懵TAT
Code
#include<cstdio>
#include<cstring>
#include<algorithm>
#define rd read()
#define rep(i,a,b) for(int i = (a); i <= (b); ++i)
#define per(i,a,b) for(int i = (a); i >= (b); --i)
using namespace std; const int N = ; int pre[N], nxt[N], a[N], n;
int ans[N]; char s[N]; bool cmp(int l1, int r1, int l2, int r2) {
while(a[l1] == && l1 <= r1) l1++;
while(a[l2] == && l2 <= r2) l2++;
if(r1 < l1 || r2 < l2) return ;
if(r1 - l1 < r2 - l2) return ;
else if(r1 - l1 > r2 - l2) return ;
for(int i = ; i <= r2 - l2; ++i)
if(a[l1 + i] > a[l2 + i]) return ;
else if(a[l1 + i] < a[l2 + i]) return ;
return ;
} int main()
{
scanf("%s", s + );
n = strlen(s + );
for(int i = ; i <= n; ++i) a[i] = s[i] - '';
for(int i = ; i <= n; ++i) pre[i] = ;
for(int i = ; i <= n; ++i)
for(int j = i; j >= ; --j)
if(cmp(pre[j - ], j - , j, i)) {
pre[i] = j; break;
}
nxt[pre[n]] = n;
for(int i = pre[n]; i <= n; ++i) nxt[i] = n;
int pos0 = pre[n];
while(a[pos0 - ] == ) pos0--, nxt[pos0] = n;
for(int i = pre[n] - ; i; --i)
for(int j = pre[n] - ; j >= i; --j)
if(cmp(i, j, j + , nxt[j + ])) {
nxt[i] = j; break;
}
for(int l = , i = nxt[]; l <= n; i = nxt[l]) {
for(int j = l; j <= i; j++) printf("%d",a[j]);
l = i + ;
if(l > n) break;
printf(",");
}
putchar('\n');
}
Luogu 1415-拆分数列-动态规划的更多相关文章
- luogu P2401 不等数列 |动态规划
题目描述 将1到n任意排列,然后在排列的每两个数之间根据他们的大小关系插入">"和"<".问在所有排列中,有多少个排列恰好有k个"< ...
- 洛谷P1415 拆分数列[序列DP 状态 打印]
题目背景 [为了响应党中央勤节俭.反铺张的精神,题目背景描述故事部分略去^-^] 题目描述 给出一列数字,需要你添加任意多个逗号将其拆成若干个严格递增的数.如果有多组解,则输出使得最后一个数最小的同时 ...
- luoguP1415 拆分数列 [dp]
题目描述 给出一列数字,需要你添加任意多个逗号将其拆成若干个严格递增的数.如果有多组解,则输出使得最后一个数最小的同时,字典序最大的解(即先要满足最后一个数最小:如果有多组解,则使得第一个数尽量大:如 ...
- CJOJ 1644 编辑距离 / Luogu 2758 编辑距离(动态规划)
CJOJ 1644 编辑距离 / Luogu 2758 编辑距离(动态规划) Description 字符串是数据结构和计算机语言里很重要的数据类型,在计算机语言中,对于字符串我们有很多的操作定义,因 ...
- 洛谷 P1415 拆分数列 解题报告
拆分数列 题目背景 [为了响应党中央勤节俭.反铺张的精神,题目背景描述故事部分略去^-^] 题目描述 给出一列数字,需要你添加任意多个逗号将其拆成若干个严格递增的数. 如果有多组解,则输出使得最后一个 ...
- 洛谷P1415 拆分数列(dp)
题目链接:传送门 题目: 题目背景 [为了响应党中央勤节俭.反铺张的精神,题目背景描述故事部分略去^-^] 题目描述 给出一列数字,需要你添加任意多个逗号将其拆成若干个严格递增的数.如果有多组解,则输 ...
- Luogu 2679 子串 (动态规划)
Luogu 2679 NOIP 2015 子串 (动态规划) Description 有两个仅包含小写英文字母的字符串 A 和 B.现在要从字符串 A 中取出 k 个互不重叠的非空子串,然后把这 k ...
- BZOJ 1500/Luogu 2042 - 维修数列 - [NOI2005][Splay]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1500 题目链接:https://www.luogu.org/problemnew/sho ...
- 洛谷P1415 拆分数列
题目背景 [为了响应党中央勤节俭.反铺张的精神,题目背景描述故事部分略去^-^] 题目描述 给出一列数字,需要你添加任意多个逗号将其拆成若干个严格递增的数.如果有多组解,则输出使得最后一个数最小的同时 ...
随机推荐
- vue分页 点击(非下拉)
1.主页面 <template> <div class="list"> <template v-if="count"> 55 ...
- mysql 性能测试工具 mysqlslap
原文链接: https://my.oschina.net/moooofly/blog/152547 连接数据库: # mysqlslap -h localhost -uroot -p123456 -- ...
- jquery获取焦点位于的元素
参考 https://zhidao.baidu.com/question/497311181016643684.html if ($(":focus").length > 0 ...
- leetcode 数组类型题
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h& ...
- GIT 命令集
Git图形化界面 下面是我整理的常用 Git 命令清单.几个专用名词的译名如下. Workspace:工作区 Index / Stage:暂存区 Repository:仓库区(或本地仓库) Remot ...
- TOJ 4976: 新生数(深搜)
传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=4976 时间限制(普通/Java): ...
- Codeforces Round #450 (Div. 2)
Codeforces Round #450 (Div. 2) http://codeforces.com/contest/900 A #include<bits/stdc++.h> usi ...
- http://www.rabbitmq.com/documentation.html
http://www.rabbitmq.com/documentation.html https://www.gitbook.com/book/geewu/rabbitmq-quick/details
- Java06-java基础语法(五)数组
Java06-java基础语法(五)数组 一.循环的嵌套 在一个循环体内部再含有一个或多个循环 强调:内循环全部做完以后再去执行下一次的外循环 int k = 0; for(int i = 0; i& ...
- 编程,计算data段中的第一组数据的3次方,结果保存在后面一组dword单元中
assume cs:code data segment dw ,,,,,,, dd ,,,,,,, data ends code segment start: mov ax,data mov ds,a ...