Submission #1042516

# Submission time Handle Problem Language Result Execution time Memory
1042516 2024-08-03T06:07:37 Z xiaowuc1 Xylophone (JOI18_xylophone) C++17
100 / 100
63 ms 868 KB
#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 time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 2 ms 344 KB Output is correct
17 Correct 3 ms 344 KB Output is correct
18 Correct 5 ms 344 KB Output is correct
19 Correct 9 ms 344 KB Output is correct
20 Correct 13 ms 344 KB Output is correct
21 Correct 6 ms 344 KB Output is correct
22 Correct 7 ms 344 KB Output is correct
23 Correct 8 ms 444 KB Output is correct
24 Correct 5 ms 344 KB Output is correct
25 Correct 5 ms 600 KB Output is correct
26 Correct 6 ms 344 KB Output is correct
27 Correct 7 ms 344 KB Output is correct
28 Correct 6 ms 600 KB Output is correct
29 Correct 6 ms 344 KB Output is correct
30 Correct 6 ms 444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 2 ms 344 KB Output is correct
17 Correct 3 ms 344 KB Output is correct
18 Correct 5 ms 344 KB Output is correct
19 Correct 9 ms 344 KB Output is correct
20 Correct 13 ms 344 KB Output is correct
21 Correct 6 ms 344 KB Output is correct
22 Correct 7 ms 344 KB Output is correct
23 Correct 8 ms 444 KB Output is correct
24 Correct 5 ms 344 KB Output is correct
25 Correct 5 ms 600 KB Output is correct
26 Correct 6 ms 344 KB Output is correct
27 Correct 7 ms 344 KB Output is correct
28 Correct 6 ms 600 KB Output is correct
29 Correct 6 ms 344 KB Output is correct
30 Correct 6 ms 444 KB Output is correct
31 Correct 17 ms 600 KB Output is correct
32 Correct 26 ms 344 KB Output is correct
33 Correct 59 ms 592 KB Output is correct
34 Correct 59 ms 448 KB Output is correct
35 Correct 54 ms 688 KB Output is correct
36 Correct 52 ms 344 KB Output is correct
37 Correct 44 ms 440 KB Output is correct
38 Correct 40 ms 440 KB Output is correct
39 Correct 53 ms 448 KB Output is correct
40 Correct 63 ms 428 KB Output is correct
41 Correct 57 ms 344 KB Output is correct
42 Correct 48 ms 676 KB Output is correct
43 Correct 38 ms 684 KB Output is correct
44 Correct 45 ms 440 KB Output is correct
45 Correct 51 ms 684 KB Output is correct
46 Correct 53 ms 432 KB Output is correct
47 Correct 54 ms 440 KB Output is correct
48 Correct 52 ms 448 KB Output is correct
49 Correct 53 ms 448 KB Output is correct
50 Correct 50 ms 448 KB Output is correct
51 Correct 55 ms 600 KB Output is correct
52 Correct 56 ms 444 KB Output is correct
53 Correct 50 ms 448 KB Output is correct
54 Correct 49 ms 432 KB Output is correct
55 Correct 55 ms 436 KB Output is correct
56 Correct 53 ms 436 KB Output is correct
57 Correct 54 ms 868 KB Output is correct
58 Correct 56 ms 704 KB Output is correct
59 Correct 48 ms 440 KB Output is correct
60 Correct 54 ms 448 KB Output is correct