Potted Flower
Description
The little cat takes over the management of a new park. There is a large circular statue in the center of the park, surrounded by N pots of flowers. Each potted flower will be assigned to an integer number (possibly negative) denoting how attractive it is. See the following graph as an example:
(Positions of potted flowers are assigned to index numbers in the range of 1 ... N. The i-th pot and the (i + 1)-th pot are consecutive for any given i (1 <= i < N), and 1st pot is next to N-th pot in addition.) ![]() The board chairman informed the little cat to construct "ONE arc-style cane-chair" for tourists having a rest, and the sum of attractive values of the flowers beside the cane-chair should be as large as possible. You should notice that a cane-chair cannot be a total circle, so the number of flowers beside the cane-chair may be 1, 2, ..., N - 1, but cannot be N. In the above example, if we construct a cane-chair in the position of that red-dashed-arc, we will have the sum of 3+(-2)+1+2=4, which is the largest among all possible constructions. Unluckily, some booted cats always make trouble for the little cat, by changing some potted flowers to others. The intelligence agency of little cat has caught up all the M instruments of booted cats' action. Each instrument is in the form of "A B", which means changing the A-th potted flowered with a new one whose attractive value equals to B. You have to report the new "maximal sum" after each instruction. Input
There will be a single test data in the input. You are given an integer N (4 <= N <= 100000) in the first input line.
The second line contains N integers, which are the initial attractive value of each potted flower. The i-th number is for the potted flower on the i-th position. A single integer M (4 <= M <= 100000) in the third input line, and the following M lines each contains an instruction "A B" in the form described above. Restriction: All the attractive values are within [-1000, 1000]. We guarantee the maximal sum will be always a positive integer. Output
For each instruction, output a single line with the maximum sum of attractive values for the optimum cane-chair.
Sample Input 5 3 -2 1 2 -5 4 2 -2 5 -5 2 -4 5 -1 Sample Output 4 4 3 5 Source
POJ Monthly--2006.01.22,Zeyuan Zhu
|
提示
题意:
给出n个数的环,每次对环上的一点做更新,每次更新输出每次环上最大连续数列和,但不能全部选。
思路:
链接:http://blog.csdn.net/lin375691011/article/details/19932765
有点动态规划的味道,其中为什么要有从左,右开始最大(最小)连续序列值?
1.线段树存的是线段,题中所给的是环,会只有两段而没有中间的情况。
2.线段树是以二分的形式存的数据,不仅只有左边区域或右边区域的情况,还有左边一部分加上右边的一部分的情况。
示例程序
写的比较臃肿:
Source Code
Problem: 2750 Code Length: 2294B
Memory: 9628K Time: 641MS
Language: GCC Result: Accepted
#include <stdio.h>
struct
{
int minnum,maxnum,sum,maxl,maxr,minl,minr,minsum,maxsum;
}line[400000];//最小值、最大值、和、左边最大连续、右边最大连续、左边最小连续、右边最小连续、当前总的最大连续、当前总的最小连续
int max(int a,int b)
{
if(a>b)
{
return a;
}
else
{
return b;
}
}
int min(int a,int b)
{
if(a<b)
{
return a;
}
else
{
return b;
}
}
void init(int t,int x)
{
line[t-1].minnum=x;
line[t-1].maxnum=x;
line[t-1].sum=x;
line[t-1].maxl=x;
line[t-1].maxr=x;
line[t-1].minl=x;
line[t-1].minr=x;
line[t-1].minsum=x;
line[t-1].maxsum=x;
}
void pushup(int t)
{
line[t-1].sum=line[t*2-1].sum+line[t*2].sum;
line[t-1].maxnum=max(line[t*2-1].maxnum,line[t*2].maxnum);
line[t-1].minnum=min(line[t*2-1].minnum,line[t*2].minnum);
line[t-1].maxl=max(line[t*2-1].sum+line[t*2].maxl,line[t*2-1].maxl);
line[t-1].maxr=max(line[t*2].sum+line[t*2-1].maxr,line[t*2].maxr);
line[t-1].minl=min(line[t*2-1].sum+line[t*2].minl,line[t*2-1].minl);
line[t-1].minr=min(line[t*2].sum+line[t*2-1].minr,line[t*2].minr);
line[t-1].maxsum=max(max(line[t*2-1].maxsum,line[t*2].maxsum),line[t*2-1].maxr+line[t*2].maxl);
line[t-1].minsum=min(min(line[t*2-1].minsum,line[t*2].minsum),line[t*2-1].minr+line[t*2].minl);
}
void bulid(int t,int l,int r)
{
int mid,x;
if(l==r)
{
scanf("%d",&x);
init(t,x);
return;
}
mid=(l+r)/2;
bulid(t*2,l,mid);
bulid(t*2+1,mid+1,r);
pushup(t);
}
void update(int pos,int t,int l,int r,int x)
{
int mid;
if(l==r)
{
init(t,x);
return;
}
mid=(l+r)/2;
if(mid>=pos)
{
update(pos,t*2,l,mid,x);
}
else
{
update(pos,t*2+1,mid+1,r,x);
}
pushup(t);
}
int output()
{
if(line[0].maxsum<line[0].sum-line[0].minsum)
{
line[0].maxsum=line[0].sum-line[0].minsum;
}
else if(line[0].maxsum==line[0].sum)
{
return line[0].sum-line[0].minnum;
}
else if(line[0].minsum==line[0].sum)
{
return line[0].maxnum;
}
return line[0].maxsum;
}
int main()
{
int n,i,m,pos,x;
scanf("%d",&n);
bulid(1,1,n);
scanf("%d",&m);
for(i=1;m>=i;i++)
{
scanf("%d %d",&pos,&x);
update(pos,1,1,n,x);
printf("%d\n",output());
}
return 0;
}