Description
Input
Output
Sample Input
5
1 2 3 4 5
Sample Output
5 -1 4 -2 3
Data Constraint
Thinking
cdq partition
Assume a left election S1, S2 choose the right one can make the best answer.
Now we ask for the length of the answer to S + 1, selected from the left is assumed S1 'a right election S2' one can make the best answer.
Found S1 'and S1; S2' and S2 are some links. Their difference is not more than three.
Code
#include<bits/stdc++.h>
#define maxn 100077
#define inf 2e9
#define ll long long
using namespace std;
int n,a[maxn],lim1,lim2,cc1,cc2;
int f[maxn][2],u[maxn][2],v[maxn][2],w[maxn][2];
int get0(int t1,int t2)
{
if(t1<0||t1>lim1||t2<0||t2>lim2) return -inf;
return u[t1][0]+v[t2][(t1&1)];
}
int get1(int t1,int t2)
{
if(t1<0||t1>lim1||t2<0||t2>lim2) return -inf;
return u[t1][1]+v[t2][(t1&1)^1];
}
void cdq(int l,int r)
{
if(l==r)
{
f[l][0]=a[l],f[l][1]=-a[l]; return;
}
int mid=(l+r)/2;
cdq(l,mid),cdq(mid+1,r);
for(int i=l; i<=mid; i++) u[i-l+1][0]=f[i][0],u[i-l+1][1]=f[i][1];
for(int i=mid+1; i<=r; i++) v[i-mid][0]=f[i][0],v[i-mid][1]=f[i][1];
lim1=mid-l+1,lim2=r-mid;
int c1=0,c2=0;
while(c1+c2<r-l+1)
{
w[c1+c2+1][0]=-inf;
for(int d=0; d<4; d++)
{
int tmp=get0(c1+2-d,c2-1+d);
if(tmp>w[c1+c2+1][0])
cc1=c1+2-d,cc2=c2-1+d,w[c1+c2+1][0]=tmp;
}
c1=cc1,c2=cc2;
}
if(l==4&&r==5)
{
printf("");
}
c1=0,c2=0;
while(c1+c2<r-l+1)
{
w[c1+c2+1][1]=-inf;
for(int d=0; d<4; d++)
{
int tmp=get1(c1+2-d,c2-1+d);
if(tmp>w[c1+c2+1][1])
cc1=c1+2-d,cc2=c2-1+d,w[c1+c2+1][1]=tmp;
}
c1=cc1,c2=cc2;
}
for(int i=l; i<=r; i++) f[i][0]=w[i-l+1][0],f[i][1]=w[i-l+1][1];
}
int main()
{
freopen("pe.in","r",stdin); freopen("pe.out","w",stdout);
scanf("%d",&n);
for(int i=1; i<=n; i++) scanf("%d",&a[i]);
cdq(1,n);
for(int i=1; i<=n; i++) printf("%d ",f[i][0]);
}