QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#137140#6352. SPPPSPSS.berarchegas#WA 2ms11796kbC++174.8kb2023-08-09 23:20:222023-08-09 23:20:23

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-08-09 23:20:23]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:11796kb
  • [2023-08-09 23:20:22]
  • 提交

answer

#include <algorithm>
#include <deque>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <random>
#include <bits/stdc++.h>

using namespace std;
#define F first
#define S second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vl;
typedef vector<pii> vpi;
typedef pair<ll,ll> pll;
typedef vector<string> vs;
typedef vector<pll> vpl;
typedef vector<int> vi;

ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down

#define Unique(v) sort(all(v));v.erase(unique(all(v)),v.end());

const int maxn = 10*100000 + 10;

// const int inf = 1000000000;
// const ll inf = 1000000000000000000LL;

// const ll mod = 998244353;
// const ll mod = 1000000007; // 10^9 + 7

int n;

int freq[maxn];
int v[maxn], aux[maxn], pre[maxn], suf[maxn];

void sortInterval(int l, int r)
{
    for(int i = l ; i <= r ; i++)
        freq[ aux[i] ]++;

    for(int i = 1, p = l ; i <= n ; i++)
        if( freq[i] > 0 ) freq[i]--, aux[p++] = i; 
}

void sortIntervalPre(int l, int r)
{
    for(int i = l ; i <= r ; i++)
        freq[ pre[i] ]++;

    for(int i = 1, p = l ; i <= n ; i++)
        if( freq[i] > 0 ) freq[i]--, pre[p++] = i; 
}

void sortIntervalSuf(int l, int r)
{
    for(int i = l ; i <= r ; i++)
        freq[ suf[i] ]++;

    for(int i = 1, p = l ; i <= n ; i++)
        if( freq[i] > 0 ) freq[i]--, suf[p++] = i; 
}

bool isSorted()
{
    for(int i = 1 ; i <= n ; i++)
        if( aux[i] != i ) return false;

    return true;
}

bool isSortedPre()
{
    for(int i = 1 ; i <= n ; i++)
        if( pre[i] != i ) return false;

    return true;
}

bool isSortedSuf()
{
    for(int i = 1 ; i <= n ; i++)
        if( suf[i] != i ) return false;

    return true;
}

bool test(int k, bool suffix = false)
{
    int c = 2;

    for(int i = 1 ; i <= n ; i++)
        aux[i] = v[i];

    bool t = suffix;

    for(int i = k ; i > 1 && k - i <= c - 1 ; i--, suffix = !suffix)
    {
        if( suffix )
            sortInterval( n - i + 1 , n );
        else
            sortInterval( 1 , i );
    }

    return isSorted();
}

int bs()
{
    int l = 0, r = n / 2 + 1;

    while( l < r )
    {
        int m = ( l + r )/2;

        if( test(m, false) || test(m, true) ) r = m;
        else l = m + 1;
    }

    return r;
}

void solve(int testcase)
{
    cin >> n;

    for(int i = 1 ; i <= n ; i++)
        cin >> v[i];

    int ans = bs();
    if (ans <= n / 2) {
        if (ans == 0) {
            cout << '.' << endl;
        }
        else if (test(ans, false)) {
            for (int i = 0; i < ans - 2; i++) cout << 'P';
            cout << "SP." << endl;
        }
        else {
            for (int i = 0; i < ans - 2; i++) cout << 'S';
            cout << "PS." << endl;
        }
    }
    else {
        for (int i = 1; i <= n; i++) {
            pre[i] = suf[i] = v[i];
        }
        for (int i = n / 2; i <= n; i++) {
            if ((i - n / 2) % 2 == 0) {
                sortIntervalPre(1, i);
                sortIntervalSuf(n - i + 1, n);
            }
            else {
                sortIntervalPre(n - i + 1, n);
                sortIntervalSuf(1, i);
            }
            // for (int j = 1; j <= n; j++) {
            //     cout << pre[j] << ' ';
            // }
            // cout << endl;
            // for (int j = 1; j <= n; j++) {
            //     cout << suf[j] << ' ';
            // }
            // cout << endl;
            if (isSortedPre()) {
                for (int j = 1; j < n / 2; j++) cout << 'S';
                for (int j = n / 2; j <= i; j++) {
                    if ((j - n/2) & 1) cout << 'S';
                    else cout << 'P';
                }
                cout << '.' << endl;
                return;
            }
            if (isSortedSuf()) {
                for (int j = 1; j < n / 2; j++) cout << 'S';
                for (int j = n / 2; j <= i; j++) {
                    if ((j - n/2) & 1) cout << 'S';
                    else cout << 'P';
                }
                cout << '.' << endl;
                return;
            }
        }
    }

}

int main()
{
    ios_base::sync_with_stdio(false);  
    cin.tie(NULL);

    int t = 1;
    // cin >> t;

    for(int testcase = 1 ; testcase <= t ; testcase++)
        solve( testcase );
}

詳細信息

Test #1:

score: 100
Accepted
time: 2ms
memory: 7740kb

input:

3
1 2 3

output:

.

result:

ok OK 0 operations

Test #2:

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

input:

2
2 1

output:

PS.

result:

ok OK 2 operations

Test #3:

score: 0
Accepted
time: 2ms
memory: 9628kb

input:

9
3 2 4 1 5 6 7 9 8

output:

PPSP.

result:

ok OK 4 operations

Test #4:

score: 0
Accepted
time: 2ms
memory: 11796kb

input:

10
2 9 5 7 10 6 3 1 8 4

output:

SSSSPSPS.

result:

ok OK 8 operations

Test #5:

score: -100
Wrong Answer
time: 2ms
memory: 11672kb

input:

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

output:

SSSSSPSP.

result:

wrong answer the permutation is not sorted