제출 #1042516

#제출 시각아이디문제언어결과실행 시간메모리
1042516xiaowuc1Xylophone (JOI18_xylophone)C++17
100 / 100
63 ms868 KiB
#include <bits/stdc++.h>
#include "xylophone.h"

using namespace std;

// BEGIN NO SAD
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
typedef vector<int> vi;
#define f first
#define s second
#define derr if(1) cerr

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
 
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ", "; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? ", " : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<" [" << #x << "] = ["; _print(x); cerr << "\e[39m" << flush;
// END NO SAD

template<class Fun>
class y_combinator_result {
  Fun fun_;
public:
  template<class T>
  explicit y_combinator_result(T &&fun): fun_(std::forward<T>(fun)) {}

  template<class ...Args>
  decltype(auto) operator()(Args &&...args) {
    return fun_(std::ref(*this), std::forward<Args>(args)...);
  }
};

template<class Fun>
decltype(auto) y_combinator(Fun &&fun) {
  return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

template<class T>
bool updmin(T& a, T b) {
  if(b < a) {
    a = b;
    return true;
  }
  return false;
}
template<class T>
bool updmax(T& a, T b) {
  if(b > a) {
    a = b;
    return true;
  }
  return false;
}

static int A[5001];
static bool used[5001];

void solve(int n) {
  int lhs = 1;
  int rhs = n;
  int ans = -1;
  while(lhs <= rhs) {
    int mid = (lhs+rhs)/2;
    if(query(mid, n) == n-1) {
      ans = mid;
      lhs = mid+1;
    }
    else {
      rhs = mid-1;
    }
  }
  auto place = [&](int idx, int val) -> void {
    assert(!used[val]);
    assert(A[idx] == 0);
    A[idx] = val;
    used[val] = true;
  };
  place(ans, 1);
  for(int i = ans-1; i >= 1; i--) {
    int diff = query(i, i+1);
    vector<int> cands;
    for(int x = 1; x <= n; x++) {
      if(!used[x] && abs(x - A[i+1]) == diff) cands.pb(x);
    }
    assert(sz(cands) > 0);
    if(sz(cands) == 2) {
      int full = query(i, i+2);
      vector<int> ncands;
      for(int cand: cands) {
        int lhs = min(cand, min(A[i+1], A[i+2]));
        int rhs = max(cand, max(A[i+1], A[i+2]));
        if(rhs-lhs == full) ncands.pb(cand);
      }
      swap(cands, ncands);
    }
    assert(sz(cands) == 1);
    place(i, cands[0]);
  }
  for(int i = ans+1; i <= n; i++) {
    int diff = query(i-1, i);
    vector<int> cands;
    for(int x = 1; x <= n; x++) {
      if(!used[x] && abs(x - A[i-1]) == diff) cands.pb(x);
    }
    assert(sz(cands) > 0);
    if(sz(cands) == 2) {
      int full = query(i-2, i);
      vector<int> ncands;
      for(int cand: cands) {
        int lhs = min(cand, min(A[i-1], A[i-2]));
        int rhs = max(cand, max(A[i-1], A[i-2]));
        if(rhs-lhs == full) ncands.pb(cand);
      }
      swap(cands, ncands);
    }
    assert(sz(cands) == 1);
    place(i, cands[0]);
  }
	for(int i = 1; i <= n; i++) {
		answer(i, A[i]);
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...