QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#602269 | #8837. Increasing Income | xydCatGirl | TL | 0ms | 7652kb | C++14 | 2.2kb | 2024-09-30 22:28:56 | 2024-09-30 22:28:58 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
constexpr int N = 2e5 + 5;
struct SegTree {
static constexpr int N = 2e5 + 5;
std::array<int, 2> data[N << 2];
#define ls(u) (u << 1)
#define rs(u) (u << 1 | 1)
void build(int u, int l, int r) {
if (l == r) return data[u] = {0, l}, void();
int mid = (l + r) >> 1;
build(ls(u), l, mid); build(rs(u), mid + 1, r);
data[u] = std::max(data[ls(u)], data[rs(u)]);
}
void modify(int u, int l, int r, int pos, int val) {
if (l == r) return data[u] = {val, l}, void();
int mid = (l + r) >> 1;
if (pos <= mid) modify(ls(u), l, mid, pos, val);
if (pos > mid) modify(rs(u), mid + 1, r, pos, val);
data[u] = std::max(data[ls(u)], data[rs(u)]);
}
std::array<int, 2> query(int u, int l, int r, int ql, int qr) {
if (ql <= l && r <= qr) return data[u];
int mid = (l + r) >> 1; std::array<int, 2> ret = {0, 0};
if (ql <= mid) ret = std::max(ret, query(ls(u), l, mid, ql, qr));
if (qr > mid) ret = std::max(ret, query(rs(u), mid + 1, r, ql, qr));
return ret;
}
} tree;
int T, n, m;
int p[N], q[N], f[N], pre[N], vis[N];
void solve() {
std::cin >> n; tree.build(1, 1, n);
for (int i = 1; i <= n; i++) {
std::cin >> p[i]; q[p[i]] = i, vis[i] = 0;
auto [max, pos] = tree.query(1, 1, n, 1, p[i]);
pre[i] = pos, f[i] = max + 1;
tree.modify(1, 1, n, p[i], f[i]);
}
pre[1] = 0; std::vector<int> a, b;
int cur = std::max_element(f + 1, f + 1 + n) - f;
while (cur != 0) a.push_back(cur), vis[cur] = 1, cur = q[pre[cur]];
std::reverse(a.begin(), a.end()); int m = a.size();
for (auto i : a) b.push_back(p[i]);
for (int i = 1; i < m; i++) b[i] = std::max(b[i - 1], b[i]);
std::vector<std::vector<int>> ins(n + 1);
for (int i = 1; i <= n; i++) if (!vis[i]) {
int pa = std::upper_bound(a.begin(), a.end(), i) - a.begin() - 1;
int pb = std::upper_bound(b.begin(), b.end(), p[i]) - b.begin() - 1;
ins[a[std::max(pa, pb)]].push_back(i);
}
for (auto i : a) {
std::cout << i << " ";
for (auto _ : ins[i]) std::cout << _ << " ";
}
std::cout << "\n";
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> T;
while (T--) solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 7652kb
input:
3 3 1 2 3 4 2 4 3 1 5 1 5 2 4 3
output:
1 2 3 1 2 3 4 1 3 4 2 5
result:
ok Correct (3 test cases)
Test #2:
score: -100
Time Limit Exceeded
input:
153 4 2 4 3 1 4 1 4 2 3 5 2 1 4 5 3 5 1 4 5 3 2 4 1 3 2 4 5 1 5 2 4 3 5 5 3 1 2 4 5 4 1 2 5 3 5 1 2 5 3 4 5 3 1 4 2 5 5 5 4 2 3 1 5 2 1 5 4 3 5 3 4 1 5 2 5 1 4 3 5 2 5 5 1 3 4 2 5 5 3 2 4 1 5 1 5 3 2 4 5 2 4 3 1 5 5 1 5 4 3 2 5 1 2 4 5 3 5 4 2 5 3 1 5 1 3 5 2 4 5 3 1 4 5 2 3 2 1 3 5 1 2 4 3 5 5 5 1 ...