QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#186475 | #6352. SPPPSPSS. | ucup-team859 | TL | 154ms | 4712kb | C++17 | 4.0kb | 2023-09-23 22:12:35 | 2023-09-23 22:12:36 |
Judging History
answer
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
using ull = unsigned long long;
using ll = long long;
using namespace std;
inline bool Good(const vector<int>& v) {
for (int i = 1; i < (int)v.size(); i++) {
if (v[i] != i) {
return false;
}
}
return true;
}
constexpr int MAXN = 1e6;
int freq[MAXN + 1];
inline void Sort(vector<int>& v, int l, int r) {
int n = (int)v.size() - 1;
int mn = n + 1, mx = 0;
for (int i = l; i <= r; i++) {
freq[v[i]]++;
mn = min(mn, v[i]);
mx = max(mx, v[i]);
}
int pos = l;
for (int i = mn; i <= mx; i++) {
while (freq[i] > 0) {
v[pos++] = i;
freq[i]--;
}
}
}
void Small(vector<int> p, int n, ostream& cout) {
if (Good(p)) {
cout << "." << endl;
exit(0);
}
auto Check = [&](int len) -> pair<bool, int> {
if (len + len + 1 > n) return {true, -1};
auto temp = p;
Sort(temp, 1, len);
Sort(temp, n - len, n);
if (Good(temp)) {
return {true, 0};
}
temp = p;
Sort(temp, n - len + 1, n);
Sort(temp, 1, len + 1);
if (Good(temp)) {
return {true, 1};
}
return {false, -1};
};
int result = -1;
for (int step = 1 << 20; step; step >>= 1) {
if (!Check(result + step).first) {
result += step;
}
}
result++;
auto curr = Check(result);
if (result + result + 1 <= n && curr.first) {
string str;
for (int i = 1; i <= result + 1; i++) {
if ((result % 2 == 1 && curr.second == 0) || (result % 2 == 0 && curr.second == 1)) {
str += (i % 2 == 1 ? 'P' : 'S');
} else {
str += (i % 2 == 1 ? 'S' : 'P');
}
}
cout << str << "." << endl;
exit(0);
}
}
int Big(vector<int> p, int n, bool isPref) {
if (!isPref) {
for (int i = 1; i <= n; i++) {
p[i] = n - p[i] + 1;
}
reverse(p.begin() + 1, p.end());
}
int len = (n + 1) / 2;
if (len % 2) {
Sort(p, 1, len);
Sort(p, n - len, n);
} else {
Sort(p, n - len + 1, n);
Sort(p, 1, len + 1);
}
int pref = 0;
while (pref + 1 <= n && p[pref + 1] == pref + 1) {
pref++;
}
int suff = 0;
while (suff < n && p[n - suff] == n - suff) {
suff++;
}
if (pref + suff >= n) {
return len + 1;
}
len++;
while (true) {
len++;
if (len & 1) {
Sort(p, pref + 1, len);
} else {
Sort(p, n - len + 1, n - suff);
}
while (pref < n && p[pref + 1] == pref + 1) {
pref++;
}
while (suff < n && p[n - suff] == n - suff) {
suff++;
}
if (pref + suff >= n) {
return len;
}
}
}
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 n;
cin >> n;
// n = MAXN - 1;
vector<int> p(n + 1);
// for (int i = 1; i <= n; i++) {
// p[i] = n - i + 1;
// }
for (int i = 1; i <= n; i++) {
cin >> p[i];
}
Small(p, n, cout);
auto res1 = Big(p, n, true);
auto res2 = Big(p, n, false);
string str;
cerr << res1 << " " << res2 << "\n";
if (res1 < res2) {
for (int i = 0; i < res1; i++) {
if (i % 2 == 0) {
str += 'P';
} else {
str += 'S';
}
}
} else {
for (int i = 0; i < res2; i++) {
if (i % 2 == 1) {
str += 'P';
} else {
str += 'S';
}
}
}
cout << str << ".";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3568kb
input:
3 1 2 3
output:
.
result:
ok OK 0 operations
Test #2:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
2 2 1
output:
SP.
result:
ok OK 2 operations
Test #3:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
9 3 2 4 1 5 6 7 9 8
output:
SPSP.
result:
ok OK 4 operations
Test #4:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
10 2 9 5 7 10 6 3 1 8 4
output:
SPSPSPSP.
result:
ok OK 8 operations
Test #5:
score: 0
Accepted
time: 0ms
memory: 3652kb
input:
13 9 8 5 4 3 2 1 13 12 11 10 7 6
output:
PSPSPSPS.
result:
ok OK 8 operations
Test #6:
score: 0
Accepted
time: 0ms
memory: 3876kb
input:
20 17 6 15 14 4 10 18 7 13 8 2 12 1 19 20 3 11 16 5 9
output:
SPSPSPSPSPSPSP.
result:
ok OK 14 operations
Test #7:
score: 0
Accepted
time: 0ms
memory: 3864kb
input:
100 98 85 81 18 27 10 68 48 19 2 55 51 61 20 91 54 35 22 83 75 8 5 17 23 21 95 37 15 92 50 78 82 44 39 26 87 52 66 70 74 89 4 59 40 12 88 86 43 14 80 53 46 63 3 36 97 60 58 57 96 11 67 99 41 34 47 71 72 73 79 9 94 6 1 77 25 31 7 45 100 90 32 24 13 76 16 93 38 29 69 42 84 30 28 33 56 49 62 64 65
output:
SPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPS.
result:
ok OK 57 operations
Test #8:
score: 0
Accepted
time: 1ms
memory: 3648kb
input:
1000 668 554 210 852 617 846 561 95 341 893 276 224 287 1000 356 362 897 205 369 654 181 590 339 377 346 557 382 593 55 62 126 899 49 509 977 585 614 232 865 800 790 292 219 957 379 914 946 246 294 403 940 517 768 623 376 624 331 353 887 626 424 449 115 628 569 809 956 942 300 894 61 936 678 779 549...
output:
SPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSP...
result:
ok OK 523 operations
Test #9:
score: 0
Accepted
time: 154ms
memory: 4712kb
input:
100000 30619 15529 4854 9258 46894 29948 59533 56945 19473 7608 42291 95532 80537 83105 70434 68130 89221 96367 26768 43837 54765 52814 88446 14950 63224 63479 11957 41446 38702 8466 85556 57724 50097 29014 17961 65178 88627 96815 80115 79096 19625 2979 51033 68224 48649 96250 3406 96433 22584 79610...
output:
SPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSPSP...
result:
ok OK 50224 operations
Test #10:
score: -100
Time Limit Exceeded
input:
999999 1578 253230 300479 227250 673195 324827 649281 5836 935739 543519 370574 305888 969960 550993 463132 799873 408104 207874 140152 331798 698935 749195 181667 828624 142806 789266 207545 870039 88700 189925 952818 455047 184020 276480 107352 733977 864792 660399 680663 667372 602838 139419 6373...