Codeforces Round #620 (Div. 2) D. Shortest and Longest LIS(最长、最短LIS)

智商压制篇:“构造,又是构造!”链接:http://codeforces.com/contest/1304/problem/D

题意:输出两行,每行数字范围是1~n,第一行最短LIS,第二行最长LIS。

题解:最长、最短LIS相同情况只有一种:“全是<” 或者 “全是>”。

其他的只要有一个符号不同,就会有很多种组合方式。

每一次符号改变都是一次转折点,围绕这一点进行分块!

最短LIS:尽可能把最大的数字往最前放,

最长LIS:尽可能把最小的数字往最前放。

#include<iostream>
#include <string>
#include <string.h>
using namespace std;
int n;
string s;
int a[200005],b[200005];
void solve()
{
	int top=0,num=n;
	//最短LIS 
	for(int i=0;i<n;i++)
	{
		if(i==n-1||s[i]=='>')
		{					
			for(int j=i;j>=top;j--)
			a[j]=num--; 
			top=i+1;	
		}
	}
	top=0,num=1;
	//最长LIS 
	for(int i=0;i<n;i++)
	{
		if(i==n-1||s[i]=='<')
		{
			for(int j=i;j>=top;j--)
			b[j]=num++;
			top=i+1;
		}
	}
} 
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>n>>s;
		solve();
		for(int i=0;i<n;i++) 
		printf("%d ",a[i]);
		cout<<endl;
		
		for(int i=0;i<n;i++) 
		printf("%d ",b[i]);
		cout<<endl;
	}
	return 0;
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值