結果

問題 No.1095 Smallest Kadomatsu Subsequence
ユーザー soto800soto800
提出日時 2020-06-27 00:25:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,174 bytes
コンパイル時間 1,717 ms
コンパイル使用メモリ 171,900 KB
実行使用メモリ 11,112 KB
最終ジャッジ日時 2023-09-18 11:22:07
合計ジャッジ時間 4,843 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 WA -
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 WA -
testcase_09 AC 2 ms
4,380 KB
testcase_10 WA -
testcase_11 AC 2 ms
4,376 KB
testcase_12 WA -
testcase_13 AC 7 ms
4,376 KB
testcase_14 AC 7 ms
4,376 KB
testcase_15 WA -
testcase_16 AC 7 ms
4,380 KB
testcase_17 WA -
testcase_18 AC 7 ms
4,376 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 8 ms
4,376 KB
testcase_23 AC 139 ms
10,740 KB
testcase_24 AC 139 ms
10,816 KB
testcase_25 AC 139 ms
10,736 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 140 ms
10,740 KB
testcase_29 AC 139 ms
10,800 KB
testcase_30 AC 139 ms
11,000 KB
testcase_31 AC 139 ms
10,780 KB
testcase_32 AC 139 ms
10,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
 
#define lli long long int
#define REP(i,s,n) for(lli i=s;i<n;i++)
#define NUM 2520
#define INF (1LL<<50)
#define DEBUG 0
#define mp(a,b) make_pair(a,b)
#define SORT(V) sort(V.begin(),V.end())
#define PI (3.141592653589794)
#define TO_STRING(VariableName) # VariableName
#define LOG(x) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<endl;
#define LOG2(x,y) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<endl;
#define LOG3(x,y,z) if(DEBUG)cout<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
#define LOG4(w,x,y,z) if(DEBUG)cout<<TO_STRING(w)<<"="<<w<<" "<<TO_STRING(x)<<"="<<x<<" "<<TO_STRING(y)<<"="<<y<<" "<<TO_STRING(z)<<"="<<z<<endl;
 
template<class T>bool chmax(T & a, const T & b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
//https://algo-logic.info/segment-tree
/* SegTree<X>(n,fx,ex): モノイド(集合X, 二項演算fx, 単位元ex)についてサイズnで構築
    set(int i, X x), build(): i番目の要素をxにセット。まとめてセグ木を構築する。O(n)
    update(i,x): i 番目の要素を x に更新。O(log(n))
    query(a,b): [a,b) 全てにfxを作用させた値を取得。O(log(n))
   auto maxf = [](lli x1,lli x2) -> lli{return max(x1,x2);};
    SegTree<lli> stmax(n+1,maxf,-INF);
*/
template <typename X>
struct SegTree {
    using FX = function<X(X, X)>; // X•X -> X となる関数の型
    int n;
    FX fx;
    const X ex;
    vector<X> dat;
    SegTree(int n_, FX fx_, X ex_) : n(), fx(fx_), ex(ex_), dat(n_ * 4, ex_) {
        int x = 1;
        while (n_ > x) {
            x *= 2;
        }
        n = x;
    }
 
    void set(int i, X x) { dat[i + n - 1] = x; }
    void build() {
        for (int k = n - 2; k >= 0; k--) dat[k] = fx(dat[2 * k + 1], dat[2 * k + 2]);
    }
 
    void update(int i, X x) {
        i += n - 1;
        dat[i] = x;
        while (i > 0) {
            i = (i - 1) / 2;  // parent
            dat[i] = fx(dat[i * 2 + 1], dat[i * 2 + 2]);
        }
    }
 
    X query(int a, int b) { return query_sub(a, b, 0, 0, n); }
    X query_sub(int a, int b, int k, int l, int r) {
        if (r <= a || b <= l) {
            return ex;
        } else if (a <= l && r <= b) {
            return dat[k];
        } else {
            X vl = query_sub(a, b, k * 2 + 1, l, (l + r) / 2);
            X vr = query_sub(a, b, k * 2 + 2, (l + r) / 2, r);
            return fx(vl, vr);
        }
    }
};
void solve(){

    lli n;
    cin>>n;
    vector<lli> a(n);
    REP(i,0,n)cin>>a[i];
    
    auto maxf = [](lli x1,lli x2) -> lli{return min(x1,x2);};
    SegTree<lli> stmin(n+1,maxf,INF);

    REP(i,0,n){
        stmin.set(i,a[i]);
    }
    stmin.build();

    lli ans = INF;

    REP(i,1,n-1){
        lli lef = stmin.query(0,i);
        lli rig = stmin.query(i+1,n);
        LOG4(i,lef,a[i],rig);
        if(lef> a[i] || rig > a[i])continue;
        chmin(ans,lef+rig+a[i]);
    }

    if(ans==INF)cout<<-1<<endl;
    else cout<<ans<<endl;

}

int main(){

	lli t;
    t=1;
    while(t--)solve();


	return 0;
}
0