HDU 6357.Hills And Valleys-动态规划(区间翻转l,r找最长非递减子序列)
题意:给一串由n个数字组成的字符串,选择其中一个区间进行翻转,要求翻转后该字符串的最长非降子序列长度最长,输出这个最长非降子序列的长度以及翻转的区间的左右端点
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = ;
const int maxm = ;
int n, a[maxn], dp[maxn][maxm], b[maxm];
int ans, ansl, ansr, l, r, cnt;
int al[maxn][maxm], ar[maxn][maxm];
char s[maxn]; int solve()
{
for(int i = ; i <= cnt; i++)
dp[][i] = ;
for(int i = ; i <= n; i++)
{
for(int j = ; j <= cnt; j++)
{
dp[i][j] = dp[i - ][j];
al[i][j] = al[i - ][j];//al记录翻转的左端点
ar[i][j] = ar[i - ][j];//al记录翻转的左端点
if(a[i] == b[j])
{
dp[i][j] = dp[i - ][j] + ;
if(l == j && !al[i][j])//如果当前的j就是b开始翻转的左端点,更新记录
al[i][j] = i;
if(r == j)//当前的j是b翻转的右端点,记录更新
ar[i][j] = i;
}
if(dp[i][j - ] > dp[i][j])//如果答案有更新就要更新答案
{
dp[i][j] = dp[i][j - ];
al[i][j] = al[i][j - ];
ar[i][j] = ar[i][j - ];
}
}
}
return dp[n][cnt];
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%d%s", &n, s + );
for(int i = ; i <= n; i++)
a[i] = s[i] - '';
cnt = ;
for(int i = ; i <= ; i++)
b[++cnt] = i;
ansl = ansr = l = r = ;
ans = solve();
for(int i = ; i <= ; i++) //枚举翻转b数组的每一段
{
for(int j = i + ; j <= ; j++)
{
cnt = ;
for(int k = ; k <= i; k++)
b[++cnt] = k;
l = cnt+;//左端点
for(int k = j; k >= i; k--)//只翻转i~j区间的数
b[++cnt] = k;
r = cnt;//右端点
for(int k = j; k <= ; k++)
b[++cnt] = k;
/* for(int i=1; i<=cnt; i++)
printf("%d ",b[i]);
printf("\n");*/
int tmp = solve();
if(ans < tmp && al[n][cnt] && ar[n][cnt])//不断更新答案
{
ans = tmp;
ansl = al[n][cnt], ansr = ar[n][cnt];
}
}
}
printf("%d %d %d\n", ans, ansl, ansr);
}
return ;
}
HDU 6357.Hills And Valleys-动态规划(区间翻转l,r找最长非递减子序列)的更多相关文章
- HDU 6357.Hills And Valleys-字符串非严格递增子序列(LIS最长非下降子序列)+动态规划(区间翻转l,r找最长非递减子序列),好题哇 (2018 Multi-University Training Contest 5 1008)
6357. Hills And Valleys 自己感觉这是个好题,应该是经典题目,所以半路选手补了这道字符串的动态规划题目. 题意就是给你一个串,翻转任意区间一次,求最长的非下降子序列. 一看题面写 ...
- HDU - 6357 Hills And Valleys(DP)
http://acm.hdu.edu.cn/showproblem.php?pid=6357 题意 给一个数值范围为0-9的a数组,可以选择翻转一个区间,问非严格最长上升子序列,以及翻转的区间. 分析 ...
- HDU 6357 Hills And Valleys
Hills And Valleys 题意:给你一个序列, 可以翻转一次区间 [l, r] 求最大 非递减的 序列长度. 题解:枚举翻转区间,然后匹配. 如果不翻转区间, 那么就相当于用b[] = {0 ...
- HDU 1025 Constructing Roads In JGShining's Kingdom[动态规划/nlogn求最长非递减子序列]
Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...
- HDU 5532 Almost Sorted Array (最长非递减子序列)
题目链接 Problem Description We are all familiar with sorting algorithms: quick sort, merge sort, heap s ...
- AtCoder Beginner Contest 116 C题 【题意:可以在任意区间【L,R】上加1,求通过最少加1次数得到题目给定的区间】】{思维好题}
C - Grand Garden In a flower bed, there are NN flowers, numbered 1,2,......,N1,2,......,N. Initially ...
- 【动态规划】【二分】【最长不下降子序列】洛谷 P1020 导弹拦截
最长不下降子序列的nlogn算法 见 http://www.cnblogs.com/mengxm-lincf/archive/2011/07/12/2104745.html 这题是最长不上升子序列,倒 ...
- HDU 3308 线段树求区间最长连续上升子序列长度
题意:两种操作,Q L R查询L - R 的最长连续上升子序列长度,U pos val 单点修改值 #include <bits/stdc++.h> #define N 100005 us ...
- HDU 3911 Black and White (线段树,区间翻转)
[题目地址] vjudge HDU [题目大意] 海滩上有一堆石头. 石头的颜色是白色或黑色. 小肥羊拥有魔术刷,她可以改变连续石的颜色,从黑变白,从白变黑. 小肥羊非常喜欢黑色,因此她想知道范围 ...
随机推荐
- SpringBoot23 分模块开发
1 开发环境说明 JDK:1.8 MAVEN:3.5 IDEA:2017.2.5 SpringBoot:2.0.3.RELEASE 2 创建SpringBoot项目 2.1 项目信息 2.2 添加项目 ...
- ubunt 14.04 Could not find CMAKE_ROOT !!! CMake has most likely not been installed correctly. Modul
CMake Error: Could not find CMAKE_ROOT !!! CMake has most likely not been installed correctly. Modul ...
- 基于 EntityFramework 的数据库主从读写分离架构(2)- 改进配置和添加事务支持
回到目录,完整代码请查看(https://github.com/cjw0511/NDF.Infrastructure)中的目录: src\ NDF.Data.EntityFramew ...
- ShopNc登录
- httpd和apache的区别
今天要配置集成服务器环境 apache + tomcat + php + jsp + mysql + sqlserver 去下载apache 发现有: apache_2.2.14-win32-x86 ...
- HDU 4803 Poor Warehouse Keeper(贪心)
题目链接 题意 :屏幕可以显示两个值,一个是数量x,一个是总价y.有两种操作,一种是加一次总价,变成x,1+y:一种是加一个数量,这要的话总价也会相应加上一个的价钱,变成x+1,y+y/x.总价显示的 ...
- ABP源码uml类图
陆陆续续学习ABP框架有一段时间了,阳光铭睿的入门教程和HK Zhang的源码分析文章对我的学习帮助都很大.之所以会花这么大工夫去学习ABP.看ABP的源代码,一是因为本人对于DDD也非常有兴趣,AB ...
- 转 XlsReadWriteII 的博文(自留参考)
如何使用XlsReadWriteII在Delphi中读取Excel文件 XLSReadWriteII v5.20.01a for Delphi XE5 x32下载地址: http://download ...
- .net 序列化 与反序列化 Serializable
序列化:序列化指的是 将对象 通过流的方式 保存为一个文件. 反序列化则是将该文件还原成 对象的过程. 序列化的作用:序列化可以跨语言跨平台 传输数据,将某一对象序列化成通用的文件格式在进行传输. 比 ...
- MVC5中 在更新 Microsoft.Aspnet.Identity 后编译器错误
环境:vs2013预览版chs,我试着创建vb.net web应用,从对话框中选择MVC和WebAPI.编译ok了.通过NuGet管理器更新了Microsoft.Aspnet.Identity.Cor ...