QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#104048#5665. AA Country and King DreamoontarjenTL 19ms47320kbC++173.9kb2023-05-08 13:27:552023-05-08 13:27:58

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-05-08 13:27:58]
  • 评测
  • 测评结果:TL
  • 用时:19ms
  • 内存:47320kb
  • [2023-05-08 13:27:55]
  • 提交

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];
bool never[maxn];
int jmp[maxn];
int a[maxn];
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(never[x])return;
    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+6;i++)ve[i].clear(),L[i]=R[i]=m[i]=jmp[i]=-1,in[i].clear(),out[i].clear(),a[i]=0,never[i]=false;
    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);
        if(ve[i].size()>0){
            jmp[ve[i].back()]=ve[i].front();
        }
    }
}
void solve()
{
    int n;cin>>n;
    init(n);
    vector<tuple<int,int,int>> sta;
    sta.emplace_back(1,1,ve[1].back());
    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((sta.size()>1)&&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()));
            never[get<0>(sta.back())]=true;
            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()));
            int t=get<2>(sta.back())-1;
            while(a[t]!=a[i]&&a[t]!=0){
                // cout<<"??\n";
                // cout<<"t="<<t<<"\n";
                // cout<<ve[a[t]].size()<<"\n";
                auto it=lower_bound(ve[a[t]].begin(),ve[a[t]].end(),i);
                t=*it-2;
                if(test)cout<<"i="<<i<<" t="<<t<<"\n";
            }
            
            sta.emplace_back(a[i],i,t);
        }
        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;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 19ms
memory: 47320kb

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 1 4 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1
1 2 1 3 1 4 1 5 1

result:

ok 9 lines

Test #2:

score: -100
Time Limit Exceeded

input:

28668
2
0 2 1
2
0 0 1
2
0 0 0
2
1 0 1
2
1 0 0
2
1 2 0
3
0 2 1 3 1
3
0 0 1 3 1
3
0 0 0 3 1
3
0 0 0 0 1
3
0 0 0 0 0
3
1 0 1 3 1
3
1 0 0 3 1
3
1 0 0 0 1
3
1 0 0 0 0
3
1 2 0 3 1
3
1 2 0 0 1
3
1 2 0 0 0
3
1 2 1 0 1
3
1 2 1 0 0
3
1 2 1 3 0
3
0 2 3 2 1
3
0 0 3 2 1
3
0 0 0 2 1
3
1 0 3 2 1
3
1 0 0 2 1
3
1 2 ...

output:

1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 1 3 1
1 2 3 2 1
1 2 3 2 1
1 2 3 2 1
1 2 3 2 1
1 2 3 2 1
1 2 3 2 1
1 2 3 2 1
1 2 3 2 1
1 2 3 2 1
1 3 1 2 1
1 3 1 2 1
1 3 ...

result: