(POJ2750)Potted Flower <环形最大连续子段和 线段树单点更新>

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个节点的环,每个节点有一个数值,问这个环的最大真子段和的最大值?

思路分析:
首先我们来复习一下以前写过的条形的最大连续子段和的问题的解法:

int MaxSubSum(int *a,int left,int right)
{   
    int sum = 0;
    if(left == right)
    {
        sum = a[left]>0?a[left]:0;
    }
    else
    {
        int center = (left+right)/2;
        int leftsum = MaxSubSum(a,left,center);
        int rightsum = MaxSubSum(a,center+1,right);

        int s1 = 0;
        int lefts = 0;
        for(int i=center; i>=left;i--)
        {
            lefts += a[i];
            if(lefts>s1)
            {
                s1=lefts;
            }
        }

        int s2 = 0;
        int rights = 0;
        for(int i=center+1; i<=right;i++)
        {
            rights += a[i];
            if(rights>s2)
            {
                s2=rights;
            }
        }
        sum = s1+s2;
        if(sum<leftsum)
        {
            sum = leftsum;
        }
        if(sum<rightsum)
        {
            sum = rightsum;
        }

    }
    return sum;
}

我们用的分治法。那么这一题和这个有什么不同呢?
首先他是环形的(不喜欢),所以我们先把环变成一条链,求链上的最大子段和。这时会有两点要注意:1 题目是环型的, 2 题目要求是真子段和,

对于第1点在环形时最大子段和就等于:max(链型的最大子段和,和减去最小子段和)

对于第2点,只要节点不都是正数,那么我们要求的就是环形上的最大值。
特殊情况: 要是全是正数,那么就等于所有的和减去最小子段和

其次,题目有更新操作。要是按照上面的代码每次求出来,那一定会超时。(其实上面代码的求法也是一颗树,只是没有明显的表示出来),所以关于点更新问题,我们只需要利用线段树来进行维护就可以了。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

const int maxn = 100010;
int a[maxn];
int n,m;
struct node
{
    int L,R,sum;
    int lmax,rmax;
    int lmin,rmin;
    int mmax,mmin;
}t[maxn<<2];

void pushup(int id)
{
    t[id].sum = t[id<<1].sum + t[id<<1|1].sum;
    t[id].lmax = max(t[id<<1].lmax,t[id<<1].sum+t[id<<1|1].lmax);
    t[id].lmin = min(t[id<<1].lmin,t[id<<1].sum+t[id<<1|1].lmin);
    t[id].rmax = max(t[id<<1|1].rmax,t[id<<1|1].sum+t[id<<1].rmax);
    t[id].rmin = min(t[id<<1|1].rmin,t[id<<1|1].sum+t[id<<1].rmin);
    t[id].mmax = max(max(t[id<<1].mmax,t[id<<1|1].mmax),t[id<<1].rmax+t[id<<1|1].lmax);
    t[id].mmin = min(min(t[id<<1].mmin,t[id<<1|1].mmin),t[id<<1].rmin+t[id<<1|1].lmin);
}

void build(int id,int l,int r)
{
    t[id].L = l; t[id].R = r;
    if(l == r)
    {
        t[id].lmax = t[id].lmin = t[id].mmax = t[id].mmin = t[id].rmax = t[id].rmin = t[id].sum = a[l];
        return ;
    }
    int mid = (l + r)>>1;
    build(id<<1,l,mid);
    build(id<<1|1,mid+1,r);
    pushup(id);
}

void update(int id,int l,int r,int pos,int val)
{
    if(l == r)
    {
        t[id].lmax = t[id].lmin = t[id].mmax = t[id].mmin = t[id].rmax = t[id].rmin = t[id].sum = val;
        return;
    }
    int mid = (l + r)>>1;
    if(pos <= mid) update(id<<1,l,mid,pos,val);
    else update(id<<1|1,mid+1,r,pos,val);
    pushup(id);
}


int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
        build(1,1,n);
        scanf("%d",&m);
        while(m--)
        {
            int pos, val;
            scanf("%d%d",&pos,&val);
            update(1,1,n,pos,val);
            if(t[1].mmax != t[1].sum)
                printf("%d\n",max(t[1].mmax,t[1].sum-t[1].mmin));
            else printf("%d\n",t[1].sum-t[1].mmin);
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值