https://codeforces.com/problemset/problem/1304/D
Gildong recently learned how to find the longest increasing subsequence (LIS) in O(nlogn)O(nlogn) time for a sequence of length nn. 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 nn distinct integers between 11 and nn, inclusive, to test his code with your output.
题意就是按照给的比较符号写出一个数列来,要求第一行的上升子序列最短,第二行的上升子序列最长
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
参考别人写的其实,很棒👍🏻
我一开始只是很普通的思路,不仅用了额外的数组来标记了1-n的值,而且就是单纯的判断各个符号的个数来输出,繁琐不说,考虑也很不全面,改起来也很麻烦了
这个代码,我喜欢的点在不用单独的数组来标记需要输出的数,而是一边判断一边输出,思路简练,一步抓住关键点。
思路: 第一行的上升子序列最短,就是让大数尽量去前面,以’>‘为主,同理,第二行就是尽量让小数去前面,以’<'为主。
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int t,n;
int main()
{
string s;
cin>>t;
while(t--)
{
cin>>n;
cin>>s;
int q=0;
s[n-1]='>';//多标记一个主要符号
for(int i=0;i<n;i++)
{
if(s[i]=='>') //只考虑大于号即可,小于号时候的数在下面循环里就可以输出
{
for(int j=n-i;j<=n-q;j++)上一个大于号到这个大于号这个区段里面需要的数字
cout<<j<<" ";
q=i+1;//标记当前输出过的最小值
}
}
cout<<endl;
//下段同理,尽量让大数去后面即可
s[n-1]='<';
q=0;
for(int i=0;i<n;i++)
{
if(s[i]=='<')
{
for(int j=i+1;j>q;j--)
cout<<j<<" ";
q=i+1;
}
}
cout<<endl;
}
return 0;
}