#include <bits/stdc++.h>
using namespace std;
#include <bits/stdc++.h>
#include <utility>
using namespace std;
using ll = long long;
using pi = pair<int,int>;
using pl = pair<ll,ll>;
#define temp(T) template<class T>
temp(T) using V = vector<T>;
using vi = V<int>;
using vl = V<ll>;
using vvi = V<vi>;
using vpi = V<pi>;
using vb = V<bool>;
using vvb = V<vb>;
vi dx = {0,1,-1,0};
vi dy = {1,0,0,-1};
#define sz(x) x.size()
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sor(x) sort(all(x))
#define rsz resize
#define ins insert
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define F0R(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define R0F(i,a) ROF(i,0,a)
#define rep(a) F0R(_,a)
#define each(a,x) for (auto& a: x)
using str = string;
const int MOD = 1e9 + 7;
const ll BIG = 1e18;
const int INF = 1e9 + 2;
temp(T) bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0; } // set a = min(a,b)
temp(T) bool ckmax(T& a, const T& b) {
return a < b ? a = b, 1 : 0; } // set a = max(a,b)
void unsyncIO() { cin.tie(0)->sync_with_stdio(0); }
// FILE I/O
void setIn(str s) { freopen(s.c_str(),"r",stdin); }
void setOut(str s) { freopen(s.c_str(),"w",stdout); }
void setIO(str s = "") {
unsyncIO();
if (sz(s)) setIn(s+".in"), setOut(s+".out"); // for USACO
}
#define read(x) int x; cin >> x
temp(T) void pr(T x) {
cout << to_string(x);
}
temp(T) void prln(T x) {
cout << to_string(x) << "\n";
}
vi readV(int n) {
vi input(n);
F0R(i,n)
cin >> input[i];
return input;
}
int rand(int a, int b) {
return a + rand() % (b - a + 1);
}
#define gen(x,a,b) int x = rand(a,b)
temp(T) void moveAll(V<T> &a, V<T> &b) {
each(x,b) {
a.pb(x);
}
b.clear();
}
vi genArr(int n, int a, int b) {
vi generated(n);
F0R(i,n) {
generated[i] = rand(a,b);
}
return generated;
}
vvi genTree(int n) {
vvi g(n + 1);
F0R(i,n) {
gen(p,0,i);
g[p].pb(i + 1);
g[i + 1].pb(p);
}
return g;
}
vvb readGrid(int a, int b) {
vvb grid(a + 2, vb(b + 2, true));
F0R(i, a) {
F0R(j, b) {
bool k; cin >> k;
grid[i + 1][j + 1] = k;
}
}
return grid;
}
bool Query(int a, int b);
int findBest(int a, int b) {
if (a == -1) return b;
if (b == -1) return a;
return (Query(a,b)) ? b : a;
}
struct Seg {
Seg *left = nullptr, *right = nullptr;
int l, r, mid;
int v;
Seg(int l, int r) : l(l), r(r), mid((r + l) / 2) {
if (r - l > 1) {
left = new Seg(l,mid);
right = new Seg(mid,r);
v = findBest(left->v, right->v);
} else v = l;
}
void update(int k) {
if (r - l <= 1) {
v = -1;
return;
}
if (k < mid)
left->update(k);
else
right->update(k);
v = findBest(left->v, right->v);
}
};
int n;
vi arr;
int findNext(int l) {
if (l >= n - 1) return 1; // Edge literally
int siz = 2;
if (Query(arr[l], arr[l + 1])) return 1;
while (l + siz < n && Query(arr[l], arr[l + siz])) {
siz++;
}
return siz;
}
void flip(int l, int j) {
F0R(i, (j + 1) / 2) {
swap(arr[l + i], arr[l + (j - i)]);
}
}
vi edges;
void fix(int l) {
while (l < n) {
int nextJ = findNext(l);
if (nextJ == 1) {
edges.pb(l);
l += nextJ;
if (l >= n) return;
continue;
}
edges.pb(l);
edges.pb(l + nextJ - 1);
if (nextJ > 3 && !Query(l + 1, l + nextJ - 1)) {
nextJ--;
} else {
if (sz(edges) == 2) {
fix(l + nextJ);
} else {
int p1 = edges[sz(edges) - 3];
int p2 = edges[sz(edges) - 4];
if (!Query(arr[p1], arr[l + nextJ - 1]) and !Query(arr[p2], arr[l + nextJ - 1])) {
nextJ--;
}
}
}
flip(l, nextJ - 1);
l += nextJ;
}
}
std::vector<int> Solve(int N) {
Seg seg(0, N);
while (seg.v != -1) {
arr.pb(seg.v);
seg.update(seg.v);
}
n = N;
fix(0);
vi ans(n);
F0R(i, n) {
ans[arr[i]] = i;
}
return ans;
}
#ifdef DEBUG
namespace {
using std::exit;
using std::fprintf;
using std::printf;
using std::scanf;
constexpr int Q_MAX = 25'000;
constexpr int N_MAX = 1'000;
int N;
int S[N_MAX];
int query_count = 0;
void WrongAnswer(int code) {
printf("Wrong Answer [%d]\n", code);
exit(0);
}
} // namespace
bool Query(int a, int b) {
if (++query_count > Q_MAX) WrongAnswer(6);
if (a < 0 || N <= a || b < 0 || N <= b) WrongAnswer(4);
if (a == b) WrongAnswer(5);
return (S[a] > S[b]) ^ (abs(S[a] - S[b]) == 1);
}
int main() {
if (scanf("%d", &N) != 1) {
fprintf(stderr, "Error while reading.\n");
exit(1);
}
for (int i = 0; i < N; i++) {
if (scanf("%d", S + i) != 1) {
fprintf(stderr, "Error while reading.\n");
exit(1);
}
}
std::vector<int> T = Solve(N);
if ((int)T.size() != N) WrongAnswer(1);
for (int i = 0; i < N; i++) {
if (T[i] < 0 || N <= T[i]) WrongAnswer(2);
if (T[i] != S[i]) WrongAnswer(3);
}
printf("Accepted: %d\n", query_count);
return 0;
}
#endif
Compilation message
monster.cpp: In function 'void setIn(str)':
monster.cpp:59:28: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
59 | void setIn(str s) { freopen(s.c_str(),"r",stdin); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
monster.cpp: In function 'void setOut(str)':
monster.cpp:60:29: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | void setOut(str s) { freopen(s.c_str(),"w",stdout); }
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Wrong Answer [3] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
344 KB |
Wrong Answer [3] |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
41 ms |
780 KB |
Wrong Answer [3] |
2 |
Halted |
0 ms |
0 KB |
- |