QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#553840#5665. AA Country and King DreamoonBongoCatEnjoyer#WA 69ms3812kbC++203.3kb2024-09-08 21:06:572024-09-08 21:06:58

Judging History

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

  • [2024-09-08 21:06:58]
  • 评测
  • 测评结果:WA
  • 用时:69ms
  • 内存:3812kb
  • [2024-09-08 21:06:57]
  • 提交

answer

#include <bits/stdc++.h>

#define ll                      long long
#define all(v)                  (v).begin(), (v).end()
#define forn(var, n)            for (ll (var) = 0; (var) < (n); ++(var))
#define forr(var, start, end)   for (ll (var) = (start); (var) < (end); ++(var))
#define fori(var, start, end)   for (ll (var) = (start); (var) > (end); --(var))
#define fora(var, obj)          for (auto (var) : (obj))
#define MOD1                    (ll) (1e9 + 7)
#define MOD9                    (ll) (998244353)
#define prints(x)               cout << (x) << " "
#define println(x)              cout << (x) << "\n"
#define newl()                  cout << "\n"

using namespace std;

void solve() {
    ll n; cin >> n;
    vector<ll> a(2 * n - 1); fora(&x, a) cin >> x;

    ll sz = a.size();
    if (a[0] == 0) a[0] = 1;
    if (a[sz - 1] == 0) a[sz - 1] = 1;
    set<ll> uv; forn(i, n) uv.insert(i + 1); fora(x, a) if (uv.find(x) != uv.end()) uv.erase(x);

    stack<ll> ls, lps, rs, rps; ll lp = -1, rp = -1;
    forn(i, sz) {
        if (a[i] == 0) {
            lp = i;
            break;
        }

        if (ls.empty()) {
            ls.push(a[i]);
        } else if (lps.empty()) {
            ls.push(a[i]);
            lps.push(a[i - 1]);
        } else if (a[i] == lps.top()) {
            ls.pop();
            lps.pop();
        } else {
            ls.push(a[i]);
            lps.push(a[i - 1]);
        }
    }

    if (lp == -1) {
        fora(x, a) prints(x); newl();
        return;
    }

    fori(i, sz - 1, -1) {
        if (a[i] == 0) {
            rp = i;
            break;
        }

        if (rs.empty()) {
            rs.push(a[i]);
        } else if (rps.empty()) {
            rs.push(a[i]);
            rps.push(a[i + 1]);
        } else if (a[i] == rps.top()) {
            rs.pop();
            rps.pop();
        } else {
            rs.push(a[i]);
            rps.push(a[i + 1]);
        }
    }

    // println(*uv.begin());
    while (ls.top() != rs.top()) {
        // fora(x, a) prints(x); newl();
        if (rs.size() > ls.size()) {
            a[rp] = rps.top();
            rs.pop();
            rps.pop();
            rp--;
            continue;
        }
 
        if (uv.size() && *uv.begin() < lps.top()) {
            a[lp] = *uv.begin();
            uv.erase(a[lp]);
            ls.push(a[lp]);
            lps.push(a[lp - 1]);
            lp++;
        } else {
            a[lp] = lps.top();
            lps.pop();
            ls.pop();
            lp++;
        }
    }

    ll tsz = ls.size();
    while (lp <= rp) {
        // prints(lp); println(rp);
        // fora(x, a) prints(x); newl();

        if (ls.size() == tsz || (uv.size() && *uv.begin() < lps.top())) {
            a[lp] = *uv.begin();
            uv.erase(a[lp]);
            lps.push(a[lp - 1]);
            ls.push(a[lp]);
        } else {
            a[lp] = lps.top();
            lps.pop();
            ls.pop();
        }

        lp++;
    }

    fora(x, a) {
        if (x == 0) assert(0);
    }

    fora(x, a) prints(x); newl();
    return;
}

int main() {
    // std::ios::sync_with_stdio(false);
    // std::cin.tie(nullptr);

    ll t = 1;
    cin >> t;
    while (t--) solve();

    return 0;
}

詳細信息

Test #1:

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

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
Wrong Answer
time: 69ms
memory: 3468kb

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 3 1 2 1 
1 2 3 2 1 
1 3 1 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3 2 1 
1 2 3...

result:

wrong answer 24th lines differ - expected: '1 2 3 2 1', found: '1 3 1 2 1 '