D. Shortest and Longest LIS -------------------- thinking (construction + greedy)

Gildong recently learned how to find the longest increasing subsequence (LIS) in O(nlogn) time for a sequence of length n. 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 n distinct integers between 1 and n, inclusive, to test his code with your output.

The quiz is as follows.

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

He wants you to find two possible sequences (not necessarily distinct) consisting of n distinct integers between 1 and n, 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).

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), 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−1.

It is guaranteed that the sum of all n in all test cases doesn’t exceed 2⋅105.

Output
For each test case, print two lines with n 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 1 and n, inclusive, and should satisfy the comparison results.

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

Example

inputCopy
3
3 <<
7 >><>><
5 >>><
outputCopy
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 3 is the only possible answer.

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

Meaning of the questions:
to give you a just include the <> characters.
'<' Indicates A [I] <A [I +. 1]
'>' represents a [i]> a [i + 1]
let you build a rising sequence and a sequence of shortest longest sequence rising sequence
parsing :

For the shortest rising sequences, then we must have the maximum number of greedy on the front
for the longest rising sequence, then we must have the minimum number of greedy on the front

For example:
7 >> <>> <
respect to the shortest sequences if they increase the number of '<' the first empty location, and the like '<' traversed back again filled number
because the maximum discharge greedy
rise to the longest sequence If you encounter '>' number in this position to empty, etc. '>' traversed back again to fill the number
because greedy put minimum

#include<bits/stdc++.h>
using namespace std;
const int N=2e5+1000;
int t,n;
int a[N];
int b[N];
string s;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>s;
		int m=n;
		for(int i=0;i<n;i++)
		{
			int len=1;
			while(i<s.size()&&s[i]=='<') //先把'<'遍历完 再回头去填数 满足贪心填最大
			{
				len++;
				i++;
			} 
			for(int j=i;j>=i-len+1;j--)
			{
				a[j]=m;
				m--;
			}
		}
		for(int i=0;i<n;i++) cout<<a[i]<<" ";
		cout<<endl;
		m=1;
		for(int i=0;i<n;i++)
		{
			int len=1;
			while(i<s.size()&&s[i]=='>')//先把'>'遍历完 再回头去填数 满足贪心填最小
			{
				len++;
				i++;
			} 
			for(int j=i;j>=i-len+1;j--)
			{
				b[j]=m;
				m++;
			}
		}
		for(int i=0;i<n;i++) cout<<b[i]<<" ";
		cout<<endl;
	}
}
Published 412 original articles · won praise 8 · views 8835

Guess you like

Origin blog.csdn.net/qq_43690454/article/details/104359446