博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 2479 Maximum sum
阅读量:6397 次
发布时间:2019-06-23

本文共 2063 字,大约阅读时间需要 6 分钟。

hot3.png

/*Maximum sumTime Limit: 1000MS		Memory Limit: 65536KTotal Submissions: 29822		Accepted: 9123DescriptionGiven a set of n integers: A={a1, a2,..., an}, we define a function d(A) as below:Your task is to calculate d(A).InputThe input consists of T(<=30) test cases. The number of test cases (T) is given in the first line of the input.Each test case contains two lines. The first line is an integer n(2<=n<=50000). The second line contains n integers: a1, a2, ..., an. (|ai| <= 10000).There is an empty line after each case.OutputPrint exactly one line for each test case. The line should contain the integer d(A).Sample Input1101 -1 2 2 3 -3 4 -4 5 -5Sample Output13HintIn the sample, we choose {2,2,3,-3,4} and {5}, then we can get the answer.Huge input,scanf is recommended.SourcePOJ Contest,Author:Mathematica@ZSU*/#include 
#include
#include
#include
using namespace std;int n = 0, a[50005] = {0}, dp1[50005] = {0}, dp2[50005] = {0};int m1[50005] = {0}, m2[50005] = {0};void solve(){ dp1[0] = a[0]; m1[0] = a[0]; for(int i = 1; i < n; ++i) { dp1[i] = (a[i] > dp1[i - 1] + a[i]) ? a[i] : (dp1[i - 1] + a[i]); m1[i] = (m1[i - 1] > dp1[i]) ? m1[i - 1] : dp1[i]; } dp2[n - 1] = a[n - 1]; m2[n - 1] = a[n - 1]; for(int i = n - 2; i >= 0; --i) { dp2[i] = (a[i] > dp2[i + 1] + a[i]) ? a[i] : (dp2[i + 1] + a[i]); m2[i] = (m2[i + 1] > dp2[i]) ? m2[i - 1] : dp2[i]; }}int main(){ int t = 0; scanf("%d", &t); for(int i = 0; i < t; ++i) { scanf("%d", &n); memset(dp1, 0, sizeof(dp1)); memset(dp2, 0, sizeof(dp2)); memset(m1, 0, sizeof(m1)); memset(m2, 0, sizeof(m2)); for(int j = 0; j < n; ++j) scanf("%d", &a[j]); solve(); int m = -10000000; for(int j = 0; j < n - 1; ++j) { if(m < m1[j] + m2[j + 1]) m = m1[j] + m2[j + 1]; } printf("%d\n", m); } return 0;}

转载于:https://my.oschina.net/locusxt/blog/140936

你可能感兴趣的文章
懒加载和预加载
查看>>
前端面试题
查看>>
Python的赋值、浅拷贝、深拷贝
查看>>
用python操作mysql数据库(之代码归类)
查看>>
ArcGIS Server 10.1 SP1连续查询出现Unable to complete operation错误
查看>>
执行./configure报checking for g++... no错误
查看>>
Dojo学习笔记(十一):Dojo布局——嵌套样例
查看>>
Appium for Android元素定位方法
查看>>
pfSense LAGG(链路聚合)设置
查看>>
教学思路SQL之入门习题《学生成绩》 七.存储过程基础知识
查看>>
createrepo 无法使用解决
查看>>
.net安全类库
查看>>
tablespace backup模式一个没用的技术
查看>>
PostgreSQL安装
查看>>
七牛实时音视频云视频连线demo(web部分)
查看>>
Mysql 权限
查看>>
Spring事务管理(详解+实例)
查看>>
ubuntu apt-get install 出现无法定位软件包...
查看>>
centos7 下 基于docker搭建java/tomcat (方式一)
查看>>
全世界最好的编辑器VIM之Windows配置(gvim)[未测试]
查看>>