Shortest and Longest LIS CodeForces - 1304D(思维)

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

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 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.
题意:根据给定的字符串构造n的序列,分别使得LIS最小和最大。
思路:如果使得LIS最小的话,那么整体要成递减趋势。除了那些必须成递增的之外,其余的都要是递减趋势。如果使得LIS最大的话,那么整体要成递增趋势,除了必须递减的之外,其余的都要是递增趋势。
其实思路很明确的一道题,但是没有整体考虑,结果wa了很久。参考了大佬的博客才明白,tcl。
代码如下:

#include<bits/stdc++.h>
using namespace std;

const int maxx=2e5+100;
char s[maxx];
int ans[maxx];
int n;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int n;
		cin>>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++) cout<<ans[i]<<" ";cout<<endl;
		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++) cout<<ans[i]<<" ";cout<<endl;
	}
    return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值