QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#101876 | #6352. SPPPSPSS. | zhouhuanyi# | WA | 3ms | 5412kb | C++11 | 2.7kb | 2023-05-01 15:42:08 | 2023-05-01 15:42:12 |
Judging History
answer
#include<iostream>
#include<cstdio>
#include<set>
#include<algorithm>
#include<cmath>
#define N 1000001
using namespace std;
int read()
{
char c=0;
int sum=0;
while (c<'0'||c>'9') c=getchar();
while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
return sum;
}
int n,ans1,ans2,p[N+1],q[N+1],maxn[N+1],minn[N+1];
set<int>s1;
set<int>s2;
void solve(int x,int type)
{
if ((x&1)==type) sort(q+1,q+x+1);
else sort(q+n-x+1,q+n+1);
return;
}
int main()
{
int l,r;
bool op=1;
n=read(),minn[n+1]=n+1;
for (int i=1;i<=n;++i) p[i]=read(),op&=(p[i]==i);
if (op)
{
puts(".");
return 0;
}
for (int i=1;i<=n;++i) maxn[i]=max(maxn[i-1],p[i]);
for (int i=n;i>=1;--i) minn[i]=min(minn[i+1],p[i]);
for (int i=1;i<=(n>>1);++i)
{
if (maxn[i]==i&&minn[n-i+2]==n-i+2)
{
for (int j=1;j<=i;++j) printf(j&1?"P":"S");
puts(".");
return 0;
}
if (maxn[i]==i-1&&minn[n-i+1]==n-i+1)
{
for (int j=1;j<=i;++j) printf(j&1?"S":"P");
puts(".");
return 0;
}
}
l=max((n>>1)-(int)(ceil(sqrt(n))),1),r=min(((n+1)>>1)+(int)(ceil(sqrt(n))),n);
//0
for (int i=1;i<=n;++i) q[i]=p[i];
if ((n>>1)-1>=1) solve((n>>1)-1,1);
if ((n>>1)>=1) solve(n>>1,1);
for (int i=1;i<=l-1;++i) s1.insert(q[i]);
for (int i=r+1;i<=n;++i) s2.insert(q[i]);
for (int i=(n>>1)+1;i<=(n>>1)+sqrt(n);++i)
{
if (i&1)
{
for (int j=l;j<=i;++j) s1.insert(q[j]);
for (int j=i;j>=l;--j)
{
auto it=s1.end();
it--,q[j]=(*it),s1.erase(it);
}
}
else
{
for (int j=n-i+1;j<=r;++j) s2.insert(q[j]);
for (int j=n-i+1;j<=r;++j) q[j]=(*s2.begin()),s2.erase(s2.begin());
}
op=1;
for (int j=l;j<=r;++j) op&=(q[j]==j);
if (op)
{
ans1=i;
break;
}
}
//1
s1.clear(),s2.clear();
for (int i=1;i<=n;++i) q[i]=p[i];
if ((n>>1)-1>=1) solve((n>>1)-1,0);
if ((n>>1)>=1) solve(n>>1,0);
for (int i=1;i<=l-1;++i) s1.insert(q[i]);
for (int i=r+1;i<=n;++i) s2.insert(q[i]);
for (int i=(n>>1)+1;i<=(n>>1)+sqrt(n);++i)
{
if (!(i&1))
{
for (int j=l;j<=i;++j) s1.insert(q[j]);
for (int j=i;j>=l;--j)
{
auto it=s1.end();
it--,q[j]=(*it),s1.erase(it);
}
}
else
{
for (int j=n-i+1;j<=r;++j) s2.insert(q[j]);
for (int j=n-i+1;j<=r;++j) q[j]=(*s2.begin()),s2.erase(s2.begin());
}
op=1;
for (int j=l;j<=r;++j) op&=(q[j]==j);
if (op)
{
ans2=i;
break;
}
}
if (ans1<ans2)
{
for (int j=1;j<=ans1;++j) printf(j&1?"P":"S");
puts(".");
}
else
{
for (int j=1;j<=ans2;++j) printf(j&1?"S":"P");
puts(".");
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 3ms
memory: 5388kb
input:
3 1 2 3
output:
.
result:
ok OK 0 operations
Test #2:
score: 0
Accepted
time: 2ms
memory: 5412kb
input:
2 2 1
output:
SP.
result:
ok OK 2 operations
Test #3:
score: -100
Wrong Answer
time: 0ms
memory: 5396kb
input:
9 3 2 4 1 5 6 7 9 8
output:
PSPS.
result:
wrong answer the permutation is not sorted