You are given an array consisting of n non-negative integers a1, a2, ..., an.
You are going to destroy integers in the array one by one. Thus, you are given the permutation of integers from 1 to n defining the order elements of the array are destroyed.
After each element is destroyed you have to find out the segment of the array, such that it contains no destroyed elements and the sum of its elements is maximum possible. The sum of elements in the empty segment is considered to be 0.
The first line of the input contains a single integer n (1 ≤ n ≤ 100 000) — the length of the array.
The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109).
The third line contains a permutation of integers from 1 to n — the order used to destroy elements.
Print n lines. The i-th line should contain a single integer — the maximum possible sum of elements on the segment containing no destroyed elements, after first i operations are performed.
4 1 3 2 5 3 4 1 2
5 4 3 0
5 1 2 3 4 5 4 2 3 5 1
6 5 5 1 0
8 5 5 4 4 6 6 5 5 5 2 8 7 1 3 4 6
18 16 11 8 8 6 6 0
Consider the first sample:
- Third element is destroyed. Array is now 1 3 * 5. Segment with maximum sum 5 consists of one integer 5.
- Fourth element is destroyed. Array is now 1 3 * * . Segment with maximum sum 4 consists of two integers 1 3.
- First element is destroyed. Array is now * 3 * * . Segment with maximum sum 3 consists of one integer 3.
- Last element is destroyed. At this moment there are no valid nonempty segments left in this array, so the answer is equal to 0.
题目大意:
给你长度为N的一个序列,其一开始所有数都在其对应的位子上,再给你一个长度为N的序列,表示第i次将删除哪个位子的数。每次操作之后,输出连续的序列中最大的和。
分析样例:
1 3 2 5
3 4 1 2
5
4
3
0
第一次删除第三个位子的数据后序列变成:
1 3 X 5
第一段连续的序列的和为4,第二段连续的序列的和为5,那么输出5
第二次删除第四个位子的数据后序列变成:
1 3 X X
只有一段连续的序列的和为4,输出4.
第三次删除第一个位子的数据后序列变成:
X 3 X X ,只有一段连续的序列的和为3,输出3。
最后一次删除之后,序列为空,输出0。
思路:
1、首先我们往下删除数据相对而言想起来比较难处理,难维护,那么我们逆序来想,从一个空的序列,向一个满的序列去加。
2、那么我们将每个点都先看成是孤立的,现在一共N个点,并且设定一个数组vis【i】,表示第i个数(点),是否已经加上了对应位子的数据。
那么对应我们将一个点的数据加上了之后:vis【b【i】】=1;表示这个数已经加上了对应位子的数据。然后判断其左边的数据是否也加上了(vis【b【i】-1】),如果vis【b【i】-1】==1,那么merge(b【i】,b【i】-1),表示将两个点并在一起,形成一段连续的序列,那么其序列和的确定问题呢?
我们再设定一个ji【i】,表示以i为根的集合的权值和,其实就是说是以i为根的序列的价值和。那么在merge(b【i】,b【i】-1)的时候,对应如果:
f【find(b【i】)】=find(b【i】-1),那么对应要有:ji【find(b【i】-1)】+=ji【find(b【i】)】;
3、那么每一次将操作进行完毕之后,我们维护要输出的解ans=max(ans,ji【find(b【i】)】)即可。
具体细节参考Ac代码:
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define ll __int64
ll ji[1000000];
ll b[1000000];
int vis[1000000];
int f[1000000];
ll output[1000000];
int find(int a)
{
int r=a;
while(f[r]!=r)
r=f[r];
int i=a;
int j;
while(i!=r)
{
j=f[i];
f[i]=r;
i=j;
}
return r;
}
void merge(int a,int b)
{
int A,B;
A=find(a);
B=find(b);
if(A!=B)
{
f[B]=A;
ji[A]+=ji[B];
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)f[i]=i;
for(int i=1;i<=n;i++)
{
scanf("%I64d",&ji[i]);
}
for(int i=1;i<=n;i++)
{
scanf("%I64d",&b[i]);
}
ll ans=0;
int contz=0;
for(int i=n;i>=1;i--)
{
output[contz++]=ans;
vis[b[i]]=1;
if(b[i]-1>=1&&vis[b[i]-1]==1)
{
merge(b[i],b[i]-1);
}
if(b[i]+1<=n&&vis[b[i]+1]==1)
{
merge(b[i],b[i]+1);
}
ans=max(ji[find(b[i])],ans);
}
for(int i=contz-1;i>=0;i--)
{
printf("%I64d\n",output[i]);
}
}
}