QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#287644#5665. AA Country and King Dreamoonuser224RE 0ms23956kbC++143.5kb2023-12-20 21:03:092023-12-20 21:03:09

Judging History

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

  • [2023-12-20 21:03:09]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:23956kb
  • [2023-12-20 21:03:09]
  • 提交

answer

#include <bits/stdc++.h>
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
#pragma GCC optimize(2)
#pragma unroll 5
#define fori(i,a,b) for(int i = a;i <= b;i ++)
#define fore(i,a,b) for(int i = a;i >= b;i --)
#define batbit(x,i) (x | (1ll << i))
#define tatbit(x,i) (x &~ (1 << i))
#define bit(n,i) ((n >> i) & 1)
#define pii pair<int,int>
#define se second
#define fi first
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define pob pop_back
using namespace std;
typedef long long ll;
mt19937 rng (chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r)
{
    return l + rng() % (r - l + 1);
}
mt19937 mrand(random_device{}());
int const N = 5e5 + 10;
vector<int> ve[N];
void speed()
{
	ios_base::sync_with_stdio(NULL);
	cin.tie(NULL);
	cout.tie(NULL);
}
int n;
int a[N],ans[N];
int vis[N],h[N],pa[N];
set<int> s;
vector<int> ui,yi;
void reset(int m)
{
    s.clear();
    ui.clear();
    yi.clear();
    fori(i,1,m)
    {
        h[i] = 0;
        vis[i] = 0;
        pa[i] = 0;
        ans[i] = 0;
    }
}
bool check()
{
    if(!s.empty() && ui.empty()) return true;
    int u = *s.begin();
    int v = ui.back();
    if(!s.empty() && u < v)
        return true;
    return false;
}
void solve()
{
    cin >> n;
    int m = 2 * n - 1;
    fori(i,1,m) cin >> a[i];
    ans[1] = ans[m] = 1;
    a[1] = a[m] = 1;
    int l = 0,r = 0;
    fori(i,1,m)
    {
        if(a[i]) continue;
        if(!l) l = i;
        r = i;
    }
    if(l == 1 && r == m)
    {
        int cnt = 1;
        fori(i,2,m - 1)
        {
            if(i % 2) ans[i] = 1;
            else ans[i] = ++ cnt;
        }
        fori(i,1,m) cout << ans[i] << " ";
        reset(m);
        return;
    }
    int p = 1;
    fori(i,1,l - 1)
    {
        int j = a[i];
        if(pa[j]) p = j;
        else
        {
            pa[j] = p;
            h[j] = h[p] + 1;
            p = j;
        }
    }
    //ans[m] = 1;
    int q = 1;
    fore(i,m,r + 1)
    {
        int j = a[i];
        if(pa[j]) q = j;
        else
        {
            pa[j] = q;
            h[j] = h[q] + 1;
            q = j;
        }
    }
    fori(i,1,m) vis[a[i]] ++;
    fori(i,1,n)
    {
        if(vis[i]) continue;
        s.insert(i);
    }
    ll x = p,X = q;
    while(x != X)
    {
        if(h[x] >= h[X])
        {
            x = pa[x];
            if(x != X) ui.pb(x);
        }
        else
        {
            X = pa[X];
            if(x != X) yi.pb(X);
        }
    }
    reverse(yi.begin(),yi.end());
    if(p != q) yi.pb(q);
    for(auto v : yi) ui.pb(v);
    reverse(ui.begin(),ui.end());
    fori(i,1,l - 1) ans[i] = a[i];
    fori(i,l,r)
    {
        if(check())
        {
            ui.pb(p);
            p = *s.begin();
            s.erase(s.begin());
            ans[i] = p;
        }
        else
        {
            ans[i] = ui.back();
            p = ui.back();
            ui.pob();
        }
    }
    fori(i,r + 1,m) ans[i] = a[i];
    fori(i,1,m) cout << ans[i] << " ";
    reset(m);
}
int main()
{
    speed();
    if(fopen("task.inp.txt","r"))
    {
        freopen("task.inp.txt","r",stdin);
        freopen("task.out.txt","w",stdout);
    }
    if(fopen("euler.inp","r"))
    {
        freopen("euler.inp","r",stdin);
        freopen("euler.out","w",stdout);
    }
    int t;
    cin >> t;
    while(t --)
    {
        solve();
        cout << "\n";
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 23956kb

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
Runtime Error

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:


result: