結果

問題 No.1087 転倒数の転倒数
ユーザー beetbeet
提出日時 2020-06-19 23:18:52
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,848 bytes
コンパイル時間 2,479 ms
コンパイル使用メモリ 217,860 KB
実行使用メモリ 167,256 KB
最終ジャッジ日時 2023-09-16 15:30:35
合計ジャッジ時間 27,663 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 RE -
testcase_04 WA -
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 RE -
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 5 ms
4,912 KB
testcase_15 RE -
testcase_16 AC 5 ms
4,912 KB
testcase_17 AC 432 ms
167,256 KB
testcase_18 RE -
testcase_19 RE -
testcase_20 AC 5 ms
4,844 KB
testcase_21 RE -
testcase_22 AC 2 ms
4,380 KB
testcase_23 WA -
testcase_24 AC 20 ms
12,312 KB
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 WA -
testcase_29 AC 226 ms
91,748 KB
testcase_30 AC 100 ms
46,196 KB
testcase_31 AC 311 ms
123,616 KB
testcase_32 AC 313 ms
123,616 KB
testcase_33 AC 187 ms
76,704 KB
testcase_34 RE -
testcase_35 AC 390 ms
165,680 KB
testcase_36 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
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;}
using Int = long long;
const char newl = '\n';


template<typename T> void drop(const T &x){cout<<x<<endl;exit(0);}


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;
  }
};

//INSERT ABOVE HERE
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  int n,k;
  cin>>n>>k;

  if(n*(n-1)<k) drop("No");
  cout<<"Yes"<<newl;
  if(n==1) drop(0);

  assert(n>3);

  auto idx=[&](int i,int j){return i*n+j;};
  TopologicalSort G(n*n);

  auto gen=[&](int x){
    assert(0<=x and x<n);
    vector<int> vs(n,-1);
    vs[n-(x+1)]=n-1;
    for(int i=0,j=0;i<n;i++)
      if(vs[i]<0) vs[i]=j++;
    return vs;
  };

  auto rev=[&](vector<int> vs){
    int n=vs.size();
    vector<int> rs(n);
    for(int i=0;i<n;i++) rs[vs[i]]=i;
    return rs;
  };

  if(k<=n*(n-1)/2){
    for(int i=0;i+1<n;i++)
      for(int j=0;j<n;j++)
        G.add_edge(idx(i,j),idx(i+1,j));

    vector<int> ord(n);
    iota(ord.begin(),ord.end(),0);

    while(k){
      for(int i=0;i+1<n;i++)
        if(k and ord[i]<ord[i+1]) swap(ord[i],ord[i+1]),k--;
    }

    for(int i=0;i<n;i++){
      auto vs=gen(ord[i]);
      auto rs=rev(vs);
      for(int j=0;j+1<n;j++)
        G.add_edge(idx(i,rs[j]),idx(i,rs[j+1]));
    }

  }else{
    for(int j=0;j<n;j++){
      auto vs=gen(n-(j+1));
      auto rs=rev(vs);
      for(int i=0;i+1<n;i++)
        G.add_edge(idx(rs[i],j),idx(rs[i+1],j));
    }

    k-=n*(n-1)/2;
    for(int i=n-1;i>=0;i--){
      auto vs=gen(n-(i+1));
      vector<int> ws(n);
      iota(ws.begin(),ws.end(),0);

      int d=min(k,i);
      k-=d;
      while(d)
        for(int j=0;j+2<n;j++)
          if(d and ws[j]<ws[j+1]) swap(ws[j],ws[j+1]),d--;

      vector<int> zs(n);
      for(int j=0;j<n;j++) zs[j]=vs[ws[j]];

      auto rs=rev(zs);
      for(int j=0;j+1<n;j++)
        G.add_edge(idx(i,rs[j]),idx(i,rs[j+1]));
    }
  }


  auto vs=G.build();
  assert((int)vs.size()==n*n);
  auto rs=rev(vs);
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      if(j) cout<<' ';
      cout<<rs[idx(i,j)];
    }
    cout<<newl;
  }
  return 0;
}
0