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
*/

猜你喜欢

转载自blog.csdn.net/Jaihk662/article/details/81462203
今日推荐