結果

問題 No.1910 High Element on Grid
ユーザー milanis48663220milanis48663220
提出日時 2022-04-22 23:31:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 103 ms / 2,000 ms
コード長 3,995 bytes
コンパイル時間 1,787 ms
コンパイル使用メモリ 129,392 KB
実行使用メモリ 32,480 KB
最終ジャッジ日時 2023-09-06 10:25:32
合計ジャッジ時間 11,061 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,384 KB
testcase_11 AC 73 ms
24,008 KB
testcase_12 AC 78 ms
25,112 KB
testcase_13 AC 77 ms
24,812 KB
testcase_14 AC 88 ms
28,764 KB
testcase_15 AC 62 ms
20,448 KB
testcase_16 AC 54 ms
18,112 KB
testcase_17 AC 59 ms
19,968 KB
testcase_18 AC 76 ms
24,508 KB
testcase_19 AC 60 ms
20,056 KB
testcase_20 AC 60 ms
20,056 KB
testcase_21 AC 70 ms
23,324 KB
testcase_22 AC 46 ms
16,216 KB
testcase_23 AC 81 ms
26,424 KB
testcase_24 AC 53 ms
17,764 KB
testcase_25 AC 69 ms
22,476 KB
testcase_26 AC 62 ms
20,848 KB
testcase_27 AC 68 ms
21,988 KB
testcase_28 AC 41 ms
14,668 KB
testcase_29 AC 53 ms
17,604 KB
testcase_30 AC 59 ms
19,340 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 103 ms
32,468 KB
testcase_33 AC 103 ms
32,456 KB
testcase_34 AC 103 ms
32,480 KB
testcase_35 AC 100 ms
32,476 KB
testcase_36 AC 3 ms
4,380 KB
testcase_37 AC 42 ms
17,352 KB
testcase_38 AC 17 ms
8,484 KB
testcase_39 AC 20 ms
8,348 KB
testcase_40 AC 4 ms
4,380 KB
testcase_41 AC 27 ms
10,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <deque>
#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;

template<typename T>
vector<vector<T>> vec2d(int n, int m, T v){
    return vector<vector<T>>(n, vector<T>(m, v));
}

template<typename T>
vector<vector<vector<T>>> vec3d(int n, int m, int k, T v){
    return vector<vector<vector<T>>>(n, vector<vector<T>>(m, vector<T>(k, v)));
}

template<typename T>
void print_vector(vector<T> v, char delimiter=' '){
    if(v.empty()) {
        cout << endl;
        return;
    }
    for(int i = 0; i+1 < v.size(); i++) cout << v[i] << delimiter;
    cout << v.back() << endl;
}

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;
}


using P = pair<int, int>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int h, w; cin >> h >> w;
    vector<int> r(h), c(w);
    for(int i = 0; i < h; i++) {
        cin >> r[i];
    }
    for(int i = 0; i < w; i++) {
        cin >> c[i];
    }
    auto to_index = [&](int i, int j){
        return i*w+j;
    };
    vector<vector<int>> g(h*w);
    for(int i = 1; i < h; i++) g[to_index(i, 0)].push_back(to_index(0, 0));
    for(int j = 1; j < w; j++) g[to_index(0, j)].push_back(to_index(0, 0));
    for(int i = 1; i < h; i++){
        int x = w-r[i]+1;
        for(int j = 1; j <= x-1; j++){
            g[to_index(i, j)].push_back(to_index(i, 0));
        }
        if(x != w) g[to_index(i, 0)].push_back(to_index(i, x));
        for(int j = x; j+1 < w; j++){
            g[to_index(i, j)].push_back(to_index(i, j+1));
        }
    }
    for(int j = 1; j < w; j++){
        int x = h-c[j]+1;
        for(int i = 1; i <= x-1; i++){
            g[to_index(i, j)].push_back(to_index(0, j));
        }
        if(x != h) g[to_index(0, j)].push_back(to_index(x, j));
        for(int i = x; i+1 < h; i++){
            g[to_index(i, j)].push_back(to_index(i+1, j));
        }
    }
    vector<int> ord;
    assert(topological_sort(g, ord));
    vector<int> inv_ord(h*w);
    for(int i = 0; i < h*w; i++){
        inv_ord[ord[i]] = i;
    }
    auto ans = vec2d(h, w, 0);
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            int idx = to_index(i, j);
            ans[i][j] = inv_ord[idx]+1;
        }
    }
    for(int i = 0; i < h; i++) print_vector(ans[i]);
    // for(int i = 0; i < h; i++){
    //     int mx = 0;
    //     int cnt = 0;
    //     for(int j = 0; j < w; j++){
    //         if(mx < ans[i][j]) cnt++;
    //         chmax(mx, ans[i][j]);
    //     }
    //     assert(cnt == r[i]);   
    // }
    // for(int j = 0; j < w; j++){
    //     int mx = 0;
    //     int cnt = 0;
    //     for(int i = 0; i < w; i++){
    //         if(mx < ans[i][j]) cnt++;
    //         chmax(mx, ans[i][j]);
    //     }
    //     assert(cnt == c[j]);   
    // }
}
0