結果

問題 No.918 LISGRID
ユーザー ningenMeningenMe
提出日時 2019-10-21 02:52:10
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 50 ms / 2,000 ms
コード長 1,693 bytes
コンパイル時間 1,824 ms
コンパイル使用メモリ 179,800 KB
実行使用メモリ 13,072 KB
最終ジャッジ日時 2023-09-15 14:53:45
合計ジャッジ時間 8,025 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 29 ms
8,936 KB
testcase_02 AC 10 ms
5,300 KB
testcase_03 AC 14 ms
5,880 KB
testcase_04 AC 10 ms
5,084 KB
testcase_05 AC 36 ms
10,052 KB
testcase_06 AC 32 ms
9,596 KB
testcase_07 AC 47 ms
12,412 KB
testcase_08 AC 27 ms
8,996 KB
testcase_09 AC 35 ms
10,252 KB
testcase_10 AC 35 ms
10,380 KB
testcase_11 AC 34 ms
10,312 KB
testcase_12 AC 40 ms
11,196 KB
testcase_13 AC 32 ms
9,528 KB
testcase_14 AC 44 ms
11,888 KB
testcase_15 AC 50 ms
13,072 KB
testcase_16 AC 17 ms
6,672 KB
testcase_17 AC 19 ms
6,972 KB
testcase_18 AC 17 ms
6,668 KB
testcase_19 AC 7 ms
4,380 KB
testcase_20 AC 25 ms
8,492 KB
testcase_21 AC 8 ms
4,620 KB
testcase_22 AC 5 ms
4,376 KB
testcase_23 AC 16 ms
6,140 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 3 ms
4,380 KB
testcase_35 AC 3 ms
4,380 KB
testcase_36 AC 2 ms
4,384 KB
testcase_37 AC 23 ms
8,012 KB
testcase_38 AC 16 ms
6,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {

	//入力
	int H,W; cin >> H >> W;
	vector<int> A,B;
	
	for(int i = 0; i < H; ++i){
		int tmp; cin >> tmp;
		A.push_back(tmp);
	}
	for(int j = 0; j < W; ++j){
		int tmp; cin >> tmp;
		B.push_back(tmp);
	}

    //sort O(HlogH + WlogW)
	sort(A.begin(),A.end());
	sort(B.begin(),B.end());
	
	//(i,j)からa[i],b[j]に対応した辺を張る
	vector<vector<int>> edge(H*W);//辺
	vector<vector<int>> cnt(H,vector<int>(W,0));//入次数
	for(int i = 0; i < H; ++i){
		for(int j = 0; j < W - A[i]; ++j){
			edge[i*W+j].push_back(i*W+j+1);
			cnt[i][j+1]++;
		}
		for(int j = W - A[i]; j + 1 < W; ++j){
			edge[i*W+j+1].push_back(i*W+j);
			cnt[i][j]++;
		}
	}
	for(int j = 0; j < W; ++j){
		for(int i = 0; i < H - B[j]; ++i){
			edge[i*W+j].push_back((i+1)*W+j);
			cnt[i+1][j]++;
		}
		for(int i = H - B[j]; i + 1 < H; ++i){
			edge[(i+1)*W+j].push_back(i*W+j);
			cnt[i][j]++;
		}
	}

	//出力する行列
	vector<vector<int>> ans(H,vector<int>(W,0));
	//4つ角からスタート
	vector<int> sy = {0,0,H-1,H-1},sx = {0,W-1,0,W-1};
	//デクリメントしていく値
	int val = H*W;

	//トポロジカル順を守りながらbfs;
	for(int i = 0; i < 4; ++i){
		queue<pair<int,int>> pq;
		if(!cnt[sy[i]][sx[i]] && !ans[sy[i]][sx[i]]) pq.push({sy[i],sx[i]});
		while(pq.size()){
			int y = pq.front().first;
			int x = pq.front().second;
			pq.pop();
			ans[y][x] = val--;
			for(auto j: edge[y*W+x]){
				int s = j/W, t = j%W;
				cnt[s][t]--;
				if(!cnt[s][t] && !ans[s][t]) pq.push({s,t});
			}
		}
	}
	
	for(int i = 0; i < H; ++i){
		for(int j = 0; j < W; ++j){
			cout << ans[i][j] << " ";
		}
		cout << endl;
	}
	return 0;
}
0