단봉 시퀀스의 이상 1002 학교 두 번째 타이틀 미용 (LIS + 단조로운 스택)의 2019 끊기 전기장

주제 링크

생각

첫째, \ (A \) 각 실행의 플러스, 마이너스 측 \합니다 (LIS \) 각각의 전방 절반의 기록 위치 \합니다 (LIS가 \) 위치에 배치한다 (\ ANS1 [I] \) , 후방 절반의 위치 \ (ANS2 [I] \) .

순서의 최소 사전 프로그램, 우리는 다음 첫 번째 피크, 앞으로 통과를 찾을 수 있습니다. 에서 \ (내가 \) 가있는 경우,이 위치에 \ (LIS \) 위치에 넣어 \ (POS \) 우리는 현재의 장소를 보면, \ (POS + 1 \) 값은 큰보다 큰 그럼에 있도록 전방의 소정 비율의 위치를 도시 \ (POS \) 이 위치에서 더 나은 (AS 전적으로 이하, \ ([1, 난] \ ) 채워야 \ ([1, POS-1 ] \) ), 우리는 현재 스택 단조로운 넣어 덜 사용합니다 \ (POS \) 모든 값 \ (팝 \) 멀리. 각 위치에 대한 직접 우리의 후반을 커버 할 수 반복적으로 넣어.

우리에게 가장 큰 사전 순서는 상반기 단조로운 스택 유지 보수 하반기 및 사전 편찬 정반대을 충당하기 위해 반복된다.

코드는 다음을 달성하기 위해

#include <set>
#include <map>
#include <deque>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long LL;
typedef pair<LL, LL> pLL;
typedef pair<LL, int> pLi;
typedef pair<int, LL> pil;;
typedef pair<int, int> pii;
typedef unsigned long long uLL;

#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) x&(-x)
#define name2str(name) (#name)
#define bug printf("*********\n")
#define debug(x) cout<<#x"=["<<x<<"]" <<endl
#define FIN freopen("/home/dillonh/CLionProjects/Dillonh/in.txt","r",stdin)
#define IO ios::sync_with_stdio(false),cin.tie(0)

const double eps = 1e-8;
const int mod = 998244353;
const int maxn = 3e5 + 7;
const double pi = acos(-1);
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;

int n;
stack<int> st;
vector<int> vec, v;
int pos[maxn];
int a[maxn], dp[maxn], ans1[maxn], ans2[maxn];

int main() {
#ifndef ONLINE_JUDGE
    FIN;
#endif
    while(~scanf("%d", &n)) {
        memset(dp, inf, sizeof(dp));
        dp[0] = 0;
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &a[i]);
            ans1[i] = ans2[i] = 0;
            pos[i] = inf;
            ans1[i] = lower_bound(dp + 1, dp + n + 1, a[i]) - dp;
            dp[ans1[i]] = a[i];
        }
        memset(dp, inf, sizeof(dp));
        dp[0] = 0;
        for(int i = n; i >= 1; --i) {
            ans2[i] = lower_bound(dp + 1, dp + n + 1, a[i]) - dp;
            dp[ans2[i]] = a[i];
        }
        while(!st.empty()) st.pop();
        int idx = 1, mx = ans1[1] + ans2[1];
        for(int i = 2; i <= n; ++i) {
            if(ans1[i] + ans2[i] > mx) {
                idx = i;
                mx = ans1[i] + ans2[i];
            }
        }
        pos[ans1[idx]] = a[idx];
        for(int i = idx - 1; i >= 1; --i) {
            if(ans1[i] >= ans1[idx]) continue;
            if(pos[ans1[i]+1] <= a[i]) continue;
            while(!st.empty() && ans1[i] >= ans1[st.top()]) {
                pos[ans1[st.top()]] = inf;
                st.pop();
            }
            st.push(i);
            pos[ans1[i]] = a[i];
        }
        vec.clear();
        while(!st.empty()) {
            vec.push_back(st.top());
            st.pop();
        }
        vec.push_back(idx);
        int las = idx;
        for(int i = idx + 1; i <= n; ++i) {
            if(ans2[i] == ans2[las] - 1 && a[i] < a[las]) {
                vec.push_back(i);
                las = i;
            }
        }
        int flag = 0;
        for(auto x:vec) {
            if(flag) printf(" ");
            flag = 1;
            printf("%d", x);
        }
        printf("\n");

        vec.clear();
        idx = 1, mx = ans1[1] + ans2[1];
        for(int i = 2; i <= n; ++i) {
            if(ans1[i] + ans2[i] >= mx) {
                idx = i;
                mx = ans1[i] + ans2[i];
            }
        }
        las = idx;
        vec.push_back(idx);
        for(int i = idx - 1; i >= 1; --i) {
            if(ans1[i] == ans1[las] - 1 && a[i] < a[las]) {
                vec.push_back(i);
                las = i;
            }
        }
        reverse(vec.begin(), vec.end());
        for(int i = 1; i<= n; ++i) pos[i] = inf;
        pos[ans2[idx]] = a[idx];
        while(!st.empty()) st.pop();
        for(int i = idx + 1; i <= n; ++i) {
            if(ans2[i] > ans2[idx]) continue;
            if(a[i] >= pos[ans2[i]+1]) continue;
            while(!st.empty() && ans2[i] >= ans2[st.top()]) {
                pos[ans2[st.top()]] = inf;
                st.pop();
            }
            st.push(i);
            pos[ans2[st.top()]] = a[i];
        }
        v.clear();
        while(!st.empty()) {
            v.push_back(st.top());
            st.pop();
        }
        reverse(v.begin(), v.end());
        flag = 0;
        for(auto x:vec) {
            if(flag) printf(" ");
            flag = 1;
            printf("%d", x);
        }
        for(auto x:v) {
            if(flag) printf(" ");
            flag = 1;
            printf("%d", x);
        }
        printf("\n");
    }
    return 0;
}

추천

출처www.cnblogs.com/Dillonh/p/11240083.html