[codeforces 1304D] Shortest and Longest LIS 从极端结果出发进行演化

Codeforces Round #620 (Div. 2)

[codeforces 1304D] Shortest and Longest LIS   从极端结果出发进行演化

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址https://codeforces.ml/contest/1304/problem/D

Problem Lang Verdict Time Memory
D - Shortest and Longest LIS GNU C++11 Accepted 109 ms 800 KB

算法过程如下

//排列组合,之后再选,根本就用不想,天文数字

从极端结果出发进行演化
n=5
5 4 3 2 1  min=0
1 2 3 4 5  max=5
从极端情况出发,处理出满足约束条件的序列。
>>>< min时,只需统计<   5 4 3 (1 2)
>>>< max时,只需统计>   (4 3 2 1) 5

n=7
1 2 3 4 5 6 7 min=0
7 6 5 4 3 2 1 max=7
从极端情况出发,处理出满足约束条件的序列。
>><>><  min时,只需统计<  7 6 (4 5) 3 (1 2)
>><>><  max时,只需统计>  (3 2 1) (6 5 4) 7

独立思考后的AC代码如下

#include <stdio.h>
#define maxn 200010
int a[maxn],n;
char s[maxn];
void solve_min(){
	int d,i,j,cnt=0,b,e;
	for(i=1;i<=n;i++)a[i]=n-i+1;//min
		for(i=1;i<=n;i++)//s[n]=='\0';
			if(s[i]=='<'){
				cnt++;
				if(cnt==1)b=i;
			}else//s[i]=='>'
				if(cnt){
					e=i-1+1;
					for(j=b;j<=b+(e-b)/2;j++)d=a[j],a[j]=a[b+e-j],a[b+e-j]=d;//将顺序颠倒
					cnt=0;
				}
	printf("%d",a[1]);
	for(i=2;i<=n;i++)printf(" %d",a[i]);
	printf("\n");
}
void solve_max(){
	int d,i,j,cnt=0,b,e;
	for(i=1;i<=n;i++)a[i]=i;//max
		for(i=1;i<=n;i++)//s[n]=='\0';
			if(s[i]=='>'){
				cnt++;
				if(cnt==1)b=i;
			}else//s[i]=='<'
				if(cnt){
					e=i-1+1;
					for(j=b;j<=b+(e-b)/2;j++)d=a[j],a[j]=a[b+e-j],a[b+e-j]=d;//将顺序颠倒
					cnt=0;
				}
	printf("%d",a[1]);
	for(i=2;i<=n;i++)printf(" %d",a[i]);
	printf("\n");
}
int main(){
	int t,i;
	scanf("%d",&t);
	while(t--){
		scanf("%d%s",&n,s+1);
		solve_min();
		solve_max();
	}
	return 0;
}
发布了537 篇原创文章 · 获赞 529 · 访问量 44万+

猜你喜欢

转载自blog.csdn.net/mrcrack/article/details/104347145