結果

問題 No.930 数列圧縮
ユーザー ShibuyapShibuyap
提出日時 2019-11-22 22:45:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 84 ms / 2,000 ms
コード長 3,531 bytes
コンパイル時間 1,988 ms
コンパイル使用メモリ 167,608 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-19 12:08:40
合計ジャッジ時間 4,320 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 3 ms
6,940 KB
testcase_08 AC 58 ms
6,940 KB
testcase_09 AC 65 ms
6,940 KB
testcase_10 AC 51 ms
6,940 KB
testcase_11 AC 57 ms
6,940 KB
testcase_12 AC 67 ms
6,940 KB
testcase_13 AC 37 ms
6,940 KB
testcase_14 AC 48 ms
6,940 KB
testcase_15 AC 73 ms
6,944 KB
testcase_16 AC 73 ms
6,944 KB
testcase_17 AC 81 ms
6,944 KB
testcase_18 AC 84 ms
6,940 KB
testcase_19 AC 81 ms
6,940 KB
testcase_20 AC 84 ms
6,940 KB
testcase_21 AC 82 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 71 ms
6,940 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 AC 2 ms
6,940 KB
testcase_26 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); ++i)
#define rrep(i,n) for(int i = 1; i <= (n); ++i)
#define drep(i,n) for(int i = (n)-1; i >= 0; --i)
#define srep(i,s,t) for (int i = s; i < t; ++i)
using namespace std;
typedef pair<int,int> P;
typedef long long int ll;
#define dame { puts("-1"); return 0;}
#define yn {puts("Yes");}else{puts("No");}

const int MAX_N = 1 << 17;

// セグメント木を持つグローバル配列
int n, dat[2 * MAX_N - 1];

// 初期化
void init(int n_){
    // 簡単のため要素数を2のべき乗に
    n = 1;
    while(n < n_)n *= 2;

    // 全ての値をINT_MAXに
    for(int i = 0; i < 2 * n - 1; i++)dat[i] = INT_MAX;
}

// k番目の値(0-indexed)をaに変更
void update(int k, int a){
    // 葉の接点
    k += n - 1;
    dat[k] = a;
    // 登りながら更新
    while(k > 0){
        k = (k - 1) / 2;
        dat[k] = min(dat[k * 2 + 1], dat[k * 2 + 2]);
    }
}

// [a,b)の最小値を求める
// 後ろのほうの引数は、計算の簡単のための引数。
// kは節点の番号、l, rはその節点が[l, r)に対応づいていることを表す。
// したがって、外からはquery(a, b, 0, 0, n)として呼ぶ。
int query(int a, int b, int k, int l, int r){
    // [a, b)と[l, r)が交差しなければ、INT_MAX
    if (r <= a || b <= l)return INT_MAX;

    // [a, b)が[l, r)を完全に含んでいれば、この節点の値
    if (a <= l && r <= b)return dat[k];
    else {
        // そうでなければ、2つの子の最小値
        int vl = query(a, b, k * 2 + 1, l, (l + r) / 2);
        int vr = query(a, b, k * 2 + 2, (l + r) / 2, r);
        return min(vl, vr);
    }
}


int main(){
    int n_; cin >> n_;
    n = n_;

    init(n);

    int a[n_];
    rep(i,n_)cin >> a[i];
    int f[n_+1] = {};
    rep(i,n_)f[a[i]] = i;
    int ma[n_ + 1] = {};
    srep(i,1,n_+1){
        int tmp = query(0, f[i] + 1, 0, 0, n);
        if(tmp > 1001001001)tmp = 0;
        ma[i] = -tmp;
        update(f[i], -i);
    }
    // cout << "OK1" << endl;
    init(n);
    int mi[n_ + 1] = {};
    drep(i,n_+1){
        if(i == 0)break;
        int tmp = query(f[i], n, 0, 0, n);
        if(tmp > 1001001001)tmp = 0;
        mi[i] = tmp;
        update(f[i], i);
    }
    // cout << "OK2" << endl;
    int y = n_;
    while(y > 0){
        if(ma[y] != 0){
            y--;
        }else{
            break;
        }
    }
    int x = 1;
    while(x < n_){
        if(mi[x] != 0){
            x++;
        }else{
            break;
        }
    }
    // cout << "OK3" << endl;
    /*
    srep(i,1,n_ + 1)cout << ma[i] << ' ';
    cout << endl;
    srep(i,1,n_ + 1)cout << mi[i] << ' ';
    cout << endl;
    */
    srep(i,1,n_){
        if(i <= x - 1 && i + 1 > y){
            if(ma[i+1] == i && mi[i] == i+1){
                cout << "Yes" << endl;
                vector<int> v;
                v.push_back(i);
                srep(j, i+2,n_ + 1 ){
                    //if(j < i + 2)break;
                    v.push_back(j);
                }
                drep(j,i){
                    if(j == 0)break;
                    v.push_back(j);
                }
                
                drep(j,v.size()){
                    cout << v[j];
                    if(j == 0){
                        cout << endl;
                    }else{
                        cout << ' ';
                    }
                }
                return 0;
            }
        }
    }


    cout << "No" << endl;
    return 0;
}
0