QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#241217 | #7729. Permutation Compression II | asxziill | WA | 141ms | 3572kb | C++23 | 4.1kb | 2023-11-06 00:29:11 | 2023-11-06 00:29:12 |
Judging History
answer
#include <bits/stdc++.h>
using ll = long long;
template<class Info>
struct SegmentTree {
int n;
std::vector<Info> info;
SegmentTree() : n(0) {}
SegmentTree(int n_, Info v_ = Info()) {
init(n_, v_);
}
template<class T>
SegmentTree(std::vector<T> init_) {
init(init_);
}
void init(int n_, Info v_ = Info()) {
init(std::vector(n_, v_));
}
template<class T>
void init(std::vector<T> init_) {
n = init_.size();
info.assign(4 << std::__lg(n), Info());
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
info[p] = init_[l];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
pull(p);
};
build(1, 0, n);
}
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void modify(int p, int l, int r, int x, const Info &v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = (l + r) / 2;
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 0, n, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if (l >= y || r <= x) {
return Info();
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
}
Info rangeQuery(int l, int r) {
return rangeQuery(1, 0, n, l, r);
}
template<class F>
int findFirst(int p, int l, int r, int x, int y, F pred) {
if (l >= y || r <= x || !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
int res = findFirst(2 * p, l, m, x, y, pred);
if (res == -1) {
res = findFirst(2 * p + 1, m, r, x, y, pred);
}
return res;
}
template<class F>
int findFirst(int l, int r, F pred) {
return findFirst(1, 0, n, l, r, pred);
}
template<class F>
int findLast(int p, int l, int r, int x, int y, F pred) {
if (l >= y || r <= x || !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
int res = findLast(2 * p + 1, m, r, x, y, pred);
if (res == -1) {
res = findLast(2 * p, l, m, x, y, pred);
}
return res;
}
template<class F>
int findLast(int l, int r, F pred) {
return findLast(1, 0, n, l, r, pred);
}
};
struct Info
{
int mx = 0;
int p = -1;
};
Info operator+(Info a, Info b){
if (a.mx > b.mx){
return a;
}
else{
return b;
}
}
void solve(){
int n;
std::cin >> n;
std::vector<int> pre(n + 1, -1);
SegmentTree<Info> seg(n);
std::vector<int> a(n);
for (int i = 0; i < n; i++){
std::cin >> a[i];
Info res = seg.rangeQuery(0, a[i]);
pre[a[i]] = res.p;
res.mx += 1;
res.p = i;
seg.modify(a[i], res);
}
Info res = seg.rangeQuery(0, n);
int b = res.mx;
int p = res.p;
std::set<int> s;
while (p != -1){
s.insert(p);
p = pre[a[p]];
}
int mx = 0;
std::vector<int> ans;
for (int i = 0; i < n; i++){
if (s.count(i)){
mx = std::max(mx, a[i]);
}
else if (a[i] > mx){
ans.push_back(i);
}
}
std::cout << b << " " << ans.size() << "\n";
for (int i = 0; i < ans.size(); i++){
std::cout << (ans[i] + 1) << " \n"[i == ans.size() - 1];
}
}
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--){
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3440kb
input:
2 3 3 1 2 3 1 2 3
output:
2 1 1 3 0
result:
ok ok n = 3
Test #2:
score: -100
Wrong Answer
time: 141ms
memory: 3572kb
input:
100000 7 2 6 7 1 4 3 5 6 1 5 6 3 2 4 3 1 2 3 3 2 1 3 14 9 6 13 10 4 7 5 14 1 12 8 3 2 11 3 1 2 3 14 1 9 3 14 5 7 4 6 12 2 8 11 13 10 8 7 1 3 6 2 5 8 4 16 9 3 4 8 7 16 10 6 11 1 14 2 13 12 5 15 3 3 1 2 33 9 10 23 3 16 1 19 32 25 4 5 31 28 7 22 27 30 8 6 17 2 14 13 29 20 33 26 18 24 11 12 15 21 2 2 1 ...
output:
3 0 3 0 3 0 2 0 4 5 1 3 4 8 10 3 0 7 3 2 4 9 4 1 1 7 2 1 6 2 1 1 9 21 1 2 3 5 7 8 9 12 13 15 16 17 20 22 23 24 25 26 27 28 29 1 1 1 2 0 1 0 2 0 5 5 2 3 5 6 7 2 0 6 3 3 4 11 3 0 2 0 5 3 1 3 4 1 1 1 3 2 1 7 2 0 4 3 1 2 4 10 19 1 4 8 9 10 11 12 13 14 15 16 17 20 22 25 27 28 31 32 2 2 1 2 1 0 4 3 1 5 6 ...
result:
wrong answer Jury has better answer