結果

問題 No.1668 Grayscale
ユーザー ぷらぷら
提出日時 2021-09-03 22:36:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 874 ms / 4,000 ms
コード長 2,590 bytes
コンパイル時間 2,221 ms
コンパイル使用メモリ 207,136 KB
実行使用メモリ 27,392 KB
最終ジャッジ日時 2023-08-21 23:28:10
合計ジャッジ時間 6,731 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 745 ms
27,348 KB
testcase_07 AC 747 ms
27,392 KB
testcase_08 AC 145 ms
7,312 KB
testcase_09 AC 874 ms
27,112 KB
testcase_10 AC 274 ms
7,492 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 AC 46 ms
4,696 KB
testcase_14 AC 149 ms
8,540 KB
testcase_15 AC 66 ms
4,956 KB
testcase_16 AC 41 ms
4,384 KB
testcase_17 AC 31 ms
4,440 KB
testcase_18 AC 2 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 3 ms
4,384 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 1 ms
4,380 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]);
                }
            }
        }
    }
    for(int i = N-2; i >= 0; i--) {
        mex[i] = min(mex[i],mex[i+1]);
    }
    LazySegmentTree<int>dp(N+1);
    dp.update(0,1,0);
    for(int i = 0; i < N; i++) {
        dp.update(i+1,mex[i]+1,dp.query(i,i+1)+1);
    }
    cout << dp.query(N,N+1) << endl;
}
0