結果

問題 No.1087 転倒数の転倒数
ユーザー beetbeet
提出日時 2020-06-19 22:54:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,370 bytes
コンパイル時間 2,558 ms
コンパイル使用メモリ 214,404 KB
実行使用メモリ 167,360 KB
最終ジャッジ日時 2023-09-16 15:07:46
合計ジャッジ時間 26,245 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 RE -
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 742 ms
167,360 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 5 ms
4,936 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 5 ms
4,900 KB
testcase_17 AC 415 ms
167,248 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 5 ms
4,760 KB
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 AC 19 ms
12,512 KB
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 AC 219 ms
92,032 KB
testcase_30 AC 99 ms
46,188 KB
testcase_31 AC 298 ms
123,568 KB
testcase_32 AC 300 ms
123,472 KB
testcase_33 AC 182 ms
76,864 KB
testcase_34 RE -
testcase_35 AC 409 ms
165,892 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);

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

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

  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