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的加强版,求最大子矩阵和,不过矩阵是可以循环的,矩阵到结尾时可以循环到开头.开始听纠结的,想着难道要分情况讨论吗?!就去网上搜,看到可以通过补全进行 ...
随机推荐
- js实现无限级分类
let arr = [ {id:1,name:"php",pid:0}, {id:2,name:"php基础",pid:1}, {id:3,name:" ...
- php webservice 可能存在的坑
场景: 今天在生产机器上调用webservice失败 报 ...failed to load external entity... wget一下地址发现报500错误 把wsdl去掉再wget 发现就 ...
- 阿里云中linux 下svn服务器安装
摘要: 安装步骤如下: 1.yum install subversion 2.输入rpm -ql subversion查看安装位置,如下图: 我们知道svn在bin目录下生成了几个二进制文件. 输 ...
- hdu1730Northcott Game(nim博弈)
Northcott Game Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- Linux中常用Shell命令
本随笔文章,由个人博客(鸟不拉屎)转移至博客园 写于:2018 年 05 月 04 日 原地址:https://niaobulashi.com/archives/linux-shell.html -- ...
- java扫描控制台输入
由于因最近练习算法的需要,加上API文档中翻译的太过模糊,做了一些小测试,算是武断的记下一些个人结论. Scanner cin = new Scanner(System.in); 对于cin.next ...
- Idea Live Templates
常用live templates 模板 注释 : * * @param $params$ * @return $return$ * $date$ $time$ chiyuanzhen743 */ lo ...
- 90 [LeetCode] Subsets2
Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...
- eos教程如何创建eos测试账号并且使用scatter插件
EOS代币租赁平台 --- Chintai平台已经在Jungle测试网络上部署了,欢迎大家来体验. 地址见: Chintai 公测版 官网是: Chintai 目前测试网络上面需要用到Scatter插 ...
- POJ 2455 Secret Milking Machine(最大流+二分)
Description Farmer John is constructing a new milking machine and wishes to keep it secret as long a ...