Codeforces Round #620 (Div. 2)(D. Shortest and Longest LIS)

Description
Gildong recently learned how to find the longest increasing subsequence (LIS) in O(n log n)O(n\ log\ n)O(n log n) time for a sequence of length nnn. He wants to test himself if he can implement it correctly, but he couldn’t find any online judges that would do it (even though there are actually many of them). So instead he’s going to make a quiz for you about making permutations of nnn distinct integers between 111 and nnn, inclusive, to test his code with your output.

The quiz is as follows.

Gildong provides a string of length n−1n−1n−1, consisting of characters ‘<’ and ‘>’ only. The iii-th (1-indexed) character is the comparison result between the iii-th element and the i+1i+1i+1-st element of the sequence. If the iii-th character of the string is ‘<’, then the iii-th element of the sequence is less than the i+1i+1i+1-st element. If the iii-th character of the string is ‘>’, then the iii-th element of the sequence is greater than the i+1i+1i+1-st element.

He wants you to find two possible sequences (not necessarily distinct) consisting of nnn distinct integers between 111 and nnn, inclusive, each satisfying the comparison results, where the length of the LIS of the first sequence is minimum possible, and the length of the LIS of the second sequence is maximum possible.

Input
Each test contains one or more test cases. The first line contains the number of test cases t(1≤t≤104)t (1≤t≤10^4)t(1≤t≤10 4).

Each test case contains exactly one line, consisting of an integer and a string consisting of characters ‘<’ and ‘>’ only. The integer is n(2≤n≤2⋅105)n (2≤n≤2⋅10^5)n(2≤n≤2⋅10 5 ), the length of the permutation you need to find. The string is the comparison results explained in the description. The length of the string is n−1n−1n−1.

It is guaranteed that the sum of all n in all test cases doesn’t exceed 2⋅1052⋅10^52⋅10 5
.

Output
For each test case, print two lines with nnn integers each. The first line is the sequence with the minimum length of the LIS, and the second line is the sequence with the maximum length of the LIS. If there are multiple answers, print any one of them. Each sequence should contain all integers between 111 and nnn, inclusive, and should satisfy the comparison results.

It can be shown that at least one answer always exists.

Example
input

3
3 <<
7 >><>><
5 >>><

output

1 2 3
1 2 3
5 4 3 7 2 1 6
4 3 1 7 5 2 6
4 3 2 1 5
5 4 2 1 3

Note
In the first case, 1 2 31\ 2\ 31 2 3 is the only possible answer.

In the second case, the shortest length of the LIS is 222, and the longest length of the LIS is 333. In the example of the maximum LIS sequence, ‘4’ ‘3’ ‘1’ ‘7’ ‘5’ ‘2’ ‘6’ can be one of the possible LIS.

题意

给你一个表示大小关系的序列,序列里只包含’<‘和’>’,它表示的是一个序列里相邻的数字的大小关系。题目让你找出符合这个大小关系同时有着最短的“最长上升子序列”的序列,和符合这个大小关系同时有着最长的“最长上升子序列”的序列。
————————————————
最暴力的方法就是构造了,但是要加上贪心的思想去构造;

那就是求最短的序列时尽可能把值较大的数往前放,求最长的序列时金可能把值较大的数往后放。基于这两个个准则分别找到符合题目所给大小关系的以组序列即是答案。

那么具体来说就是先让“表示最短的”序列要把数字大的往前放,先初始化为5 4 3 2 1,然后不断地找字符串里的 ’<‘ ,让一段只有 ’<’ 的区间里的数字对调来满足大小关系,一遍修改之后就是答案;反之同理。
————————————————

#include <bits/stdc++.h>
using namespace std;
int a[2*100005];
char s[2*100005];
int main()
{
	int t;
	cin >>t;
	while(t--)
	{
		int n;
		cin >>n;
		memset(s,0,sizeof s);
		for(int i=1;i<n;i++) cin >>s[i]; 
		int l=0,r=0;
		//找最短 
		for(int i=1;i<=n;i++) a[i]=n-i+1;
		for(int i=1;i<n;i++)
		{
			if(s[i]=='<'&&s[i-1]!='<') l=i;
			if(s[i]=='<'&&s[i+1]!='<') r=i+1;
			if(l!=0&&r!=0)
			{
				while(l<r)
				{
					swap(a[l],a[r]);
					l++,r--;
				}
				l=0,r=0;
			}
		}
		for(int i=1;i<=n;i++) cout <<a[i]<<" ";
		cout <<endl;
		//同样的方法找最长 
		for(int i=1;i<=n;i++) a[i]=i;
		for(int i=1;i<n;i++)
		{
			if(s[i]=='>'&&s[i-1]!='>') l=i;
			if(s[i]=='>'&&s[i+1]!='>') r=i+1;
			if(l!=0&&r!=0)
			{
				while(l<r)
				{
					swap(a[l],a[r]);
					l++,r--;
				}
				l=0,r=0;
			}
		}
		for(int i=1;i<=n;i++) cout <<a[i]<<" ";
		cout <<endl;
	}
}
发布了201 篇原创文章 · 获赞 38 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_43872728/article/details/104367788