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 [题目大意] 海滩上有一堆石头. 石头的颜色是白色或黑色. 小肥羊拥有魔术刷,她可以改变连续石的颜色,从黑变白,从白变黑. 小肥羊非常喜欢黑色,因此她想知道范围 ...
随机推荐
- jmap, jhat命令
jmap命令有下面几种常用的用法 jmap [pid] jmap -histo:live [pid] >a.log jmap -dump:live,format=b,file=xxx.xxx [ ...
- dubbo参数调优
dubbo中配置优先级规律:方法级配置优先级高于接口级,consumer的优先级高于provider. 详细: consumer的method配置 > provider的method配置 c ...
- 125. Valid Palindrome判断有效的有符号的回文串
[抄题]: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...
- 23-单词数(HDU2070)
单词数 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 屌爆的xamarin,一人单挑google/apple/windows
一个IDE就把3大手机平台全包了: android:自带模拟器xamarin player,速度堪比genymotion. ios:需要一台mac机辅助,一旦配好后可全程脱离,连ios模拟器都给镜像到 ...
- Luogu 3537 [POI2012]SZA-Cloakroom
背包. 首先考虑将所有询问离线按照$m$从小到大排序,然后把所有物品按照$a$从小到大排序,对于每一个询问不断加入物品. 设$f_i$表示在组成容量为$i$的背包的所有方案中$b$最小的一个物品的最大 ...
- 利用URL重写实现Session跟踪
Servlet规范中引入了一种补充的会话管理机制,它允许不支持Cookie的浏览器也可以与WEB服务器保持连续的会话.这种补充机制要求在响应消息的实体内容中必须包含下一次请求的超链接,并将会话标识号作 ...
- hdu4643 GSM
#include<stdio.h> #include<math.h> #define Max 55 #define eps 1e-8 int n,m; struct Point ...
- 实践作业3:白盒测试----开始测试用例的设计DAY3
白盒测试与黑盒测试很大不同之处在于白盒测试必须读相应代码,对代码有一定了解的情况下针对代码的逻辑进行测试用例的设计.白盒测试有六种覆盖标准:语句覆盖.判定覆盖.条件覆盖.判定/条件覆盖.条件组合覆盖和 ...
- Robot Framework - 基础关键字 BuiltIn 库(一)
今天给大家分享的是Robot Framework 机器人框架中 BuiltIn 基础库的使用...BuiltIn 库里面提供了很多基础方法助力于我们在自动化测试领域中做的更好!——本系列教程是教会大家 ...