結果

問題 No.365 ジェンガソート
ユーザー tottoripapertottoripaper
提出日時 2017-03-26 22:35:37
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,285 bytes
コンパイル時間 1,688 ms
コンパイル使用メモリ 166,900 KB
実行使用メモリ 5,096 KB
最終ジャッジ日時 2023-09-20 11:08:39
合計ジャッジ時間 3,836 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,396 KB
testcase_01 AC 2 ms
4,580 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 3 ms
4,396 KB
testcase_04 AC 3 ms
4,376 KB
testcase_05 AC 2 ms
4,504 KB
testcase_06 AC 3 ms
4,520 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 2 ms
4,424 KB
testcase_11 AC 2 ms
4,624 KB
testcase_12 AC 2 ms
4,440 KB
testcase_13 AC 2 ms
4,480 KB
testcase_14 AC 2 ms
4,420 KB
testcase_15 AC 2 ms
4,432 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 4 ms
4,616 KB
testcase_19 AC 23 ms
4,816 KB
testcase_20 AC 10 ms
4,504 KB
testcase_21 AC 5 ms
4,448 KB
testcase_22 AC 9 ms
4,644 KB
testcase_23 AC 7 ms
4,756 KB
testcase_24 AC 9 ms
4,896 KB
testcase_25 AC 4 ms
4,668 KB
testcase_26 WA -
testcase_27 AC 12 ms
4,964 KB
testcase_28 AC 8 ms
4,668 KB
testcase_29 AC 11 ms
4,932 KB
testcase_30 AC 7 ms
4,684 KB
testcase_31 AC 5 ms
4,460 KB
testcase_32 AC 5 ms
4,540 KB
testcase_33 AC 12 ms
4,904 KB
testcase_34 AC 4 ms
4,436 KB
testcase_35 AC 10 ms
4,676 KB
testcase_36 AC 28 ms
4,784 KB
testcase_37 AC 10 ms
4,752 KB
testcase_38 WA -
testcase_39 AC 19 ms
5,092 KB
testcase_40 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

// I want to delete this stupid solution ;(
#define sandwich(x) [](auto a, auto b){return (x)(a, b);}

template <typename T>
using F2 = std::function<T(T,T)>;

template <typename T, int N, typename F = std::plus<T>>
    struct SegmentTree{
        SegmentTree(T u, F f = F()){
            size = 1;
            while(size < N){
                size <<= 1;
            }

            unit = u;
            func = f;

            init();
        }
        void init(){
            for(int i=0;i<size;++i){
                data[i+size] = unit;
            }

            for(int i=size-1;i>0;--i){
                data[i] = func(data[i*2], data[i*2+1]);
            }
        }
        void update(int k, int v){
            k += size;
            data[k] = v;

            while(k > 1){
                k >>= 1;
                data[k] = func(data[k*2], data[k*2+1]);
            }
        }
        inline T query(int l, int r){
            return _query(l, r, 1, 0, size);
        }
        T _query(int a, int b, int k, int l, int r){
            if(b <= l || r <= a){return unit;}
            if(a <= l && r <= b){return data[k];}
            int mid = (l + r) >> 1;
            return func(_query(a, b,   2*k,   l, mid),
                        _query(a, b, 2*k+1, mid, r  ));
        }

        T unit, data[N*4];
        int size;
        F func;
    };

int A[100100];
SegmentTree<int, 100100> st(0);

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    int N;
    std::cin >> N;

    int mx_idx = -1, right_mx = 0;
    
    for(int i=0;i<N;++i){
        std::cin >> A[i];

        if(mx_idx != -1){
            right_mx = max(right_mx, A[i]);
        }
        if(A[i] == N){
            mx_idx = i;
        }
    }

    int res = N - (mx_idx + 1);
    for(int i=0;i<mx_idx;++i){
        if(A[i] < right_mx){
            ++res;
        }else{
            res += st.query(A[i], N+1) > 0;
        }

        st.update(A[i], 1);
    }

    std::cout << res << std::endl;
}
0