結果

問題 No.918 LISGRID
ユーザー mamekinmamekin
提出日時 2019-11-04 21:35:18
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 2,650 bytes
コンパイル時間 2,264 ms
コンパイル使用メモリ 127,220 KB
実行使用メモリ 13,000 KB
最終ジャッジ日時 2023-10-13 02:11:56
合計ジャッジ時間 5,829 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 27 ms
9,068 KB
testcase_02 AC 9 ms
4,824 KB
testcase_03 AC 12 ms
5,704 KB
testcase_04 AC 9 ms
5,248 KB
testcase_05 AC 33 ms
10,228 KB
testcase_06 AC 28 ms
9,792 KB
testcase_07 AC 44 ms
12,728 KB
testcase_08 AC 25 ms
8,824 KB
testcase_09 AC 34 ms
10,452 KB
testcase_10 AC 34 ms
10,376 KB
testcase_11 AC 35 ms
10,504 KB
testcase_12 AC 37 ms
11,268 KB
testcase_13 AC 30 ms
9,592 KB
testcase_14 AC 44 ms
12,140 KB
testcase_15 AC 48 ms
13,000 KB
testcase_16 AC 17 ms
6,804 KB
testcase_17 AC 18 ms
7,340 KB
testcase_18 AC 17 ms
6,992 KB
testcase_19 AC 6 ms
4,372 KB
testcase_20 AC 25 ms
8,348 KB
testcase_21 AC 7 ms
4,704 KB
testcase_22 AC 5 ms
4,376 KB
testcase_23 AC 15 ms
6,276 KB
testcase_24 AC 3 ms
4,372 KB
testcase_25 AC 2 ms
4,368 KB
testcase_26 AC 2 ms
4,372 KB
testcase_27 AC 2 ms
4,368 KB
testcase_28 AC 2 ms
4,372 KB
testcase_29 AC 2 ms
4,368 KB
testcase_30 AC 2 ms
4,368 KB
testcase_31 AC 2 ms
4,372 KB
testcase_32 AC 2 ms
4,368 KB
testcase_33 AC 2 ms
4,368 KB
testcase_34 AC 3 ms
4,368 KB
testcase_35 AC 4 ms
4,368 KB
testcase_36 AC 2 ms
4,372 KB
testcase_37 AC 23 ms
8,412 KB
testcase_38 AC 16 ms
6,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <array>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
#include <memory>
#include <regex>
using namespace std;

bool topologicalSort(const vector<vector<int> >& edges, vector<int>& node)
{
    int n = edges.size();
    node.resize(n);

    vector<int> restEdges(n, 0);
    for(int i=0; i<n; ++i){
        for(unsigned j=0; j<edges[i].size(); ++j){
            ++ restEdges[edges[i][j]];
        }
    }

    int restNodes = n;
    queue<int> q;
    for(int i=0; i<n; ++i){
        if(restEdges[i] == 0){
            node[n-restNodes] = i;
            q.push(i);
            -- restNodes;
        }
    }

    while(restNodes > 0){
        if(q.empty()){
            node.clear();
            return false;
        }
        int i = q.front();
        q.pop();

        for(unsigned j=0; j<edges[i].size(); ++j){
            int k = edges[i][j];
            -- restEdges[k];
            if(restEdges[k] == 0){
                node[n-restNodes] = k;
                q.push(k);
                -- restNodes;
            }
        }
    }

    return true;
}

int main()
{
    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());

    vector<vector<int> > edges(h*w);
    for(int y=0; y<h; ++y){
        for(int x=0; x<w-1; ++x){
            int i = y * w + x;
            int j = y * w + (x + 1);
            if(x < a[y] - 1)
                edges[i].push_back(j);
            else
                edges[j].push_back(i);
        }
    }
    for(int x=0; x<w; ++x){
        for(int y=0; y<h-1; ++y){
            int i = y * w + x;
            int j = (y + 1) * w + x;
            if(y < b[x] - 1)
                edges[i].push_back(j);
            else
                edges[j].push_back(i);
        }
    }

    vector<int> node;
    topologicalSort(edges, node);
    vector<vector<int> > ans(h, vector<int>(w));
    for(int i=0; i<h*w; ++i){
        int y = node[i] / w;
        int x = node[i] % w;
        ans[y][x] = i + 1;
    }
    for(int y=0; y<h; ++y){
        cout << ans[y][0];
        for(int x=1; x<w; ++x)
            cout << ' ' << ans[y][x];
        cout << endl;
    }

    return 0;
}
0