【POJ 2750】 Potted Flower(线段树套dp)

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 4566   Accepted: 1739

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

不知道这样说够不够准确 总的来说就是把dp的思想加到了线段树中。

在我感觉肯定能A的时候给我了个WA 在我万念俱灰的时候给我了个AC。。。

首先依据题目 n个点构成的环 要求求出最大的连续子序列 n与1是相邻的(环的性质)

仅仅到这里事实上有两种状态 如果从1,n处断开 最大子序列就是[L,R](L <= R)

然而成环 又会出现[1,R]+[L,n]这样的绕过一圈的情况 事实上也好做 用总和减去1~n链的最小子序列和就好

对于求链的最大子序列和 能够由tr[root].max = max(max(tr[root<<1].max,tr[root<<1|1].max),tr[root>>1].lmax+tr[root>>1|1].rmax) 得出

即为左区间最大子序列和 右区间最大子序列和 左区间右连续的最大子序列和+右区间左连续的最大子序列和 这三个中最大的那个

罪域最小子序列和也是一样 能够由tr[root].min = min(min(tr[root<<1].min,tr[root<<1|1].min),tr[root>>1].lmin+tr[root>>1|1].rmin) 得出

即为左区间最小子序列和 右区间最小子序列和 左区间右连续的最小子序列和+右区间左连续的最小子序列和 这三个中最小的那个

这样答案也非常好得出了 ans = max( tr[1].max,tr[1].sum-tr[1].min );

然而这样会WA 最关键的一点没有注意啊!

。不能够把1~n所有选取 意思也就是说这个最大子序列和不能够是整个环 意思也就是说上面的做法通通WA啊……………………………………………………………………………………………………………………………………………………………………………………

只是莫操心。

。我不是在逗你玩。。。。

(放下板砖 施主听鰯讲。。。

事实上仅仅要统计一下负数的个数即可了 假设存在负数 那就说明最大子序列和肯定不会所有选取 至少扣掉个负数吧 也就是说这样的情况下上面的解答是正确的

可是假设全是正整数 那么就须要扣去一部分 那么扣去哪部分呢 当然是扣去最小子序列和了 事实上说更直白点 就是最小的那个正整数

这样就能够愉快的AC了。。

代码例如以下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout) using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8; struct Tree
{
int cnt,sum,lmax,rmax,mx,lmin,rmin,mn;
}; Tree tr[400400];
int n; void init(int root,int l,int r)
{
if(l == r)
{
scanf("%d",&tr[root].sum);
tr[root].cnt = (tr[root].sum < 0);
tr[root].lmin = tr[root].rmin = tr[root].mn = tr[root].lmax = tr[root].rmax = tr[root].mx = tr[root].sum;
return;
}
int mid = (l+r)>>1;
init(root<<1,l,mid);
init(root<<1|1,mid+1,r);
tr[root].sum = tr[root<<1].sum+tr[root<<1|1].sum; tr[root].lmax = max(tr[root<<1].lmax,tr[root<<1].sum+tr[root<<1|1].lmax);
tr[root].rmax = max(tr[root<<1|1].rmax,tr[root<<1|1].sum+tr[root<<1].rmax);
tr[root].mx = max(max(tr[root<<1].mx,tr[root<<1|1].mx),tr[root<<1].rmax + tr[root<<1|1].lmax); tr[root].lmin = min(tr[root<<1].lmin,tr[root<<1].sum+tr[root<<1|1].lmin);
tr[root].rmin = min(tr[root<<1|1].rmin,tr[root<<1|1].sum+tr[root<<1].rmin);
tr[root].mn = min(min(tr[root<<1].mn,tr[root<<1|1].mn),tr[root<<1].rmin + tr[root<<1|1].lmin);
tr[root].cnt = tr[root<<1].cnt+tr[root<<1|1].cnt;
} void Change(int root,int l,int r,int pos,int x)
{
if(l == r)
{
tr[root].lmin = tr[root].rmin = tr[root].mn = tr[root].lmax = tr[root].rmax = tr[root].mx = tr[root].sum = x;
tr[root].cnt = (x < 0);
return;
}
int mid = (l+r)>>1; if(mid >= pos) Change(root<<1,l,mid,pos,x);
else Change(root<<1|1,mid+1,r,pos,x); tr[root].sum = tr[root<<1].sum+tr[root<<1|1].sum; tr[root].lmax = max(tr[root<<1].lmax,tr[root<<1].sum+tr[root<<1|1].lmax);
tr[root].rmax = max(tr[root<<1|1].rmax,tr[root<<1|1].sum+tr[root<<1].rmax);
tr[root].mx = max(max(tr[root<<1].mx,tr[root<<1|1].mx),tr[root<<1].rmax + tr[root<<1|1].lmax); tr[root].lmin = min(tr[root<<1].lmin,tr[root<<1].sum+tr[root<<1|1].lmin);
tr[root].rmin = min(tr[root<<1|1].rmin,tr[root<<1|1].sum+tr[root<<1].rmin);
tr[root].mn = min(min(tr[root<<1].mn,tr[root<<1|1].mn),tr[root<<1].rmin + tr[root<<1|1].lmin);
tr[root].cnt = tr[root<<1].cnt+tr[root<<1|1].cnt;
} int main()
{
//fread();
//fwrite();
int m,pos,x; while(~scanf("%d",&n))
{
init(1,1,n);
// printf("%d\n",tr[1].mx); scanf("%d",&m);
while(m--)
{
scanf("%d%d",&pos,&x);
Change(1,1,n,pos,x); if(!tr[0].cnt)
printf("%d\n",tr[1].sum-tr[1].mn);
else printf("%d\n",max(tr[1].mx,tr[1].sum-tr[1].mn));
} } return 0;
}

id=2750">

【POJ 2750】 Potted Flower(线段树套dp)的更多相关文章

  1. POJ 2750 Potted Flower(线段树+dp)

    题目链接 虽然是看的别的人思路,但是做出来还是挺高兴的. 首先求环上最大字段和,而且不能是含有全部元素.本来我的想法是n个元素变为2*n个元素那样做的,这样并不好弄.实际可以求出最小值,总和-最小,就 ...

  2. POJ.2750.Potted Flower(线段树 最大环状子段和)

    题目链接 /* 13904K 532ms 最大 环状 子段和有两种情况,比如对于a1,a2,a3,a4,a5 一是两个端点都取,如a4,a5,a1,a2,那就是所有数的和减去不选的,即可以计算总和减最 ...

  3. POJ 2750 Potted Flower (线段树区间合并)

    开始懵逼找不到解法,看了网上大牛们的题解才发现是区间合并...  给你n个数形成一个数列环,然后每次进行一个点的修改,并输出这个数列的最大区间和(注意是环,并且区间最大只有n-1个数) 其实只需要维护 ...

  4. POJ 2376 Cleaning Shifts (线段树优化DP)

    题目大意:给你很多条线段,开头结尾是$[l,r]$,让你覆盖整个区间$[1,T]$,求最少的线段数 题目传送门 线段树优化$DP$裸题.. 先去掉所有能被其他线段包含的线段,这种线段一定不在最优解里 ...

  5. POJ 2750 Potted Flower(线段树的区间合并)

    点我看题目链接 题意 : 很多花盆组成的圆圈,每个花盆都有一个值,给你两个数a,b代表a位置原来的数换成b,然后让你从圈里找出连续的各花盆之和,要求最大的. 思路 :这个题比较那啥,差不多可以用DP的 ...

  6. (简单) POJ 2750 Potted Flower,环+线段树。

    Description The little cat takes over the management of a new park. There is a large circular statue ...

  7. POJ 2750 Potted Flower

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

  8. poj 1769 Minimizing maximizer 线段树维护dp

    题目链接 给出m个区间, 按区间给出的顺序, 求出覆盖$ [1, n] $ 至少需要多少个区间. 如果先给出[10, 20], 在给出[1, 10], 那么相当于[10, 20]这一段没有被覆盖. 令 ...

  9. Potted Flower(线段树+dp)

    http://poj.org/problem?id=2750 题意:在一个圈中取若干个相邻的数,求他们的最大序列和.不能够同时取所有的数. 看了一篇解题报告写的很详细..http://blog.csd ...

随机推荐

  1. js特效——自动滚动

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. 使用Modernizr检测支持CSS3

    使用Modernizr检测支持CSS3 如果支持某个属性,会增加一个class,名字就是该属性: 不支持,名字是no-某属性 还提供了一个全局Modernizr对象,使用如下: <script ...

  3. WAP 图片 lazyload

    原理是根据屏幕上的坐标找到需要做 lazyload 的区域 1,先监听 scroll 事件 ,scrolling_lt window.addEventListener('scroll', functi ...

  4. IE input X 去掉文本框的叉叉和password输入框的眼睛图标

    IE input X 去掉文本框的叉叉和password输入框的眼睛图标 从IE 10開始,type="text" 的 input 在用户输入内容后.会自己主动产生一个小叉叉(X) ...

  5. [NIO]dawn之Task具体解释

    在上篇文章中,我们设置好了开发环境,接下来.我们将在了解了Task以及Buffer之后,再開始了解网络编程.我们首先来看看Task task简单介绍 package zhmt.dawn; import ...

  6. 安卓通过OkHttp获取json数据

    使用Http协议访问网络 OkHttp使用 可以很好的获取接口数据!json数据! 支持get和post提交方式!!! 1.引入模块 compile 'com.squareup.okhttp3:okh ...

  7. 详解JSP九个内置对象

    [JSP]☆★之详解九个内置对象       在web开发中,为方便开发者,JSP定义了一些由JSP容器实现和管理的内置对象,这些对象可以直接被开发者使用,而不需要再对其进行实例化!本文详解,JSP2 ...

  8. [JZOJ 5910] [NOIP2018模拟10.18] DuLiu 解题报告 (并查集+思维)

    题目链接: https://jzoj.net/senior/#contest/show/2530/0 题目: LF是毒瘤出题人中AK IOI2019,不屑于参加NOI的唯一的人.他对人说话,总是满口垃 ...

  9. pip更新问题

    pip更新及Requirement already up-to-date解决方法 pip更新 更新命令 将pip更新版本 1 python -m pip install --upgrade pip R ...

  10. 计算label

    func getCGSize(size:CGSize,fontSize:CGFloat,text:String)->CGSize{ let attributes = [NSFontAttribu ...