結果

問題 No.918 LISGRID
ユーザー beetbeet
提出日時 2019-10-25 23:12:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,994 bytes
コンパイル時間 2,553 ms
コンパイル使用メモリ 226,540 KB
実行使用メモリ 27,108 KB
最終ジャッジ日時 2023-10-11 09:18:47
合計ジャッジ時間 51,739 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 38 ms
18,560 KB
testcase_02 AC 17 ms
8,388 KB
testcase_03 AC 20 ms
10,504 KB
testcase_04 AC 14 ms
8,876 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 AC 1 ms
4,348 KB
testcase_27 AC 2 ms
4,348 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 44 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 4 ms
4,348 KB
testcase_32 AC 1 ms
4,352 KB
testcase_33 AC 2 ms
4,352 KB
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using Int = long long;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}


struct TopologicalSort{
  vector< set<int> > G;
  vector<int> used,indeg,ps;

  TopologicalSort(){}
  TopologicalSort(int n):G(n),used(n,0),indeg(n,0){}

  void add_edge(int s,int t){
    G[s].emplace(t);
    indeg[t]++;
  }

  void bfs(int s){
    queue<int> que;
    que.emplace(s);
    used[s]=1;
    while(!que.empty()){
      int v=que.front();que.pop();
      ps.emplace_back(v);
      for(int u:G[v]){
        indeg[u]--;
        if(indeg[u]==0&&!used[u]){
          used[u]=1;
          que.emplace(u);
        }
      }
    }
  }

  vector<int> build(){
    int n=G.size();
    for(int i=0;i<n;i++)
      if(indeg[i]==0&&!used[i]) bfs(i);
    return ps;
  }
};

using i16 = int16_t;
using i32 = int32_t;
using i64 = int64_t;

using u16 = uint16_t;
using u32 = uint32_t;
using u64 = uint64_t;

// AtCoder
const i64 CYCLES_PER_SEC = 2000000000;

struct Timer {
	i64 start;
	Timer(){reset();}
	void reset(){start=getCycle();}
	inline double get(){return (double)(getCycle()-start)/CYCLES_PER_SEC;}
	inline i64 getCycle(){
		u32 low,high;
		__asm__ volatile ("rdtsc" : "=a" (low), "=d" (high));
		return ((i64)low)|((i64)high<<32);
	}
};

Timer timer;

//INSERT ABOVE HERE
signed main(){
  int h,w;
  cin>>h>>w;
  vector<int> as(h),bs(w);
  for(int i=0;i<h;i++) cin>>as[i];
  for(int i=0;i<w;i++) cin>>bs[i];

  auto check=
    [&](){
      TopologicalSort G(h*w);
      auto idx=[&](int y,int x){return y*w+x;};

      for(int i=0;i<h;i++){
        vector<int> vs(w);
        for(int j=0;j<w;j++) vs[j]=w-(j+1);
        for(int j=w-as[i],k=0;j<w;j++) vs[j]=k++;

        using P = pair<int, int>;
        vector<P> vp;
        for(int j=0;j<w;j++)
          vp.emplace_back(vs[j],j);
        sort(vp.begin(),vp.end());
        for(int j=0;j+1<w;j++)
          G.add_edge(idx(i,vp[j].second),idx(i,vp[j+1].second));
      }

      for(int j=0;j<w;j++){
        vector<int> vs(h);
        for(int i=0;i<h;i++) vs[i]=h-(i+1);
        for(int i=h-bs[j],k=0;i<h;i++) vs[i]=k++;
        using P = pair<int, int>;
        vector<P> vp;
        for(int i=0;i<h;i++)
          vp.emplace_back(vs[i],i);
        sort(vp.begin(),vp.end());
        for(int i=0;i+1<h;i++)
          G.add_edge(idx(vp[i].second,j),idx(vp[i+1].second,j));
      }

      auto ps=G.build();

      if((int)ps.size()!=h*w) return;

      vector< vector<int> > ans(h,vector<int>(w,0));
      for(int i=0;i<h*w;i++)
        ans[ps[i]/w][ps[i]%w]=i+1;

      for(int i=0;i<h;i++){
        for(int j=0;j<w;j++){
          if(j) cout<<" ";
          cout<<ans[i][j];
        }
        cout<<endl;
      }
      exit(0);
    };

  random_device rd;
  mt19937 mt(rd());
  while(timer.get()<1.9){
    shuffle(as.begin(),as.end(),mt);
    shuffle(bs.begin(),bs.end(),mt);
    check();
  }

  return 0;
}
0