QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#99991 | #6352. SPPPSPSS. | Wolam | WA | 2ms | 3440kb | C++14 | 3.0kb | 2023-04-24 12:14:40 | 2023-04-24 12:14:41 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int M=1e9+7;
int n,a[100005],ll,rr;
int b[100005];
string ans1,ans2;
void presort(int x,int *a)
{
if(x>ll)
sort(a+ll,a+x+1);
}
void sufsort(int x,int *a)
{
//if(x<=n)cerr<<x<<endl;
if(n-x+1<rr)
sort(a+n-x+1,a+rr+1);
}
void presort(int x)
{
if(x==1)return;
if(x==2)
{
if(b[x]<b[x-1])swap(b[x],b[x-1]);
return;
}
//cerr<<"(";
int y=x-2;
int a=b[y+1];
for(int i=y;i>=1&&b[i]>a;i--)
{
b[i+1]=b[i];
if(i==1||b[i-1]<=a)b[i]=a;
}
y++;
a=b[y+1];
for(int i=y;i>=1&&b[i]>a;i--)
{
b[i+1]=b[i];
if(i==1||b[i-1]<=a)b[i]=a;
}
//cerr<<")";
}
void sufsort(int x)
{
if(x==n)return;
if(x==n-1)
{
if(b[x]>b[x+1])swap(b[x],b[x+1]);
return;
}
// cerr<<"(";
int y=x+2;
int a=b[y-1];
for(int i=y;i<=n&&b[i]<a;i++)
{
b[i-1]=b[i];
if(i==n||b[i+1]>=a)b[i]=a;
}
y--;
a=b[y-1];
for(int i=y;i<=n&&b[i]<a;i++)
{
b[i-1]=b[i];
if(i==n||b[i+1]>=a)b[i]=a;
}
// cerr<<")";
}
bool check()
{
while(ll<=rr)
{
if(b[ll]==ll)
ll++;
else break;
}
while(ll<=rr)
{
if(b[rr]==rr)
rr--;
else break;
}
return ll>rr;
}
void solpre()
{
int t=(n+1)/2;
for(int i=1;i<=n;i++)
b[i]=a[i];
presort(t,b);
sufsort(t-1,b);
ll=1;rr=n;
if(!check())
{
for(int i=1;i<=t;i++)
if((i+t)&1)ans1+='S';
else ans1+='P';
//cerr<<t<<endl;
int i=t+1;
while(!check())
{
if((i+t)&1)sufsort(i,b),ans1+='S';
else presort(i,b),ans1+='P';
i++;
}
}
else
{
int now=0;
for(int i=t;i>=1;i--)
{
if((i+t)&1)
{
if(a[n-i+1]!=n-i+1||a[n-i+2]!=n-i+2)
{
now=i;
break;
}
}
else if(a[i]!=i||a[i-1]!=i-1)
{
now=i;
break;
}
}
for(int i=1;i<=now;i++)
if((i+t)&1)ans1+='S';
else ans1+='P';
}
}
void solsuf()
{
int t=(n+1)/2;
for(int i=1;i<=n;i++)
b[i]=a[i];
presort(t-1,b);
sufsort(t,b);
ll=1;rr=n;
if(!check())
{
for(int i=1;i<=t;i++)
if((i+t)&1)ans2+='P';
else ans2+='S';
int i=t+1;
while(!check()&&ans2.size()<ans1.size())
{
if((i+t)&1)presort(i,b),ans2+='P';
else sufsort(i,b),ans2+='S';
/* if(i<=n)
{
for(int i=1;i<=n;i++)
cerr<<b[i]<<" ";
cerr<<endl;
}*/
i++;
// cerr<<i<<endl;
}
}
else
{
int now=0;
for(int i=t;i>=1;i--)
{
if((i+t)&1)
{
if(a[i]!=i||a[i-1]!=i-1)
{
now=i;
break;
}
}
else if(a[n-i+1]!=n-i+1||a[n-i+2]!=n-i+2)
{
now=i;
break;
}
}
for(int i=1;i<=now;i++)
if((i+t)&1)ans2+='P';
else ans2+='S';
}
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
}
a[n+1]=n+1;
solpre();
solsuf();
for(int i=1;i<=n;i++)
if(i&1)sufsort(i,a);
else presort(i,a);
// for(int i=1;i<=n;i++)
// cout<<a[i]<<" ";
if(ans1.size()<=ans2.size())
cout<<ans1;
else cout<<ans2;
cout<<'.';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3440kb
input:
3 1 2 3
output:
.
result:
ok OK 0 operations
Test #2:
score: 0
Accepted
time: 2ms
memory: 3404kb
input:
2 2 1
output:
PS.
result:
ok OK 2 operations
Test #3:
score: -100
Wrong Answer
time: 2ms
memory: 3400kb
input:
9 3 2 4 1 5 6 7 9 8
output:
PSPSPS.
result:
wrong answer Jury (4) found answer better than participant (6)