Potted Flower(线段树+dp)
2024-08-30 21:16:58
http://poj.org/problem?id=2750
题意:在一个圈中取若干个相邻的数,求他们的最大序列和。不能够同时取所有的数。
看了一篇解题报告写的很详细。。http://blog.csdn.net/non_cease/article/details/7437690
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
const int N=;
using namespace std;
struct node
{
int l,r,sum,minsum,maxsum;
int lmax,rmax,lmin,rmin;
} Tree[*N];
int a[N];
void pushup(int rt)
{
int l = *rt;
int r = *rt+;
Tree[rt].sum=Tree[l].sum+Tree[r].sum;
Tree[rt].minsum=min(min(Tree[l].minsum,Tree[r].minsum),Tree[l].rmin+Tree[r].lmin);
Tree[rt].maxsum=max(max(Tree[l].maxsum,Tree[r].maxsum),Tree[l].rmax+Tree[r].lmax);
Tree[rt].lmax=max(Tree[l].lmax,Tree[l].sum+Tree[r].lmax);
Tree[rt].rmax=max(Tree[r].rmax,Tree[r].sum+Tree[l].rmax);
Tree[rt].lmin=min(Tree[l].lmin,Tree[l].sum+Tree[r].lmin);
Tree[rt].rmin=min(Tree[r].rmin,Tree[r].sum+Tree[l].rmin);
}
void build(int l,int r,int rt)
{
Tree[rt].l = l;
Tree[rt].r = r;
if (l==r)
{
Tree[rt].sum=Tree[rt].minsum=Tree[rt].maxsum=a[r];
Tree[rt].lmax=Tree[rt].rmax=Tree[rt].lmin=Tree[rt].rmin=a[r];
return ;
}
int mid = (l+r)>>;
build(l,mid,*rt);
build(mid+,r,*rt+);
pushup(rt);
}
void update(int pos,int val,int rt)
{
if (Tree[rt].l==Tree[rt].r)
{
Tree[rt].sum=Tree[rt].minsum=Tree[rt].maxsum=val;
Tree[rt].lmax=Tree[rt].rmax=Tree[rt].lmin=Tree[rt].rmin=val;
return ;
}
int mid=(Tree[rt].l+Tree[rt].r)>>;
if (pos<=mid)
update(pos,val,*rt);
else
update(pos,val,*rt+);
pushup(rt);
}
int main()
{
int n,m,pos,val;
scanf("%d",&n);
for (int i = ; i <=n; i++)
{
scanf("%d",&a[i]);
}
build(,n,);
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&pos,&val);
update(pos,val,);
if(Tree[].sum==Tree[].maxsum)
{
printf("%d\n",Tree[].sum-Tree[].minsum);
}
else
{
printf("%d\n",max(Tree[].maxsum,Tree[].sum-Tree[].minsum));
}
}
return ;
}
最新文章
- 启动了VSAN服务的主机不在vCenter集群中
- (转)C#图解—PictureBox.SizeMode 属性
- Angular自定义指令(directive)
- js中的继承
- Eclipse中启用Oracle jdbc logging
- 错误异常 (1)Android Studio错误提示:Gradle project sync failed. Basic functionality (eg. editing, debugging) will not work properly
- Bootstrap学习笔记(三) 网格系统
- Struts1和Struts2的区别和对比(完整版)
- socket.io实现
- cavium octeon 处理器启动总线Bootbus 简介
- CKPlayer从Cookie里读取上次播放记录的一个demo
- DOM元素拖拽效果
- 关于Spring总结
- Redis线程模型
- linux服务基础(一)之CentOS6编译安装httpd2.4
- 汇编 SETG,SETL ,SETGE, SETLE指令
- json 不能 dumps Decimal 解决办法
- 每天进步一点点- 资源与URI(吐血精华总结)
- RocketMQ 主从同步机制
- activiti自己定义流程之Spring整合activiti-modeler实例(一):环境搭建
热门文章
- HEVC-HM16.9源码学习(1)TEncCu::xCompressCU
- 一只小蜜蜂(hdoj 2044,动态规划递推)
- UVA 674 Coin Change (完全背包)
- Cyclic Nacklace HDU - 3746 (kmp next数组应用)
- MySQL Docker方式安装
- Java面试题大全(javaSe,HTML,CSS,js,Spring框架等)
- C#装饰模式
- codevs2370 小机房的树
- HDU——1576 A/B
- css3的高级而有用且很少人知道的属性和样式