QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#181379 | #6352. SPPPSPSS. | ucup-team859# | WA | 0ms | 3628kb | C++14 | 3.7kb | 2023-09-16 18:10:49 | 2023-09-16 18:10:50 |
Judging History
answer
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
using ull = unsigned long long;
using ll = long long;
using namespace std;
string special_case(int n, vector<int> &a, char ch) {
string s;
vector<int> sl(n + 1, 0), sr(n + 1, 0);
int l = 0, r = n + 1;
int pl = 0, pr = n + 1;
int last = 0;
for (int i = 1; i <= n; ++i) {
s.push_back(ch);
if (ch == 'S') {
ch = 'P';
int rem = 2 - (i == 1);
while (r - 1 >= n - i + 1 && l < r - 1) {
--r;
--rem;
sr[a[r]] = 1;
}
if (last == 0 && rem > 0)
last = rem;
else if (last > 0) {
rem = last + 2;
last += 2;
}
while (rem > 0 && pr > 0) {
if (sl[pr - 1] == 1)
--rem;
--pr;
}
if (l >= pr)
break;
} else {
ch = 'S';
int rem = 2 - (i == 1);
while (l + 1 <= i && l + 1 < r) {
++l;
--rem;
sl[a[l]] = 1;
}
if (last == 0 && rem > 0)
last = rem;
else if (last > 0) {
rem = last + 2;
last += 2;
}
while (rem > 0 && pl <= n) {
if (sr[pl + 1] == 1)
--rem;
++pl;
}
if (pl >= r)
break;
}
}
return s;
}
void solve() {
int n;
cin >> n;
vector<int> a(n + 1, 0), dp(n + 1, 0), p(n + 1, 0);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
p[a[i]] = i;
}
bool ok = true;
for (int i = 1; i < n && ok; ++i) {
if (a[i] > a[i + 1])
ok = false;
}
if (ok) {
cout << ".";
return;
}
dp[n] = 1;
for (int i = n - 1; i >= 1; --i) {
if (a[i] == a[i + 1] - 1)
dp[i] = dp[i + 1] + 1;
else
dp[i] = 1;
}
int mn = n + 1, mx = 1;
// case 1 -> pref x x + 1 ... x + k suf
int ans = n, ls = n, lp = 0;
for (int i = 1; i < n; ++i) {
mx = max(mx, a[i]);
mn = min(mn, a[i]);
if (mx - mn + 1 == i && mn == 1) {
if (a[i + 1] == i + 1) {
int dr = i + dp[i + 1];
int x = max(i, n - dr);
if (i == n - dr)
++x;
if (x < ans) {
ans = x;
lp = i;
ls = n - dr;
if (lp == ls)
++lp;
}
} else {
int x = max(i, n - i);
if (x < ans) {
ans = x;
lp = i;
ls = n - i;
if (lp == ls)
++ls;
}
}
}
}
// case 2 -> pref suf
int last = 0;
for (int i = 1; i < n; ++i) {
while (p[last + 1] <= i)
++last;
int x = max(i, max(i + 1, n - last));
if (i == max(i + 1, n - last))
++x;
if (x < ans) {
ans = x;
lp = i;
ls = max(i + 1, n - last);
if (lp == ls)
++lp;
}
}
// case 3 -> suf pref
last = n + 1;
for (int i = n; i > 1; --i) {
while (p[last - 1] >= i)
--last;
int x = max(i, max(last - 1, i + 1));
if (max(last - 1, i + 1) == n - i + 1)
++x;
if (x < ans) {
ans = x;
lp = max(i + 1, last - 1);
ls = i;
if (lp == ls)
++lp;
}
}
auto s1 = special_case(n, a, 'P');
auto s2 = special_case(n, a, 'S');
if (s1.size() > s2.size())
s1 = s2;
if (s1.size() < ans) {
s1.push_back('.');
cout << s1 << endl;
return;
}
for (int i = 1; i <= ans; ++i) {
if (i == lp)
cout << 'P';
else
cout << 'S';
}
cout << ".";
}
int main() {
#ifdef HOME
ifstream cin("input.in");
ofstream cout("output.out");
#endif
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int t = 1;
//cin >> t;
while (t--)
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3628kb
input:
3 1 2 3
output:
.
result:
ok OK 0 operations
Test #2:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
2 2 1
output:
SS.
result:
ok OK 2 operations
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 3616kb
input:
9 3 2 4 1 5 6 7 9 8
output:
SSP.
result:
wrong answer the permutation is not sorted