結果

問題 No.1668 Grayscale
ユーザー noiminoimi
提出日時 2021-06-10 02:57:47
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 334 ms / 4,000 ms
コード長 3,058 bytes
コンパイル時間 2,218 ms
コンパイル使用メモリ 206,668 KB
実行使用メモリ 27,460 KB
最終ジャッジ日時 2024-05-09 00:35:18
合計ジャッジ時間 4,247 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
11,748 KB
testcase_01 AC 3 ms
11,752 KB
testcase_02 AC 3 ms
11,744 KB
testcase_03 AC 3 ms
11,752 KB
testcase_04 AC 3 ms
11,748 KB
testcase_05 AC 3 ms
11,620 KB
testcase_06 AC 104 ms
27,460 KB
testcase_07 AC 124 ms
25,856 KB
testcase_08 AC 38 ms
13,440 KB
testcase_09 AC 334 ms
25,600 KB
testcase_10 AC 207 ms
16,640 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 5 ms
7,296 KB
testcase_13 AC 17 ms
5,376 KB
testcase_14 AC 55 ms
7,680 KB
testcase_15 AC 37 ms
6,144 KB
testcase_16 AC 29 ms
7,168 KB
testcase_17 AC 13 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 4 ms
6,528 KB
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 h, w, n, a[1010][1010], dp[1 << 20], nxt[1 << 20], v[1 << 20];
template <typename T = long long, const int sz = 5 * 10000000> struct IO {
    char reader[sz], writer[sz];
    char *now, *now2 = writer;
    IO() {
        reader[fread(reader, sizeof(char), sizeof(char) * sz, stdin)];
        now = reader;
    }
    inline T read() {
        while(*now && *now <= 32) now++;
        if(*now == '-') {
            now++;
            T res = 0;
            while('0' <= *now and *now <= '9') { res = res * 10 + *now++ - '0'; }
            return -res;
        } else {
            T res = 0;
            while('0' <= *now and *now <= '9') { res = res * 10 + *now++ - '0'; }
            return res;
        }
    }
    inline void read(T &res) {
        while(*now && *now <= 32) now++;
        if(*now == '-') {
            now++;
            res = 0;
            while('0' <= *now and *now <= '9') { res = res * 10 + *now++ - '0'; }
            res = -res;
        } else {
            res = 0;
            while('0' <= *now and *now <= '9') { res = res * 10 + *now++ - '0'; }
        }
    }
    inline string read_str() {
        string res;
        while(*now and *now != '\n' and *now != ' ') res += *now++;
        now++;
        return res;
    }
    inline void write(T x, char margin = ' ') {
        if(x == 0) {
            putchar('0');
            putchar(margin);
            return;
        }
        if(x < 0) {
            putchar('-');
            x = -x;
        }
        while(x) {
            *now2 = '0' + x % 10;
            now2++;
            x /= 10;
        }
        do {
            now2--;
            putchar(*now2);
        } while(now2 != writer);
        putchar(margin);
    }
    inline void write_str(string s, char margin = ' ') {
        for(auto c : s) putchar(c);
        putchar(margin);
    }
};
IO<int> io;
inline int read() { return io.read(); }
inline void write(int x) { io.write(x); }

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    h = read(), w = read(), n = read();
    for(int i = 0; i < h; i++)
        for(int j = 0; j < w; j++) a[i][j] = read();
    for(int i = 0; i < h; i++)
        for(int j = 0; j < w; j++) v[i * w + j] = a[i][j];
    sort(v, v + h * w);
    unique(v, v + h * w);
    for(int i = 0; i < h; i++)
        for(int j = 0; j < w; j++) a[i][j] = lower_bound(begin(v), begin(v) + n, a[i][j]) - begin(v);
    dp[0] = 1;
    for(int i = 0; i < n; i++) nxt[i] = n;
    for(int i = 0; i < h - 1; i++)
        for(int j = 0; j < w; j++) {
            auto [x, y] = minmax(a[i][j], a[i + 1][j]);
            if(x != y) nxt[x] = min(nxt[x], y);
        }
    for(int i = 0; i < h; i++)
        for(int j = 0; j < w - 1; j++) {
            auto [x, y] = minmax(a[i][j], a[i][j + 1]);
            if(x != y) nxt[x] = min(nxt[x], y);
        }
    for(int i = 0; i < n; i++) {
        dp[i + 1] = max(dp[i + 1], dp[i]);
        if(nxt[i] != n) dp[nxt[i]] = max(dp[nxt[i]], dp[i] + 1);
    }
    cout << dp[n] << endl;
}
0