結果

問題 No.127 門松もどき
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-10-21 19:36:34
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,053 ms / 5,000 ms
コード長 3,786 bytes
コンパイル時間 3,648 ms
コンパイル使用メモリ 175,420 KB
実行使用メモリ 195,580 KB
最終ジャッジ日時 2023-09-28 14:19:39
合計ジャッジ時間 23,799 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2,053 ms
195,580 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 94 ms
16,444 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 1,027 ms
142,768 KB
testcase_13 AC 1,351 ms
164,768 KB
testcase_14 AC 1,309 ms
157,636 KB
testcase_15 AC 1,695 ms
185,192 KB
testcase_16 AC 1,180 ms
155,212 KB
testcase_17 AC 1,389 ms
166,916 KB
testcase_18 AC 1,141 ms
152,008 KB
testcase_19 AC 580 ms
61,456 KB
testcase_20 AC 586 ms
62,192 KB
testcase_21 AC 271 ms
43,060 KB
testcase_22 AC 1,954 ms
195,544 KB
testcase_23 AC 1,934 ms
195,492 KB
testcase_24 AC 1,556 ms
171,452 KB
testcase_25 AC 1,741 ms
186,152 KB
testcase_26 AC 732 ms
67,508 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.10.21 19:29:45
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;


template < class Monoid >
struct SegmentTree {
private:
    using Func = std::function< Monoid(Monoid, Monoid) >;
    Func F;
    Monoid UNITY;
    int n;
    std::vector< Monoid > node;
    int _binary_search(int a, int b, const std::function<bool(Monoid)> &f, Monoid &s, int k = 0, int l = 0, int r = -1) {
        if (r < 0) r = n;
        if (r <= a || b <= l) return n;
        if (a <= l && r <= b && !f(F(s,node[k]))) {
            s = F(s,node[k]);
            return n;
        }
        if (l == r - 1) {s = F(s,node[k]); return l;}
        int ret = _binary_search(a, b, f, s, 2 * k + 1, l, (r - l) / 2 + l);
        return ret != n ? ret : _binary_search(a, b, f, s, 2 * k + 2, (r - l) / 2 + l, r);
    }
public:
    SegmentTree() {}
    SegmentTree(const std::vector< Monoid > &v, const Func f, const Monoid &unity) {
        F = f;
        UNITY = unity;
        int sz = v.size();
        n = 1;
        while (n < sz) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
        for (int i = 0; i < sz; i++) node[i + n - 1] = v[i];
        build();
    }
    
    SegmentTree(int m, const Monoid &val, const Func f, const Monoid &unity) {
        F = f;
        UNITY = unity;
        n = 1;
        while (n < m) n <<= 1;
        node.resize(n * 2 - 1, UNITY);
        if (val != UNITY) {
            for (int i = 0; i < m; i++) node[i + n - 1] = val;
            build();
        }
    }
    
    void set(int k, const Monoid &x) {
        node[n + k - 1] = x;
    }
    void build() {
        for (int i = n - 2; i >= 0; i--) node[i] = F(node[2 * i + 1], node[2 * i + 2]);
    }
    void update_query(int x, const Monoid &val) {
        if (x >= n || x < 0) return;
        x += n - 1;
        node[x] = val;
        while (x > 0) {
            x = (x - 1) >> 1;
            node[x] = F(node[2 * x + 1], node[2 * x + 2]);
        }
    }
    
    Monoid get_query(int l, int r) {
        Monoid L = UNITY, R = UNITY;
        if (l < 0) l = 0;
        if (r < 0) return UNITY;
        if (l >= n) return UNITY;
        if (r >= n) r = n;
        for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
            if (l & 1) L = F(L, node[l++ - 1]);
            if (r & 1) R = F(node[--r - 1], R);
        }
        return F(L, R);
    }
    
    
    int binary_search(int l, int r, const std::function<bool(Monoid)> &f) {
        Monoid s = UNITY;
        int ret = _binary_search(l,r,f,s);
        return ret != n ? ret : -1;
    }
    Monoid operator[](int x) const {
        return node[n + x - 1];
    }
    int size() {
        return n;
    }
    void print() {
        for (int i = 0; i < n; i++) {
            std::cout << i << "\t: " << node[n + i - 1] << std::endl;
        }
    }
};

int main() {
    int n;cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }
    vector<SegmentTree<int>> seg1(n,SegmentTree<int>(n,0,[](int a,int b){return max(a,b);},0));
    vector<SegmentTree<int>> seg2(n,SegmentTree<int>(n,0,[](int a,int b){return max(a,b);},0));
    for (int i = 0; i < n; i++) {
        seg1[i].update_query(i,1);
        seg2[i].update_query(i,1);
    }
    int ans = 1;
    for (int t = 1; t < n; t++) {
        for (int i = 0; i < n-t; i++) {
            int j = i+t;
            int x = 0,y = 0;
            if (a[i] < a[j]) {
                x = seg2[j].get_query(i+1,i+t+1) + 1;
            }
            if (a[i] > a[j]) {
                y = seg1[i].get_query(j-t,j) + 1;
            }
            ans = max({ans,x,y});
            seg1[i].update_query(j,x);
            seg2[j].update_query(i,y);
        }
    }
    cout << ans << endl;
    return 0;
}
0