D. Shortest and Longest LIS 构造

https://codeforces.com/contest/1304/problem/D
给定 n − 1 n-1 n1个大于或者小于号,表示元素之间的对应关系,现在要构造两种由 [ 1 , n ] [1,n] [1,n]中元素构成的序列,分别是最长递增子序列最短和最长

  • 直觉应该是如果要最短,那就把大的数放在前面,因为这可以使得前面对后面不做贡献;如果要最长,那就把小的数放在前面但是需要保证不等号方向不错,所以第一种构造可以从大于号开始划分,从最大的数开始填充;第二种构造从小于号开始划分,从最小的数开始填充,使用双指针记录位置
#include <bits/stdc++.h>

using namespace std;

int main(){
    
    
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--){
    
    
        int n;
        string s;
        cin >> n >> s;
        int last = 0;
        int now = n;
        vector<int> ans(n);
        for(int i=0;i<n;i++){
    
    
            if(i == n - 1 || s[i] == '>'){
    
    
                for(int j=i;j>=last;j--){
    
    
                    ans[j] = now--;
                }
                last = i + 1;
            }
        }
        for(int i=0;i<n;i++) cout << ans[i] << (i == n - 1 ? '\n' : ' ');
        last = 0;
        now = 1;
        for(int i=0;i<n;i++){
    
    
            if(i == n - 1 || s[i] == '<'){
    
    
                for(int j=i;j>=last;j--){
    
    
                    ans[j] = now++;
                }
                last = i + 1;
            }
        }
        for(int i=0;i<n;i++) cout << ans[i] << (i == n - 1 ? '\n' : ' ');
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/roadtohacker/article/details/121026737