結果

問題 No.1378 Flattening
ユーザー carrot46carrot46
提出日時 2021-02-08 18:45:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 216 ms / 2,000 ms
コード長 4,366 bytes
コンパイル時間 5,126 ms
コンパイル使用メモリ 172,100 KB
実行使用メモリ 198,864 KB
最終ジャッジ日時 2023-09-19 08:32:23
合計ジャッジ時間 8,702 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 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 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,384 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 216 ms
198,604 KB
testcase_32 AC 206 ms
198,656 KB
testcase_33 AC 190 ms
198,656 KB
testcase_34 AC 190 ms
198,696 KB
testcase_35 AC 206 ms
198,692 KB
testcase_36 AC 186 ms
198,784 KB
testcase_37 AC 175 ms
198,608 KB
testcase_38 AC 176 ms
198,816 KB
testcase_39 AC 174 ms
198,652 KB
testcase_40 AC 178 ms
198,756 KB
testcase_41 AC 175 ms
198,588 KB
testcase_42 AC 174 ms
198,692 KB
testcase_43 AC 175 ms
198,864 KB
testcase_44 AC 175 ms
198,604 KB
testcase_45 AC 15 ms
17,484 KB
testcase_46 AC 24 ms
27,540 KB
testcase_47 AC 5 ms
5,688 KB
testcase_48 AC 138 ms
153,608 KB
testcase_49 AC 151 ms
173,528 KB
testcase_50 AC 13 ms
15,216 KB
testcase_51 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <chrono>
//#pragma GCC optimize("Ofast")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()

using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;

ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
const ll INF = (1LL<<60);

template<class T> bool chmin(T &a, const T b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T b){
    if(a < b) {a = b; return true;}
    else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
    if(!v.empty()){
        for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
        std::cout<<v.back();
    }
    if(endline) std::cout<<std::endl;
}

template <unsigned long long mod > class modint{
public:
    ll x;
    constexpr modint(){x = 0;}
    constexpr modint(ll _x) : x((_x < 0 ? ((_x += (LLONG_MAX / mod) * mod) < 0 ? _x + (LLONG_MAX / mod) * mod : _x) : _x)%mod){}
    constexpr modint set_raw(ll _x){
        //_x in [0, mod)
        x = _x;
        return *this;
    }
    constexpr modint operator-(){
        return x == 0 ? 0 : mod - x;
    }
    constexpr modint& operator+=(const modint& a){
        if((x += a.x) >= mod) x -= mod;
        return *this;
    }
    constexpr modint operator+(const modint& a) const{
        return modint(*this) += a;
    }
    constexpr modint& operator-=(const modint& a){
        if((x -= a.x) < 0) x += mod;
        return *this;
    }
    constexpr modint operator-(const modint& a) const{
        return modint(*this) -= a;
    }
    constexpr modint& operator*=(const modint& a){
        (x *= a.x)%=mod;
        return *this;
    }
    constexpr modint operator*(const modint& a) const{
        return modint(*this) *= a;
    }
    constexpr modint pow(unsigned long long pw) const{
        modint res(1), comp(*this);
        while(pw){
            if(pw&1) res *= comp;
            comp *= comp;
            pw >>= 1;
        }
        return res;
    }
    //以下、modが素数のときのみ
    constexpr modint inv() const{
        if(x == 2) return (mod + 1) >> 1;
        return modint(*this).pow(mod - 2);
    }
    constexpr modint& operator/=(const modint &a){
        (x *= a.inv().x)%=mod;
        return *this;
    }
    constexpr modint operator/(const modint &a) const{
        return modint(*this) /= a;
    }
    constexpr bool sqrt(bool find_mini = false) {
        if(x == 0) return true;
        modint jge = this->pow((mod - 1)>>1);
        if(jge.x + 1 == mod) return false;
        if((mod&3) == 3){
            *this = this->pow((mod + 1)>>2);
        }else{
            int m = 0;
            modint c, t;
            if(mod == 998244353){
                m = 23; c = 15311432; t = this->pow(119);
                *this = this->pow(60);
            }else{
                ll q = mod - 1;
                modint z = 2;
                while(!(q&1)){q>>=1; ++m;}
                while(z.pow((mod-1)>>1).x == 1) z += 1;
                c = z.pow(q); t = this->pow(q);
                *this = this->pow((q+1)>>1);
            }
            while(t.x != 1){
                modint cpy_t = t;
                int pw = m;
                while(cpy_t.x != 1){--pw; cpy_t *= cpy_t;}
                rep(i, pw-1) c *= c;
                (*this) *= c;
                c *= c;
                t *= c;
                m -= pw;
            }
        }
        if(find_mini) this->x = min(this->x, (ll)mod - this->x);
        return true;
    }
};
#define mod1 998244353
using mint = modint<mod1>;

ostream& operator<<(ostream& os, const mint& a){
    os << a.x;
    return os;
}
using vm = vector<mint>;

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cin>>N;
    vec a(N);
    rep(i, N) {cin>>a[i]; --a[i];}
    vector<vm> dp(N + 1, vm(N + 1, 0));
    vm sum(N + 1, 0);
    dp[0][0] = sum[0] = 1;
    rep(i, N){
        int l = i, r = i + 1;
        while(l && a[l-1] > a[i]) --l;
        while(r < N && a[r] > a[i]) ++r;
        reps(j, l, r)  dp[i+1][j+1] = sum[j] + dp[i+1][j];
        rep(j, N + 1) sum[j] += dp[i+1][j];
    }
    //rep(i, N + 1) my_printv(dp[i]);
    //my_printv(sum);
    cout<<sum[N]<<endl;
}
0