QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#487425#6352. SPPPSPSS.ucup-team2307#WA 1ms3628kbC++203.2kb2024-07-22 21:30:162024-07-22 21:30:19

Judging History

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

  • [2024-07-22 21:30:19]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3628kb
  • [2024-07-22 21:30:16]
  • 提交

answer

#pragma GCC optimize("Ofast,unroll-loops")
#include<bits/stdc++.h>
#pragma GCC target("avx,avx2,sse,sse2")
#define all(x) begin(x), end(x)
using namespace std;
using ll = long long;

int solve(vector<int> p) {
    int n = p.size();

    vector<int> prefMax = p, sufMin = p, fixed(n);
    for(int i = 1; i < n; i++)
        prefMax[i] = max(prefMax[i - 1], p[i]);
    for(int i = n - 1; i--;)
        sufMin[i] = min(sufMin[i + 1], p[i]);
    for(int i = 0; i < n; i++) {
        fixed[i] = p[i] == i;
        if(i) fixed[i] += fixed[i - 1];
    }

    vector<int> mid_p = p;
    int mid_op = 1;
    while(mid_op + (mid_op + 1) <= n) mid_op++;
    {
        int pref = mid_op, suf = mid_op;
        pref -= mid_op%2 == 0;//must be odd
        suf -= mid_op%2 == 1;//must be even
        if(pref < mid_op) {//prefix first
            sort(mid_p.begin(), mid_p.begin() + pref);
            sort(mid_p.end() - suf, mid_p.end());
        } else {//sufix first
            sort(mid_p.end() - suf, mid_p.end());
            sort(mid_p.begin(), mid_p.begin() + pref);
        }
    }

    if(fixed.back() == n) return 0;

    auto check = [&](int ops) -> bool {//PSPSPSPS
        if(ops < 2) return 0;
        // cout << "CHECK" << ops << endl;
        int pref = ops, suf = ops;
        pref -= ops%2 == 0;//must be odd
        suf -= ops%2 == 1;//must be even
        if(ops <= mid_op) {
            int ok = 1;
            // for(auto i : p) cout << i << " "; cout << endl;
            ok &= prefMax[pref - 1] == (pref - 1);
            ok &= sufMin[n - suf] == (n - suf);
            // cout << prefMax[pref - 1] << " " << pref - 1 << " " << ok << endl;
            // cout << sufMin[n - suf] << " " << n - suf << " " << ok << endl;
            for(int i = pref + 1; i < n - suf; i++) ok &= p[i] == i;
            // cout << pref << " " << suf << " " << ok << endl;
            return ok;
        }
        int cut;//1 is >=cut
        if(pref == ops) {//last is P
            cut = pref;
        } else {
            cut = n - suf + 1;
        }
        vector<int> segs;
        int last = -1;
        for(auto i : mid_p) {
            i = i >= cut;
            if(last != i) segs.push_back(1);
            else segs.back()++;
            last = i;
        }
        if(segs.size() < 3) return 1;
        segs.resize(4);
        for(int op = mid_op + 1; op <= ops; op++) {
            if(op % 2 == 1) {//prefix
                while(segs[0] + segs[1] < op && segs[2])
                    segs[2]--, segs[0]++;
            } else {//suffix
                while(segs[3] + segs[2] < op && segs[1])
                    segs[1]--, segs[3]++;
            }
        }
        return (segs[0] == 0 || segs[2] == 0) && (segs[1] == 0 || segs[3] == 0);
    };
    int ans = -1;
    for(int z = 1<<20; z>>=1;)
        ans += z*!check(ans+z);
    return ans + 1;
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
    int n;
    cin >> n;
    vector<int> p(n);
    for(auto &i : p) cin >> i, i--;
    int cnt1 = solve(p);
    
    reverse(all(p));
    for(auto &i : p) i = n-1-i;
    int cnt2 = solve(p);

    string pat = "PS";
    if(cnt2 < cnt1) cnt1 = cnt2, pat = "SP";
    for(int i = 0; i < cnt1; i++) cout << pat[i&1];cout << ".\n";
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3552kb

input:

3
1 2 3

output:

.

result:

ok OK 0 operations

Test #2:

score: 0
Accepted
time: 1ms
memory: 3528kb

input:

2
2 1

output:

PS.

result:

ok OK 2 operations

Test #3:

score: 0
Accepted
time: 1ms
memory: 3628kb

input:

9
3 2 4 1 5 6 7 9 8

output:

SPSP.

result:

ok OK 4 operations

Test #4:

score: 0
Accepted
time: 1ms
memory: 3544kb

input:

10
2 9 5 7 10 6 3 1 8 4

output:

PSPSPSPS.

result:

ok OK 8 operations

Test #5:

score: -100
Wrong Answer
time: 1ms
memory: 3604kb

input:

13
9 8 5 4 3 2 1 13 12 11 10 7 6

output:

PSPSPSPSP.

result:

wrong answer Jury (8) found answer better than participant (9)