QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#458465 | #8837. Increasing Income | ucup-team045# | WA | 1ms | 3560kb | C++20 | 5.2kb | 2024-06-29 17:30:47 | 2024-06-29 17:30:48 |
Judging History
answer
#include<iostream>
#include<cstring>
#include<vector>
#include<numeric>
#include<functional>
using namespace std;
const int INF = 1e9;
struct Info{
int mx = -INF, mxpos = -INF;
};
using Tag = int;
Info operator+(const Info &a, const Info &b){
if (a.mx > b.mx) return a;
return b;
}
void apply(Info &x, Tag &a, Tag f){
x.mx += f;
a += f;
}
template<class Info, class Tag>
struct LazySegmentTree{
int n;
vector<Info> info;
vector<Tag> tag;
LazySegmentTree() {}
LazySegmentTree(int n, Info _init = Info()){
init(vector<Info>(n, _init));
}
LazySegmentTree(const vector<Info> &_init){
init(_init);
}
void init(const vector<Info> &_init){
n = (int)_init.size();
info.assign((n << 2) + 1, Info());
tag.assign((n << 2) + 1, Tag());
function<void(int, int, int)> build = [&](int p, int l, int r){
if (l == r){
info[p] = _init[l - 1];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m + 1, r);
pull(p);
};
build(1, 1, n);
}
void pull(int p){
info[p] = info[2 * p] + info[2 * p + 1];
}
void apply(int p, const Tag &v){
::apply(info[p], tag[p], v);
}
void push(int p){
apply(2 * p, tag[p]);
apply(2 * p + 1, tag[p]);
tag[p] = Tag();
}
void modify(int p, int l, int r, int x, const Info &v){
if (l == r){
info[p] = v;
return;
}
int m = (l + r) / 2;
push(p);
if (x <= m){
modify(2 * p, l, m, x, v);
}
else{
modify(2 * p + 1, m + 1, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v){
modify(1, 1, n, p, v);
}
Info query(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;
push(p);
return query(2 * p, l, m, x, y) + query(2 * p + 1, m + 1, r, x, y);
}
Info query(int l, int r){
return query(1, 1, n, l, r);
}
void modify(int p, int l, int r, int x, int y, const Tag &v){
if (l > y || r < x){
return;
}
if (l >= x && r <= y){
apply(p, v);
return;
}
int m = (l + r) / 2;
push(p);
modify(2 * p, l, m, x, y, v);
modify(2 * p + 1, m + 1, r, x, y, v);
pull(p);
}
void modify(int l, int r, const Tag &v){
return modify(1, 1, n, l, r, v);
}
};
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int T;
cin >> T;
while(T--){
int n;
cin >> n;
vector<int> p(n + 1), a(n + 1);
for(int i = 1; i <= n; i++){
cin >> p[i];
a[p[i]] = i;
}
LazySegmentTree<Info, Tag> seg(n);
vector<int> pre(n + 1);
vector<int> dp(n + 1);
for(int i = 1; i <= n; i++){
dp[p[i]] = 2;
{
auto [mx, mxpos] = seg.query(1, p[i] - 1);
if (mx + 2 > dp[p[i]]){
dp[p[i]] = mx + 2;
pre[p[i]] = mxpos;
}
}
seg.modify(p[i] + 1, n, 1);
seg.modify(p[i], {dp[p[i]], p[i]});
}
int best = -1, val = -INF;
for(int i = 1; i <= n; i++){
int t = seg.query(i, i).mx + (n - i);
if (t >= val){
val = t;
best = i;
}
// cout << i << ' ' << t << '\n';
}
vector<int> ans;
{
int t = best;
while(t){
ans.push_back(a[t]);
t = pre[t];
}
reverse(ans.begin(), ans.end());
}
vector<int> q;
for(int i = 0; i + 1 < ans.size(); i++){
int l = ans[i], r = ans[i + 1];
for(int j = l + 1; j < r; j++){
if (p[j] < p[l]){
q.push_back(j);
}
}
}
for(int i = ans.back() + 1; i <= n; i++){
if (p[i] < p[ans.back()]){
q.push_back(i);
}
}
ans.insert(ans.end(), q.begin(), q.end());
sort(ans.begin(), ans.end());
vector<bool> v(n + 1);
for(auto x : ans) v[x] = true;
q.clear();
for(int i = 1; i <= n; i++){
if (!v[i]){
q.push_back(i);
}
}
sort(q.begin(), q.end(), [&](int a, int b){
return p[a] < p[b];
});
ans.insert(ans.end(), q.begin(), q.end());
for(auto x : ans) cout << x << ' ';
cout << '\n';
}
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3560kb
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 5 2
result:
ok Correct (3 test cases)
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 3492kb
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 ...
output:
1 2 3 4 1 3 4 2 1 2 3 4 5 1 2 3 4 5 1 2 3 4 1 3 4 5 2 2 3 4 5 1 2 3 5 1 4 1 2 4 5 3 1 2 3 4 5 3 4 5 2 1 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 2 3 4 5 1 2 3 4 5 1 1 3 4 5 2 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 1 2 3 4 5 2 3 4 5 1 1 2 3 4 5 3 4 5 2 1 1 ...
result:
wrong answer Jury found better answer than participant (test case 7)