ykmakuのブログ

競技プログラミングをがんばるブログ

AtCoder Grand Contest 006 B - Median Pyramid Easy

  • 問題

B - Median Pyramid Easy

  • 解法

実際にピラミッドを作るには\mathcal{O}(N^{2})の時間計算量がかかるため無理。なので入力から直接数列を作ることを考えないといけない。
x = 1 ,\ x = 2n-1のときは無理である(中央値になりえないため)。x = nのときは1,2,\dots,2n-1を出力すれば良い。他の場合は解説pdfにある通りで2段目の真ん中の2マスがxになればよい。このようにするには、x{<}nのときは数列の真ん中に1を、その隣にxを配置すれば良い。x{>}nのときは数列の真ん中に2n-1、その隣にxを配置すれば良い。

#include <iostream>
#include <string>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cmath>

using namespace std;

typedef long long int ll;

#define all(x) x.begin(),x.end()

const ll mod = 1e9+7;
const ll INF = 1e9;
const ll MAXN = 1e9;

int main()
{
  int n,x;
  cin >> n >> x;
  if(x == 1 || x == 2*n-1) cout << "No" << endl;
  else{
    cout << "Yes" << endl;
    vector<int> ans(2*n);
    for(int i = 0; i < ans.size(); i++){
      ans[i] = i;
    }
    if(x != n){
      if(x < n){
        ans[n-1] = x;
        ans[x] = n-1;
        ans[n] = 1;
        ans[1] = n;
      }else{
        ans[n+1] = x;
        ans[x] = n+1;
        ans[2*n-1] = n;
        ans[n] = 2*n-1;
      }
      
    }

    for(int i = 1; i <= 2*n-1; i++){
      cout << ans[i] << endl;
    }
  }

  return 0;
}