Codeforces Round #620 (Div. 2)_D. Shortest and Longest LIS(C++_最长上升子序列)

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 × 1 0 5 2\times10^5 .

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.

Process

This is the problem’s process:Dream Flying Eagle .
This is the LIS templet:动态规划-最长上升(下降)子序列的O(n logn)写法(基于二分)

Code

#include<bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<int > >a, b;
vector<char>c;
void solve()
{
    cin >> m;
    int dis = 2, num1 = 0, flag = 0, maxa = 0;
    c.resize(m - 1);
    for (int i = 0; i < m - 1; i++)
        cin >> c[i];
    a.push_back(vector<int>());//最短
    b.push_back(vector<int>());//最长
    a[0].push_back(1);
    b[0].push_back(1);
    for (int i = 0; i < c.size(); i++)
    {
        if (c[i] == '<')
        {
            if (num1 + 1 >= a.size())
                a.push_back(vector<int>());//最短
            b.push_back(vector<int>());//最长
            a[++num1].push_back(dis);
            b[b.size() - 1].push_back(dis++);
        }
        else
        {
            a[0].push_back(dis);//最短
            num1=0;
            b[b.size() - 1].push_back(dis++);//最长
        }
    }
    vector<int >d(m + 1);
    for (int i = a.size() - 1; i >= 0; i--)
        for (int j = 0; j < a[i].size(); j++)
            d[a[i][j]] = --dis;
    for (int i = 1; i <= m; i++)
        cout << d[i] << (i == m ? "\n" : " ");
    dis = m;
    for (int i = b.size() - 1; i >= 0; i--)
        for (int j = 0; j < b[i].size(); j++)
            d[b[i][j]] = dis--;
    for (int i = 1; i <= m; i++)
        cout << d[i] << (i == m ? "\n" : " ");
    a.clear();
    b.clear();
}
int main()
{
    cin >> n;
    while (n--)
        solve();
    return 0;
}
发布了229 篇原创文章 · 获赞 31 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_43510916/article/details/104362790