当前位置:网站首页>【HDU6357】Hills And Valleys(DP)
【HDU6357】Hills And Valleys(DP)
2022-06-11 07:23:00 【CaptainHarryChen】
题意
给定一个数组A[],元素均在[0,9],可以翻转一段区间,求最长不下降子序列,并输出翻转区间
题解
很巧妙的DP
因为值域很小,我们可以枚举需要翻转的区间值出一个数组B[],规划好我们要求得的序列的大致趋向。
如我们翻转的区间的值为[7,3],则构建B数组为B[]={0,1,2,3,7,6,5,4,3,7,8,9},dp时按照B数组中的元素,进行转移。
定义dp状态:dp[i][j]为匹配到A串的第i位,B串的第j位,最长不下降子序列的长度:
转移为dp[i][j] = min ( dp[i][j-1], dp[i-1][j]+(A[i]==B[j]) )
从dp[i][j-1]转移是为了保证j越大,dp[i][j]也越大,使得当i固定,dp[i][j]保持不下降
从dp[i-1][j]转移即在A串中添加一位A[i],如果A[i]值在匹配的B串位置之后,则最长不下降序列可增加一位,因为我们已经保证dp[i][j]>=dp[i][j-1],所以只用判断A[i]==B[j]即可。
因为需要输出翻转方案,我们用path[i][j]数组记录dp转移路径,dp完成后,利用path恢复我们从B中选出来的元素(作为最长不下降子序列的元素),存在数组C[i]里,然后判断从哪一位开始翻转即可。
注意:如果没有翻转,需把翻转区间长度设为1(自己跟自己翻转)
代码
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=100005,MAXL=33;
int N,M;
int A[MAXN],B[MAXL];
int dp[MAXN][MAXL];
pair<int,int> path[MAXN][MAXL],C[MAXN];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&N);
for(int i=1;i<=N;i++)
scanf("%1d",&A[i]);
int ans=0,ansl,ansr;
for(int x=0;x<10;x++)
for(int y=x;y<10;y++)
{
M=0;
int l,r;
for(int i=0;i<=x;i++)
B[++M]=i;
l=M+1;
for(int i=y;i>=x;i--)
B[++M]=i;
r=M;
for(int i=y;i<10;i++)
B[++M]=i;
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++)
{
if(dp[i][j-1]<dp[i-1][j]+(A[i]==B[j]))
{
dp[i][j]=dp[i-1][j]+(A[i]==B[j]);
path[i][j]=make_pair(i-1,j);
}
else
{
dp[i][j]=dp[i][j-1];
path[i][j]=make_pair(i,j-1);
}
}
if(dp[N][M]>ans)
{
ans=dp[N][M];
int p=N,q=M,top=ans;
ansl=-1,ansr=-1;
pair<int,int> t;
while(p&&q)
{
t=path[p][q];
if(dp[t.first][t.second]+1==dp[p][q]&&A[p]==B[q])
C[top--]=make_pair(p,q);
p=t.first;
q=t.second;
}
int k;
for(k=1;k<=ans&&C[k].second<l;k++);
ansl=C[k].first;
for(;k<=ans&&C[k].second<=r;k++);
ansr=C[k-1].first;
if(ansl>ansr)
ansl=ansr=1;
}
}
//printf("%d\n",ans);
printf("%d %d %d\n",ans,ansl,ansr);
}
return 0;
}边栏推荐
- Interview question 17.08 Circus tower
- Janus feature草稿
- Shangtang technology has actively resumed work and will vigorously invest in the capacity and deployment of the digital sentry
- 10 advanced concepts that must be understood in learning SQL
- C+tinycthread implementation thread
- Ffmpe a small demo to understand 80% of common APIs
- Education expert wangzhongze solves students' problems with one move
- Senior openstacker - Bloomberg, vexxhost upgraded to the Gold member of openinfra Foundation
- Multi thread review summary parsing volatile keyword
- nosqlzoo刷题-1
猜你喜欢

Building a full-featured NAS server with raspberry pie (06): built-in file synchronization tool for penetration

C memory alignment

Decimal to binary

Modular notes

【Oracle 数据库】奶妈式教程day03 排序查询

Education expert Mr. wangzhongze: family education focuses on self growth

CMAP of Matplotlib

CRMEB/V4.4标准版打通版商城源码小程序公众号H5+App商城源码

如果要存 IP 地址,用什么数据类型比较好?99%人都会答错!
![[STL source code analysis] summary notes (10): hashtable exploration](/img/31/a77ac380dbd0f85957bd1df1b906f5.jpg)
[STL source code analysis] summary notes (10): hashtable exploration
随机推荐
1734. arrangement after decoding XOR
10 advanced concepts that must be understood in learning SQL
Concurrent tool class
Interview question 02.06 Palindrome linked list
Senior openstacker - Bloomberg, vexxhost upgraded to gold member of openinfra Foundation
P1390 sum of common divisors (Mobius inversion)
pycharm出现error.DeprecatedEnv: Env FrozenLake-v0 not found (valid versions include [‘FrozenLake-v1‘])
Leetcode-104. Maximum Depth of Binary Tree
JVM learning record (VII) -- class loading process and parental delegation model
一、SQLServer2008安装(带密码)、创建数据库、C#窗体项目测试
2022年熔化焊接与热切割考试练习题及答案
Atomicinteger atomic operation class
Regular Expression Matching
多线程复习总结之解析Volatile关键字
【CF#388 (Div. 2)】A. Bachgold Problem
Seata的几种事务模式
Mistakes in Niuke JS exercise
Notes on learning Es5 and ES6
C language to write a calculator MVC (very interesting code architecture callback and constructor mode and the use of pointer functions)
big. Js-- use / instance