結果

問題 No.918 LISGRID
ユーザー milanis48663220milanis48663220
提出日時 2023-08-03 22:50:30
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 56 ms / 2,000 ms
コード長 3,248 bytes
コンパイル時間 1,664 ms
コンパイル使用メモリ 130,208 KB
実行使用メモリ 22,120 KB
最終ジャッジ日時 2024-04-21 21:36:39
合計ジャッジ時間 5,061 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 30 ms
14,280 KB
testcase_02 AC 14 ms
6,912 KB
testcase_03 AC 16 ms
8,448 KB
testcase_04 AC 13 ms
7,296 KB
testcase_05 AC 40 ms
16,988 KB
testcase_06 AC 36 ms
15,904 KB
testcase_07 AC 52 ms
21,100 KB
testcase_08 AC 31 ms
13,868 KB
testcase_09 AC 39 ms
17,244 KB
testcase_10 AC 39 ms
17,332 KB
testcase_11 AC 39 ms
17,288 KB
testcase_12 AC 46 ms
18,480 KB
testcase_13 AC 35 ms
15,516 KB
testcase_14 AC 47 ms
19,952 KB
testcase_15 AC 56 ms
22,120 KB
testcase_16 AC 23 ms
10,240 KB
testcase_17 AC 22 ms
10,748 KB
testcase_18 AC 22 ms
10,368 KB
testcase_19 AC 9 ms
5,632 KB
testcase_20 AC 30 ms
13,160 KB
testcase_21 AC 9 ms
6,144 KB
testcase_22 AC 7 ms
5,376 KB
testcase_23 AC 18 ms
9,216 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 3 ms
5,376 KB
testcase_35 AC 5 ms
5,376 KB
testcase_36 AC 3 ms
5,376 KB
testcase_37 AC 28 ms
12,752 KB
testcase_38 AC 20 ms
9,600 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;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int h, w; cin >> h >> w;
    vector<int> a(h), b(w);
    for(int i = 0; i < h; i++) cin >> a[i];
    for(int i = 0; i < w; i++) cin >> b[i];
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    auto to_idx = [&](int i, int j){
        return i*w+j;
    };
    vector<vector<int>> g(h*w);
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            if(i+1 < h){
                if(h-1-b[j] < i){
                    // cout << i << "," << j << "->" << i+1 << "," << j << endl;
                    g[to_idx(i, j)].push_back(to_idx(i+1, j));
                }else{
                    g[to_idx(i+1, j)].push_back(to_idx(i, j));
                }
            }
            if(j+1 < w){
                if(w-1-a[i] < j){
                    g[to_idx(i, j)].push_back(to_idx(i, j+1));
                }else{
                    g[to_idx(i, j+1)].push_back(to_idx(i, j));
                }
            }
        }
    }
    vector<int> ord;
    assert(topological_sort(g, ord));
    vector<int> ans(h*w);
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            ans[ord[i*w+j]] = i*w+j;
        }
    }
    for(int i = 0; i < h; i++){
        for(int j = 0; j < w; j++){
            cout << ans[to_idx(i, j)]+1 << ' ';
        }
        cout << endl;
    }
}
0