結果

問題 No.1668 Grayscale
ユーザー milanis48663220milanis48663220
提出日時 2021-09-03 23:31:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2,135 ms / 4,000 ms
コード長 3,479 bytes
コンパイル時間 1,693 ms
コンパイル使用メモリ 134,204 KB
実行使用メモリ 120,648 KB
最終ジャッジ日時 2023-08-22 01:42:34
合計ジャッジ時間 7,396 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 878 ms
120,616 KB
testcase_07 AC 626 ms
120,648 KB
testcase_08 AC 81 ms
20,216 KB
testcase_09 AC 2,135 ms
117,036 KB
testcase_10 AC 386 ms
25,192 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 51 ms
10,264 KB
testcase_14 AC 224 ms
23,156 KB
testcase_15 AC 86 ms
11,112 KB
testcase_16 AC 51 ms
7,784 KB
testcase_17 AC 30 ms
7,564 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <tuple>
#include <cmath>
#include <numeric>
#include <functional>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;

struct UnionFind {
    vector<int> data;
    UnionFind(int size) : data(size, -1) {}
    bool unionSet(int x, int y) {
        x = root(x); y = root(y);
        if (x != y) {
        if (data[y] < data[x]) swap(x, y);
        data[x] += data[y]; data[y] = x;
        }
        return x != y;
    }
    bool findSet(int x, int y) {
        return root(x) == root(y);
    }
    int root(int x) {
        return data[x] < 0 ? x : data[x] = root(data[x]);
    }
    int size(int x) {
        return -data[root(x)];
    }
};

bool topological_sort(vector<vector<int>> g, vector<int> &order){
    int n = g.size();
    order.clear();
    vector<bool> used(n, false);
    function<void(int)> dfs = [&](int v){
        used[v] = true;
        for(int to : g[v]){
            if(!used[to]) dfs(to);
        }
        order.push_back(v);
    };
    for(int v = 0; v < n; v++){
        if(!used[v]) dfs(v);
    }
    reverse(order.begin(), order.end());
    vector<int> inv_order(n);
    for(int i = 0; i < n; i++) inv_order[order[i]] = i;
    for(int v = 0; v < n; v++){
        for(int u : g[v]){
            if(inv_order[v] > inv_order[u]) return false;
        }
    }
    return true;
}

template<typename T>
class Compress{
    public:
    vector<T> data;
    int offset;
    Compress(vector<T> data_, int offset=0): offset(offset){
        set<T> st;
        for(T x: data_) st.insert(x);
        for(T x: st) data.push_back(x);
    };
    int operator[](T x) {
        auto p = lower_bound(data.begin(), data.end(), x);
        assert(x == *p);
        return offset+(p-data.begin());
	}
    T inv(int x){
        return data[x-offset];
    }
    int size(){
        return data.size();
    }
};

using P = pair<int, int>;

int dh[4] = {0, 0, -1, 1};
int dw[4] = {-1, 1, 0, 0};



int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int h, w, n; cin >> h >> w >> n;
    vector<vector<int>> c(h, vector<int>(w));
    vector<vector<int>> ans(h, vector<int>(w));
    map<int, vector<P>> mp;
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cin >> c[i][j];
            mp[c[i][j]].push_back(P(i, j));
        }
    }
    auto is_in_field = [&](int i, int j){
        return i >= 0 && i < h && j >= 0 && j < w;
    };
    int cur = 1;
    for(int i = 1; i <= n; i++){
        int tmp = cur;
        for(auto [x, y]: mp[i]){
            int p = cur;
            for(int k = 0; k < 4; k++){
                int x_to = x+dh[k];
                int y_to = y+dw[k];
                if(!is_in_field(x_to, y_to)) continue;
                if(c[x_to][y_to] == n) continue;
                chmax(p, ans[x_to][y_to]+1);
            }
            chmax(tmp, p);
        }
        for(auto [x, y]: mp[i]) ans[x][y] = tmp;
        cur = tmp;
    }
    cout << cur << endl;
}
0