CodeForces - 1304D Shortest and Longest LIS(构造+贪心)

题目链接:点击查看

题目大意:题目给出 n - 1 个大小关系,分别代表一个长度为 n 的数列各个位置的相对大小,现在需要我们用两个符合相对大小关系的 1 ~ n 的一个排列,且该排列的最长不下降子序列分别最小和最大

题目分析:读完题后不难想到,如果我们想让最长不下降子序列最长的话,那么整个序列必然是呈整体上升的趋势,最小的话相应的是整个序列呈下降趋势,因为多了相对大小这个约束,我们可以使得符号相同的连续序列分成不同的区块,每个区块只要保持整体上升的趋势就行,换句话说,必须保证后一个区块的最小值比前一个区块的最大值还要大才行,这样我们善于运用一下reverse函数就可以轻松实现了,比赛的时候因为B题读错题了慌了神,导致这个简单题没有出,坐等掉分

代码:

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<unordered_map>
using namespace std;
    
typedef long long LL;
   
typedef unsigned long long ull;
    
const int inf=0x3f3f3f3f;
    
const int N=2e5+100;

char s[N];

int ans[N];

int main()
{
//#ifndef ONLINE_JUDGE
//  freopen("input.txt","r",stdin);
//    freopen("output.txt","w",stdout);
//#endif
//  ios::sync_with_stdio(false);
	int w;
	cin>>w;
	while(w--)
	{
		int n;
		scanf("%d%s",&n,s+1);
		for(int i=1;i<=n;i++)
			ans[i]=n-i+1;
		for(int i=1;i<=n-1;)//整体下降 
		{
			int j=i;
			while(s[j]=='<')
				j++;
			reverse(ans+i,ans+j+1);
			i=j+1;
		}
		for(int i=1;i<=n;i++)
			printf("%d ",ans[i]);
		printf("\n");
		for(int i=1;i<=n;i++)
			ans[i]=i;
		for(int i=1;i<=n-1;)//整体上升 
		{
			int j=i;
			while(s[j]=='>')
				j++;
			reverse(ans+i,ans+j+1);
			i=j+1;
		}
		for(int i=1;i<=n;i++)
			printf("%d ",ans[i]);
		printf("\n");
	}
     
     
     
     
       
       
       
       
       
       
       
    return 0;
}
发布了646 篇原创文章 · 获赞 20 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_45458915/article/details/104337105