# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1032298 |
2024-07-23T15:18:37 Z |
Tonyl |
Fun Tour (APIO20_fun) |
C++17 |
|
59 ms |
17640 KB |
#include "fun.h"
#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using pi = pair<int,int>;
using ll = long long;
#define REP(i,n) for (int i = 0; i < n; i++)
#define trav(a,x) for (auto &a : x)
#define all(x) (x).begin(), (x).end()
#define D(x) cerr << #x << ": " << x << endl;
int n, q;
struct Node {
bool exists = false;
int leftd = 0, rightd = 0;
};
const int MAX_N = 400004;
Node tree[MAX_N];
void update(int i) {
tree[i].leftd = 0;
if (tree[2*i].exists) tree[i].leftd = max(tree[2*i].leftd, tree[2*i].rightd) + 1;
tree[i].rightd = 0;
if (tree[2*i+1].exists) tree[i].rightd = max(tree[2*i+1].leftd, tree[2*i+1].rightd) + 1;
if (i > 1) update(i/2);
}
void consume(int i) {
assert(tree[i].exists);
tree[i].exists = 0;
update(i);
}
int sink(int i) {
if (tree[i].leftd > tree[i].rightd) return sink(2*i);
else if (tree[i].rightd == 0) return i;
else return sink(2*i+1);
}
int find_best(int i) {
int best = 2*i+1;
bool just_top = false;
if (tree[i].leftd >= tree[i].rightd) best = 2*i;
int maxs = max(tree[i].leftd, tree[i].rightd);
int bonus = 0;
while (i > 1) {
bool fr = i%2;
i/=2;
bonus++;
if (!tree[i].exists) break;
if (bonus > maxs) {
best = i; maxs = bonus;
just_top = 1;
}
if (fr) {
if (bonus+tree[i].leftd > maxs) {
best = 2*i;
maxs = bonus+tree[i].leftd;
just_top = 0;
}
} else {
if (bonus+tree[i].rightd > maxs) {
best = 2*i+1;
maxs = bonus+tree[i].rightd;
just_top = 0;
}
}
}
if (just_top) return best;
else return sink(best);
}
std::vector<int> createFunTour(int N, int Q) {
n = N; q = Q;
n++;
REP(i,n) tree[i].exists = 1;
REP(i,n) update(i);
int s = n-1;
vi ans = {s};
consume(s);
//D(s);
while (--N) {
s = find_best(s);
//D(s);
consume(s);
ans.push_back(s);
}
REP(i,n) ans[i]--;
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Tour is not fun |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Tour is not fun |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
444 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
59 ms |
17640 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
856 KB |
Output is correct |
22 |
Correct |
3 ms |
1372 KB |
Output is correct |
23 |
Correct |
7 ms |
2396 KB |
Output is correct |
24 |
Correct |
12 ms |
5304 KB |
Output is correct |
25 |
Correct |
33 ms |
11124 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Incorrect |
0 ms |
344 KB |
Tour is not fun |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Tour is not fun |
2 |
Halted |
0 ms |
0 KB |
- |