zoukankan      html  css  js  c++  java
  • POJ 2750 Potted Flower

    Potted Flower
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 3872   Accepted: 1446

    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
      
     线段树+dp 感觉好经典啊  log(n)就能求出一串数字的最大的连续和
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    #define N 100010
    using namespace std;
    struct num
    {
        int l,r,sum,maxsum,minsum,maxl,maxr,minl,minr;
    }a[4*N];
    int b[N];
    int main()
    {
        //freopen("data.in","r",stdin);
        void init(int k,int l,int r);
        void update(int k,int pos,int val);
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            for(int i=1;i<=n;i++)
            {
                scanf("%d",&b[i]);
            }
            init(1,1,n);
            int m;
            scanf("%d",&m);
            while(m--)
            {
                int x,y;
                scanf("%d %d",&x,&y);
                update(1,x,y);
                if(a[1].sum==a[1].maxsum)
                {
                    printf("%d
    ",a[1].sum-a[1].minsum);
                }else
                {
                    printf("%d
    ",max(a[1].maxsum,a[1].sum-a[1].minsum));
                }
            }
        }
        return 0;
    }
    void pushup(int k)
    {
        int left=k<<1,right=k<<1|1;
        a[k].sum=a[left].sum+a[right].sum;
        a[k].maxsum=max(max(a[left].maxsum,a[right].maxsum),a[left].maxr+a[right].maxl);
        a[k].minsum=min(min(a[left].minsum,a[right].minsum),a[left].minr+a[right].minl);
        a[k].maxl=max(a[left].maxl,a[left].sum+a[right].maxl);
        a[k].maxr=max(a[right].maxr,a[right].sum+a[left].maxr);
        a[k].minl=min(a[left].minl,a[left].sum+a[right].minl);
        a[k].minr=min(a[right].minr,a[right].sum+a[left].minr);
    }
    void init(int k,int l,int r)
    {
        a[k].l=l; a[k].r=r;
        if(l==r)
        {
            a[k].sum=a[k].maxsum=a[k].minsum=a[k].maxr=a[k].maxl=a[k].minr=a[k].minl=b[l];
            return ;
        }
        int mid=(l+r)>>1;
        init(k<<1,l,mid);
        init(k<<1|1,mid+1,r);
        pushup(k);
    }
    void update(int k,int pos,int val)
    {
        if(a[k].l==a[k].r)
        {
            a[k].sum=a[k].maxsum=a[k].minsum=a[k].maxr=a[k].maxl=a[k].minr=a[k].minl=val;
            return ;
        }
        int mid=(a[k].l+a[k].r)>>1;
        if(mid>=pos)
        {
            update(k<<1,pos,val);
        }else
        {
            update(k<<1|1,pos,val);
        }
        pushup(k);
    }
    


  • 相关阅读:
    使用 BinToHex() 把 TBytes 转换为十六进制字符串 回复 "梧桐栖凤" 的问题
    ASP.NET中UrlEncode应该用Uri.EscapeDataString()
    抛弃WebService,在.NET4中用 jQuery 调用 WCF
    事实证明Ajax的世界更需要ASP.NET MVC
    tinyMCEPopup.close轻松让IE 9 RC崩溃
    不走寻常路:在WebForm中使用MVC
    关于ASP.NET预编译
    不错的VS2010扩展——JSEnhancements,让js和css也折叠
    VS2010 SP1 Beta与VisualSVN的冲突引起VS2010关闭时重启
    Web应用架构探索笔记 —— 查询
  • 原文地址:https://www.cnblogs.com/suncoolcat/p/3295165.html
Copyright © 2011-2022 走看看