Codeforces 1304D. Shortest and Longest LIS

根据题目,我们可以找最短的LIS和最长的LIS,找最短LIS时,可以将每一个increase序列分成一组,从左到右将最大的还未选择的数字填写进去,不同组之间一定不会存在s[i]<s[j]的情况,保证满足题意,找最长LIS,可以找补集,将每个decrease序列分成一组,找到后取反即可

#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-x))
typedef long long LL;

vector<int> solve(int n, string s) {
    vector<int> ans(n);
    int cur = 0, i, x = n;
    while(cur < n) {
        i = cur;
        while(i < n && s[i] == '<') i++;
        for(int j = i; j >= cur; --j) ans[j] = x--;
        cur = i+1;
    }
    
    return ans;
}

void run_case() {
    string s;
    int n;
    cin >> n >> s;
    vector<int> a = solve(n, s);
    for(auto &c : s)
        c ^= '>'^'<';
    vector<int> b = solve(n, s);
    for(auto i : a) cout << i << " ";
    cout << "\n";
    for(auto i : b) cout << n-i+1 << " ";
    cout << "\n";
}
 
int main() {
    ios::sync_with_stdio(false), cin.tie(0);
    //cout.setf(ios_base::showpoint);cout.precision(10);
    int t; cin >> t;
    while(t--)
    run_case();
    cout.flush();
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/GRedComeT/p/12316647.html