結果
問題 | No.918 LISGRID |
ユーザー |
|
提出日時 | 2019-11-04 21:35:18 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 46 ms / 2,000 ms |
コード長 | 2,650 bytes |
コンパイル時間 | 1,609 ms |
コンパイル使用メモリ | 127,704 KB |
実行使用メモリ | 13,424 KB |
最終ジャッジ日時 | 2024-09-14 23:56:35 |
合計ジャッジ時間 | 5,948 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
ソースコード
#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; }