CF#67 75d Big Maximum Sum
题解:
观察到拼接后的数据范围过大,无法O(n)解决,但是大区间是由很多小区间组成,而小区间是固定的,不会变化,所以可以考虑预处理出每个小区间的信息,然后根据给定序列按顺序一步一步合并区间信息。
跟线段树维护区间最大子段和类似,要合并2个区间我们只需要知道如下信息:
前缀最大子段和,后缀最大子段和,当前区间最大子段和。
那么转移方式如下:
1,对于区间最大子段和而言,要么是继承2个区间中的某个最大子段和,要么是用上一个区间的后缀最大子段和+后一个区间的前缀最大子段和凑成一个子段和作为新的最大子段和,所以3者取max即可。
2,对于前缀最大值而言,,,其实我们在转移的时候并不需要用到上一个区间的前缀最大值,因此无需再次维护。
3,对于后缀最大值而言,要么是继承后一个区间的后缀最大子段和,要么是上一个区间的后缀最大值+后一个区间的整个区间凑成的新后缀最大值,2者取max即可。
#include<bits/stdc++.h>
using namespace std;
#define R register int
#define AC 50
#define ac 5000
#define LL long long int n, m, len;
LL l[AC], r[AC], mx[AC], sum[AC], ans, rr;
int s[ac]; inline int read()
{
int x = ;char c = getchar();bool z = false;
while(c > '' || c < '')
{
if(c == '-') z = true;
c = getchar();
}
while(c >= '' && c <= '') x = x * + c - '', c = getchar();
if(!z) return x;
else return -x;
} inline void upmax(LL &a, LL b)
{
if(b > a) a = b;
} void pre()
{
n = read(), m = read();
for(R i = ; i <= n; i ++)
{
len = read();
for(R j = ; j <= len; j ++) s[j] = read(), sum[i] += s[j];
LL tmp = ;
for(R j = ; j <= len; j ++) tmp += s[j], upmax(l[i], tmp);
tmp = ;
for(R j = len; j; j --) tmp += s[j], upmax(r[i], tmp);
tmp = ;
for(R j = ; j <= len; j ++)
{
tmp += s[j];
upmax(mx[i], tmp);
if(tmp < ) tmp = ;
}
}
} void work()
{
int x;
x = read();
rr = r[x], ans = mx[x];
for(R i = ; i <= m; i ++)
{
x = read();//读入右区间
upmax(ans, mx[x]);
upmax(ans, rr + l[x]);
rr = rr + sum[x];
upmax(rr, r[x]);
}
cout << ans << endl;
} int main()
{
//freopen("in.in", "r", stdin);
pre();
work();
//fclose(stdin);
return ;
}
CF#67 75d Big Maximum Sum的更多相关文章
- cf#513 B. Maximum Sum of Digits
B. Maximum Sum of Digits time limit per test 2 seconds memory limit per test 512 megabytes input sta ...
- CF 276C Little Girl and Maximum Sum【贪心+差分】
C. Little Girl and Maximum Sum time limit per test2 seconds memory limit per test256 megabytes input ...
- POJ2479 Maximum sum[DP|最大子段和]
Maximum sum Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39599 Accepted: 12370 Des ...
- ural 1146. Maximum Sum
1146. Maximum Sum Time limit: 0.5 secondMemory limit: 64 MB Given a 2-dimensional array of positive ...
- UVa 108 - Maximum Sum(最大连续子序列)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- 最大子矩阵和 URAL 1146 Maximum Sum
题目传送门 /* 最大子矩阵和:把二维降到一维,即把列压缩:然后看是否满足最大连续子序列: 好像之前做过,没印象了,看来做过的题目要经常看看:) */ #include <cstdio> ...
- URAL 1146 Maximum Sum(最大子矩阵的和 DP)
Maximum Sum 大意:给你一个n*n的矩阵,求最大的子矩阵的和是多少. 思路:最開始我想的是预处理矩阵,遍历子矩阵的端点,发现复杂度是O(n^4).就不知道该怎么办了.问了一下,是压缩矩阵,转 ...
- ural 1146. Maximum Sum(动态规划)
1146. Maximum Sum Time limit: 1.0 second Memory limit: 64 MB Given a 2-dimensional array of positive ...
- UVa 10827 - Maximum sum on a torus
题目大意:UVa 108 - Maximum Sum的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...
随机推荐
- MySQL高级-showProfile
一.Show Profile是什么? 是MySql提供可以用来分析当前会话中语句执行的资源消耗情况,可以用于SQL的调优的测量. 默认情况下,参数处于关闭状态,并保存最近15次的运行结果. 二.分析步 ...
- 在ubuntu安装python, theano, keras , Spearmint, Mongodb
系统配置: Ubuntu 14 (其他系统也差不多如下操作) 1. 通过anaconda安装 python 地址: https://www.continuum.io/downloads#linux 2 ...
- jmeter开发自己的sampler插件
1. 新建maven工程 2.pom文件引入jmeter的核心包 <project xmlns="http://maven.apache.org/POM/4.0.0" xml ...
- .net web api应用遇到的一些问题
1.调用webapi接口时,碰到一种情况: 通过webapi调用接口时,返回的json数据,死活转换不成对象,转换的对象一直为null: webapi端代码: [HttpGet] public str ...
- New Year and Domino:二维前缀和
题目描述: They say "years are like dominoes, tumbling one after the other". But would a year f ...
- LeetCode 102 ——二叉树的层次遍历
1. 题目 2. 解答 定义一个存放树中数据的向量 data,一个存放树的每一层数据的向量 level_data 和一个存放每一层节点的队列 node_queue. 如果根节点非空,根节点进队,然后循 ...
- springmvc项目,浏览器报404错误的问题
问题描述: 建立了web工程,配置pom.xml,web.xml,编写controller类,在spring-mvc-servlet.xml文件中指定开启注解和扫描的包位置<mvc:annota ...
- [转载]Java集合框架的常见面试题
http://www.jfox.info/40-ge-java-ji-he-lei-mian-shi-ti-he-da-an 整理自上面链接: Java集合框架为Java编程语言的基础,也是Java面 ...
- POJ 3675 Telescope(简单多边形和圆的面积交)
Description Updog is watching a plane object with a telescope. The field of vision in the telescope ...
- Greedy Gift Givers 贪婪的送礼者
Description 对于一群要互送礼物的朋友,TRW要确定每个人送出的钱比收到的多多少.在这一个问题中,每个人都准备了一些钱来送礼物,而这些钱将会被平均分给那些将收到他的礼物的人.然而,在任何一群 ...