結果

問題 No.1668 Grayscale
ユーザー ぷらぷら
提出日時 2021-09-03 22:25:05
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,507 bytes
コンパイル時間 2,869 ms
コンパイル使用メモリ 209,240 KB
実行使用メモリ 27,520 KB
最終ジャッジ日時 2024-05-09 04:54:07
合計ジャッジ時間 7,703 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 WA -
testcase_05 AC 2 ms
5,376 KB
testcase_06 WA -
testcase_07 AC 787 ms
27,416 KB
testcase_08 AC 166 ms
7,296 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 2 ms
5,376 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

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

int inf = 1001001001;

template <typename T>
struct LazySegmentTree{
    int n;
    vector<T> dat;
    vector<T>lazy;
    LazySegmentTree() {};
    LazySegmentTree(int N) {
        n = 1;
        while(n < N) {
            n *= 2;
        }
        dat.resize(n*2-1,inf);
        lazy.resize(n*2-1,inf);
    }
    void eval(int k,int l,int r) {
        if(lazy[k] == inf) return;
        if(k < n-1) {
            lazy[k*2+1] = min(lazy[k*2+1],lazy[k]);
            lazy[k*2+2] = min(lazy[k*2+2],lazy[k]);
        }
        dat[k] = min(dat[k],lazy[k]);
        lazy[k] = inf;
    }
    void update(int a, int b, T x, int k, int l, int r) {
        eval(k,l,r);
        if(a <= l && r <= b) {
            lazy[k] = min(lazy[k],x);
            eval(k,l,r);
        }
        else if(a < r && l < b) {
            update(a, b, x, k*2+1, l, (l+r)/2);
            update(a, b, x, k*2+2, (l+r)/2, r);
            dat[k] = min(dat[k*2+1],dat[k*2+2]);
        }
    }
    void update(int a, int b, T x) {//[a,b)
        update(a, b, x, 0, 0, n);
    }
    T query_sub(int a, int b, int k, int l, int r) {
        eval(k,l,r);
        if (r <= a || b <= l) {
            return inf;
        }
        else if (a <= l && r <= b) {
            return dat[k];
        }
        else {
            T vl = query_sub(a, b, k*2+1, l, (l+r)/2);
            T vr = query_sub(a, b, k*2+2, (l+r)/2, r);
            return min(vl,vr);
        }
    }
    T query(int a, int b) {//[a,b)
        return query_sub(a, b, 0, 0, n);
    }
};


int main() {
    int H,W,N;
    cin >> H >> W >> N;
    vector<vector<int>>C(H,vector<int>(W));
    for(int i = 0; i < H; i++) {
        for(int j = 0; j < W; j++) {
            cin >> C[i][j];
            C[i][j]--;
        }
    }
    vector<int>mex(N,N);
    for(int i = 0; i < H; i++) {
        for(int j = 0; j < W; j++) {
            for(int k = 0; k < 4; k++) {
                int nx = i+dx[k];
                int ny = j+dy[k];
                if(nx >= 0 && ny >= 0 && nx < H && ny < W) {
                    if(C[i][j] >= C[nx][ny]) {
                        continue;
                    }
                    mex[C[i][j]] = min(mex[C[i][j]],C[nx][ny]);
                }
            }
        }
    }
    LazySegmentTree<int>dp(N);
    dp.update(0,1,0);
    for(int i = 0; i < N; i++) {
        dp.update(i,mex[i]+1,dp.query(i,i+1)+1);
    }
    cout << dp.query(N-1,N)+1 << endl;
}
0