2018 Multi-University Training Contest 5: H. Hills And Valleys(DP)

本文探讨了一种通过翻转序列区间来最大化最长非降子序列长度的问题,并提供了一个具体的算法实现方案。该算法考虑了如何选取最优翻转区间以得到最长的非降子序列。

Hills And Valleys

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0
Special Judge

Problem Description

Tauren has an integer sequence A of length n (1-based). He wants you to invert an interval [l,r] (1≤l≤r≤n) of A (i.e. replace Al,Al+1,⋯,Ar with Ar,Ar−1,⋯,Al) to maximize the length of the longest non-decreasing subsequence of A. Find that maximal length and any inverting way to accomplish that mission.
A non-decreasing subsequence of A with length m could be represented as Ax1,Ax2,⋯,Axm with 1≤x1<x2<⋯<xm≤n and Ax1≤Ax2≤⋯≤Axm.

Input

The first line contains one integer T, indicating the number of test cases.
The following lines describe all the test cases. For each test case:
The first line contains one integer n.
The second line contains n integers A1,A2,⋯,An without any space.
1≤T≤100, 1≤n≤105, 0≤Ai≤9 (i=1,2,⋯,n).
It is guaranteed that the sum of n in all test cases does not exceed 2⋅105.

Output

For each test case, print three space-separated integers m,l and r in one line, where m indicates the maximal length and [l,r] indicates the relevant interval to invert.

Sample Input

2 9 864852302 9 203258468

Sample Output

5 1 8

6 1 2

 

题意:给你长度为n的字符串str[],只包含'0'~'9',你可以翻转一个区间,使得最后最长不下降子序列最长, 求出长度和你翻转的区间(输出任意一种即可)

假设没有翻转操作,答案就是 str[] 和 "0123456789" 的最长单字符可重公共子序列

单字符可重就是指的对于串 "0123456789" 中的每一个数字都可以匹配无数次再匹配下个数字,例如"001122223456789"等等

这道题可以翻转一次,那么可以暴力翻转区间中对答案有贡献的最小值和最大值[x, y]

假设对于当前其中一种情况[2, 3],只需要求出 str[] 和 "01232345678" 的最长单字符可重公共子序列即可

对于输出翻转区间记录路径就好了

 

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char str[100005];
int tx, ty, a[100005], pre[100005][15], dp[100005][15], now[15], x1[100005][15], x2[100005][15];
int main(void)
{
	int T, n, i, j, p, q, ans, cnt, ax, ay, Ans, c1, c2;
	scanf("%d", &T);
	while(T--)
	{
		tx = ty = 1;
		scanf("%d%s", &n, str+1);
		for(i=1;i<=n;i++)
			a[i] = str[i]-'0';
		for(i=1;i<=n;i++)
		{
			for(j=0;j<=9;j++)
				pre[i][j] = pre[i-1][j];
			pre[i][a[i]] = i;
		}
		Ans = ans = 0;
		for(p=0;p<=9;p++)
		{
			for(q=p;q<=9;q++)
			{
				cnt = 0;
				for(i=0;i<=p;i++)  now[++cnt] = i;
				for(i=q;i>=p;i--)  now[++cnt] = i;
				for(i=q;i<=9;i++)  now[++cnt] = i;
				for(i=1;i<=n;i++)
				{
					for(j=1;j<=cnt;j++)
					{
						if(a[i]==now[j])
						{
							dp[i][j] = dp[i-1][j-1]+1;
							x1[i][j] = i-1, x2[i][j] = j-1;
							if(dp[i-1][j]+1>dp[i][j])
							{
								dp[i][j] = dp[i-1][j]+1;
								x2[i][j] = j;
							}
						}
						else
						{
							dp[i][j] = dp[i-1][j];
							x1[i][j] = i-1, x2[i][j] = j;
							if(dp[i][j-1]>dp[i][j])
							{
								dp[i][j] = dp[i][j-1];
								x1[i][j] = i, x2[i][j] = j-1;
							}
						}
						if(dp[i][j]>=ans)
						{
							ans = dp[i][j];
							ax = i, ay = j;
						}
					}
				}
				if(ans>Ans)
				{
					Ans = ans;
					while(ax!=0)
					{
						if(ay==p+2 && x2[ax][ay]!=ay)
							tx = ax;
						if(ay==q+3 && x2[ax][ay]!=ay)
							ty = x1[ax][ay];
						c1 = x1[ax][ay];
						c2 = x2[ax][ay];
						ax = c1, ay = c2;
					}
				}
			}
		}
		if(ty==0)
			tx = ty = 1;
		printf("%d %d %d\n", ans, tx, ty);
	}
	return 0;
}
/*
1
2
10
*/

 

请你针对这部分修改,为避免样式改变 把样式代码也修改:.csw-h5-contest-racesSlots { padding: 12px 24px 0; .csw-opensource-h5-learningHeader { height: 24px; width: 100%; margin-bottom: 10px; display: flex; justify-content: space-between; align-items: center; .csw-opensource-h5-learningHeader-title { color: rgb(0, 0, 0); font-size: 18px; font-weight: 500; line-height: 20px; } .csw-opensource-h5-learningHeader-more { color: rgb(153, 153, 153); font-size: 12px; font-weight: 400; line-height: 16px; .csw-opensource-h5-learningHeader-moreText { padding-right: 4px; } } } .csw-h5-contest-racesSlots-content { position: relative; .adm-jumbo-tabs-header { border: 0; .adm-jumbo-tabs-tab-list { padding: 0; .adm-jumbo-tabs-tab-wrapper { flex: none; padding: 0; margin-right: 16px; .adm-jumbo-tabs-tab { font-weight: 500; color: rgb(102, 102, 102); padding: 0; } .adm-jumbo-tabs-tab-active { color: rgb(0, 65, 211); } } } } .adm-jumbo-tabs-content { padding: 16px 0 12px; } .csw-h5-contest-topSummitsCarousel { width: 100%; padding-bottom: 12px; .topSummitsList { display: flex !important; justify-content: space-between; .topSummitsItem { width: 48%; position: relative; img { width: 100%; aspect-ratio: 1.785; border-radius: 4px; } .mask { width: 100%; height: 100%; position: absolute; bottom: 0; border-radius: 4px; } .maskCourse { position: absolute; top: 6px; right: 6px; display: inline-block; height: 20px; text-align: center; line-height: 20px; padding: 2px 8px 2px 8px; border-radius: 53px; background: rgba(0, 65, 211, 0.7); color: rgb(255, 255, 255); font-size: 11px; font-weight: 400; line-height: 16px; } .topSummitsItemTitle { display: inline-block; margin-top: 8px; width: 100%; color: rgb(0, 0, 0); font-weight: 400; line-height: 24px; } .itemSubTitle { font-family: HarmonyHeiTi; color: #666666; font-size: 12px; font-weight: 400; line-height: 19px; } } } .slick-dots-bottom { bottom: 3px; z-index: 9; li { height: 2px !important; width: 6px !important; } button { width: 6px !important; background: #f2f2f2 !important; opacity: 1 !important; border-radius: 30px !important; height: 2px !important; } .slick-active { width: 12px !important; border-radius: 1px !important; button { background-color: rgb(0, 65, 211) !important; width: 12px !important; } } } } .csw-racesSlots-h5-more { color: rgb(153, 153, 153); font-size: 12px; font-weight: 400; line-height: 16px; position: absolute; right: 0; top: 17px; .anticon-right { margin-left: 3px; } } .moreDomCss { position: absolute; top: 4px; right: 0; color: rgb(153, 153, 153); font-size: 12px; font-weight: 400; line-height: 16px; .moreText { padding-right: 4px; } } } }
最新发布
11-21
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值