POJ 2750 Potted Flower (线段树+动归)

Potted Flower
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 4765 Accepted: 1814

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


题目大意:

题意:给出一数组,数组首尾是可以相接的,要求求出最大连续序列值,并且不可以包括所有元素值。

遇到求这种最大连续序列值,看了网上大牛的思想才知道一般分为两种情况:1、不同时包括两端点的情况,这时直接求整个序列中的最大连续序列值;2、同时包括两端点元素,这时求整个序列中的最小连续序列值,然后用总和减去它,就是所要求的值。

具体怎么求法呢?

假设我们将整个序列分成两个连续的序列a,b;与整个序列设成A。假如我们知道a,b序列各个的从左向右最大连续序列值lmax,从右往左的最大连续序列值rmax,和从左向右最小连续序列值lmin,从右往左的最小连续序列值rmin,和每个序列的最大连续序列值nmax和最小连续序列值nmin,和最大元素值max,和最小元素值min;

那么,就有:

A.nmax=max(a.nmax,b.max,a.rmax+b.lmax,);

A.lmax=max(a.sum+b.lmax,a.max);

A.rmax=max(b.sum+a.rmax,b.rmax);

A.lmin=min(a.sum+b.lmin,a.lmin);

A.rmin=min(b.sum+a.rmin,b.rmin);

A.min=min(a.min,b.min) ;

A.max=max(a.max,b.max) ;

现在的A.nmax并不是最终结果,因为还没考虑最大连续序列值存在两端的情况,//全部的和减去最小和与当前求得的最大相比较去大值

A.nmax=max(A.nmax,A.sum-(a.rmin+b.lmin))

这是不是最终的答案呢? 不是,因为没有考虑全为正数和全为负数的情况,所以这种情况下

A.nmax=A.namx-A.min       //全为正数

  A.nmax=A.max ;               //全为负数时去N个数中的最大值

AC代码

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int M=112345;
int a[M];
struct node
{
    int lmax,lmin;
    int rmax,rmin;
    int sum;
    int maxd,mind;
    int ma,mi;
    int l,r;
} p[M<<2];
void init(int k,int root)
{
    p[root].lmax=p[root].lmin=p[root].rmax=p[root].rmin=p[root].sum=p[root].maxd=p[root].mind=a[k];
    p[root].l=p[root].r=k;
    p[root].ma=p[root].mi=a[k];
}
void pushup(int root)
{
    int x=root<<1;
    int y=root<<1|1;

    p[root].l=p[x].l;
    p[root].r=p[y].r;

    p[root].sum=p[x].sum+p[y].sum;

    p[root].lmax=max(p[x].sum+p[y].lmax,p[x].lmax);
    p[root].rmax=max(p[y].sum+p[x].rmax,p[y].rmax);

    p[root].lmin=min(p[x].sum+p[y].lmin,p[x].lmin);
    p[root].rmin=min(p[y].sum+p[x].rmin,p[y].rmin);

    p[root].maxd=max(p[x].maxd,max(p[y].maxd,p[x].rmax+p[y].lmax));
    p[root].mind=min(p[x].mind,min(p[y].mind,p[x].rmin+p[y].lmin));

    p[root].ma=max(p[x].ma,p[y].ma);
    p[root].mi=min(p[x].mi,p[y].mi);

}
void build(int b,int e,int root)
{
    if(b==e)
    {
        init(b,root);
        return ;
    }
    int mid=(b+e)/2;
    build(b,mid,root<<1);
    build(mid+1,e,root<<1|1);
    pushup(root);
}
void updata(int root,int n,int k)
{
    if(p[root].l==p[root].r&&p[root].l==n)
    {
        p[root].lmax=p[root].lmin=p[root].rmax=p[root].rmin=p[root].sum=p[root].maxd=p[root].mind=k;
        p[root].ma=p[root].mi=k;
        return ;
    }
    int mid=(p[root].l+p[root].r)/2;
    if(mid>=n)
        updata(root<<1,n,k);
    else updata(root<<1|1,n,k);
    pushup(root);
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1; i<=n; i++)scanf("%d",&a[i]);
        build(1,n,1);
        int m;
        scanf("%d",&m);

        while(m--)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            updata(1,x,y);
            //printf("sum=%d max=%d min=%d\n",p[1].sum,p[1].maxd,p[1].mind);
            int ans;

            if(p[1].sum-p[1].mind>p[1].maxd)
            {
                p[1].maxd=p[1].sum-p[1].mind;
            }
            if(p[1].sum==p[1].maxd)//全正数
            {
                ans=p[1].sum-p[1].mi;
            }
            else if(p[1].sum==p[1].mind)//全负数
            {
                ans=p[1].ma;
            }
            else     //正负数
                ans=p[1].maxd;
            printf("%d\n",ans);
        }
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值