codeforces 1304D Shortest and Longest LIS

题目链接:http://codeforces.com/problemset/problem/1304/D

思路:

最短的LIS:n,n-1,n-2,...3,2,1

最长的LIS:1,2,3,...,n-2,n-1,n

我们可以再按照给定字符串的大小关系,对两种LIS进行区间排序即可。

#include <iostream>
#include <algorithm>
#include <map>
#include <functional>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;
#define ll long long
#define pb push_back
 
const int N = 2e5+10;
vector<int > vi;
ll a[N];
 
void show(int x){
    for(int i = 0; i < x; ++i) cout << a[i] << " ";
    cout << endl;
}
 
void solve(){
 
    int n;
    string str;
    cin >> n >> str;
    int _end = str.length();
    for(int i = 0; i < n; ++i) a[i] = n-i;
    int l = 0,r = 0;
    while(l < _end){
        if(str[l] == '>'){
            ++l; continue;
        }
        for(int i = l; i < _end; ++i){
            if(str[i] == '<') r = i;
            else break;
        }
        //cout << l << "   " << r << endl;
        sort(a+l,a+r+2);
 
        l = r+1;
    }
    for(int i = 0; i < n; ++i) cout << a[i] << " ";
    cout << endl;
    for(int i = 0; i < n; ++i) a[i] = i+1;
    l = r = 0;
    while(l < _end){
        if(str[l] == '<'){
            ++l; continue;
        }
        for(int i = l; i < _end; ++i){
            if(str[i] == '>') r = i;
            else break;
        }
        //cout << l << "   " << r << endl;
        sort(a+l,a+r+2,[](int a,int b){return a > b;});
        l = r+1;
    }
    for(int i = 0; i < n; ++i) cout << a[i] << " ";
    cout << endl;
}
 
int main(){
 
    int T;
    cin >> T;
    for(int i = 0; i < T; ++i){
        solve();
    }
 
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/SSummerZzz/p/12378436.html