QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#103953 | #5665. AA Country and King Dreamoon | Robert_JYH | RE | 4ms | 45620kb | C++17 | 3.3kb | 2023-05-07 23:15:04 | 2023-05-07 23:15:05 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
const bool test=false;
const int maxn=6e5+10;
int L[maxn],R[maxn],m[maxn];
set<int> s[3];
vector<int> ve[maxn];
vector<int> in[maxn],out[maxn];
int a[maxn*2];
int Findok(int i){
int x=1000000;
if(i<2){
if(s[i].size()>0)x=min(x,*s[i].begin());
}
if(s[2].size()>0&&x>*s[2].begin()){
x=min(x,*s[2].begin());
s[2].erase(x);
s[i].insert(x);
}
return x;
}
void pushin(int x){
if(test)cout<<"pushin "<<x<<"\n";
if(m[x]==-1){
s[2].insert(x);
}
else{
s[m[x]].insert(x);
}
}
void pushout(int x)
{
if(test)cout<<"pushout "<<x<<"\n";
for(int z=0;z<3;z++)if(s[z].find(x)!=s[z].end())s[z].erase(x);
}
void init(int n)
{
s[0].clear();
s[1].clear();
s[2].clear();
for(int i=1;i<=n*2;i++)ve[i].clear(),L[i]=R[i]=m[i]=-1,in[i].clear(),out[i].clear();
for(int i=1;i<n*2;i++){
cin>>a[i];
if(i==1||i==2*n-1)a[i]=1;
if(a[i]!=0){
ve[a[i]].push_back(i);
m[a[i]]=i%2;
}
}
vector<pair<int,int>> sta;
sta.emplace_back(1,2*n-1);
for(int i=1;i<2*n;i++){
if(a[i]!=0){
if(L[a[i]]==-1)L[a[i]]=sta.back().first;
if(R[a[i]]==-1)R[a[i]]=sta.back().second;
if(sta.back().second==i){
sta.pop_back();
auto it=upper_bound(ve[a[i]].begin(),ve[a[i]].end(),i);
if(it!=ve[a[i]].end()){
sta.emplace_back(i,*it);
}
}
else{
auto it=upper_bound(ve[a[i]].begin(),ve[a[i]].end(),i);
if(it!=ve[a[i]].end()){
sta.emplace_back(i,*it);
}
}
}
}
for(int i=1;i<=n;i++){
if(L[i]!=-1){
if(test)printf("L[%d]=%d R[%d]=%d\n",i,L[i],i,R[i]);
}
if(L[i]==-1){
L[i]=1,R[i]=n;
}
in[L[i]].push_back(i);
out[R[i]].push_back(i);
}
}
void solve()
{
int n;cin>>n;
init(n);
vector<tuple<int,int,int>> sta;
sta.emplace_back(1,1,ve[1][1]);
for(auto &it:in[1])pushin(it);
for(int i=2;i<n*2-1;i++){
for(auto &it:in[i])pushin(it);
if(test){
cout<<"i="<<i<<"-------\n";
cout<<"sta :";for(auto [x,y,z]:sta)cout<<x<<"("<<y<<","<<z<<") ";;cout<<"\n";
}
if(a[i]==0){
if(get<2>(sta[(int)sta.size()-2])==i){
a[i]=get<0>(sta[(int)sta.size()-2]);
}
else{
a[i]=Findok(i%2);
assert(a[i]<=n);
}
}
if(a[i]==get<0>(sta[(int)sta.size()-2])){
pushout(get<0>(sta.back()));
sta.pop_back();
}
else{
pushout(get<0>(sta.back()));
in[max(i+1,ve[a[i]].empty()?0:ve[a[i]].back()+1)].push_back(get<0>(sta.back()));
sta.emplace_back(a[i],i,get<2>(sta.back())-1);
}
if(test)cout<<"a["<<i<<"]="<<a[i]<<"\n";
}
for(int i=1;i<2*n;i++)cout<<a[i]<<" \n"[i==2*n-1];
}
int main()
{
// ios::sync_with_stdio(false);
// cin.tie(0);
int T;cin>>T;while(T--)solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 45620kb
input:
9 5 1 2 3 2 0 2 1 5 1 5 1 2 3 0 0 2 1 5 1 5 1 2 0 0 0 2 1 5 1 5 1 2 0 0 0 0 1 5 1 5 1 0 0 0 0 0 1 5 1 5 1 0 0 0 0 0 0 5 1 5 1 0 0 0 0 0 0 0 1 5 1 0 0 0 0 0 0 0 0 5 0 0 0 0 0 0 0 0 0
output:
1 2 3 2 4 2 1 5 1 1 2 3 2 4 2 1 5 1 1 2 3 2 4 2 1 5 1 1 2 1 3 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