結果

問題 No.918 LISGRID
ユーザー ningenMeningenMe
提出日時 2019-06-29 11:49:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 1,789 bytes
コンパイル時間 2,719 ms
コンパイル使用メモリ 159,876 KB
実行使用メモリ 13,028 KB
最終ジャッジ日時 2023-09-14 23:08:37
合計ジャッジ時間 8,236 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,388 KB
testcase_01 AC 33 ms
8,796 KB
testcase_02 AC 11 ms
4,916 KB
testcase_03 AC 14 ms
5,940 KB
testcase_04 AC 11 ms
5,152 KB
testcase_05 AC 40 ms
10,104 KB
testcase_06 AC 38 ms
9,664 KB
testcase_07 AC 57 ms
12,484 KB
testcase_08 AC 32 ms
8,516 KB
testcase_09 AC 40 ms
10,496 KB
testcase_10 AC 41 ms
10,472 KB
testcase_11 AC 42 ms
10,364 KB
testcase_12 AC 47 ms
11,148 KB
testcase_13 AC 37 ms
9,624 KB
testcase_14 AC 53 ms
12,056 KB
testcase_15 AC 56 ms
13,028 KB
testcase_16 AC 21 ms
6,840 KB
testcase_17 AC 21 ms
7,024 KB
testcase_18 AC 20 ms
6,792 KB
testcase_19 AC 6 ms
4,412 KB
testcase_20 AC 31 ms
8,384 KB
testcase_21 AC 8 ms
4,716 KB
testcase_22 AC 6 ms
4,404 KB
testcase_23 AC 17 ms
6,144 KB
testcase_24 AC 3 ms
4,384 KB
testcase_25 AC 2 ms
4,384 KB
testcase_26 AC 2 ms
4,508 KB
testcase_27 AC 1 ms
4,384 KB
testcase_28 AC 1 ms
4,384 KB
testcase_29 AC 2 ms
4,384 KB
testcase_30 AC 2 ms
4,384 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,384 KB
testcase_35 AC 4 ms
4,384 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 29 ms
8,108 KB
testcase_38 AC 17 ms
6,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main() {

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

	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