一道动态规划  F(x ) = max (F[x-1] + a[x] ,a[x])

题目:

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 123249    Accepted Submission(s): 28497

Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
Sample Input
2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
Sample Output
Case 1: 14 1 4 Case 2: 7 1 6
Author
Ignatius.L
代码:
 1 #include <iostream>
 2 using namespace std;
 3 const int maxn = 100000+10;
 4 int arr[maxn];
 5 int sum[maxn];
 6 int main()
 7 {
 8     int tst;
 9     cin>>tst;
10     int cse= 0;
11     while(tst--)
12     {
13         int n;
14         cin>>n;
15         for(int i=0;i<n;i++)
16         {
17             cin>>arr[i];
18         }
19
20         int s=0,e=0,pos=0;
21         int max = arr[0];
22         int now  = arr[0];
23         for(int i=1;i<n;i++)
24         {
25             if(now+arr[i]<arr[i])
26             {
27                 pos = i;
28                 now = arr[i];
29             }
30             else
31             {
32                     now+=arr[i];
33
34             }
35             if(now > max)
36             {
37                 s= pos;
38                 e = i;
39
40                 max = now;
41             }
42         }
43         cout<<"Case "<<++cse<<":"<<endl;
44
45         cout<<max<<" "<<s+1<<" "<<e+1<<endl;
46         if(tst)
47         cout<<endl;
48
49     }
50
51
52     return 0;
53 }

转载于:https://www.cnblogs.com/doubleshik/p/3489629.html

hud1003 max num相关推荐

  1. Python之max(num, key=lambda x:x[0])用法的详细解析

    1.max(num, key=lambda x:x[0])语法介绍如下: key=lambda 元素: 元素[字段索引] print(max(C, key=lambda x: x[0])) x:x[] ...

  2. HDU1003——MAX SUM

    简单DP,状态转移公式:num[j].data = max{num[j].data, num[j].data+num[j-1].data},也就是保证加上前一个数不失自己的值减小. View Code ...

  3. hdu 1231 最大连续子序列 ,1003 Max Sum;

    题目(1231) #include<stdio.h> #include<iostream> using namespace std; int main() {int K,num ...

  4. [hdu 1003] Max Sum

    跟<算法导论(第三版)>上的一样,抄下来的 30 - 60 ms 标准的分治策略 1 #include <stdio.h> 2 3 int A[100000], Lenght; ...

  5. 返回一个二维整数数组中的最大的子数组和

    一.题目: 1.输入一个二维整形数组,数组里有正数有负数. 2.二维数组中连续的一个子矩阵组成一个子数组. 3.求所有子数组的和的最大值. 二.设计思想: .定义一个二维数组,使用二重循环对其进行赋值 ...

  6. 洛谷 3519 bzoj 2213 Difference

    联考考试考到了这个题,随机化40分,现在来秒掉它吧. 题意: 给一个字符串,求其中的一段,使得出现次数最多的字符与出现次数最少的字符的出现次数之差最大. 输入输出样例 输入样例#1: 复制 10 aa ...

  7. PAT Advanced Level 1010

    1010 Radix (25)(25 分) Given a pair of positive integers, for example, 6 and 110, can this equation 6 ...

  8. 根据“坐标”生成趋势图

    数据库环境:SQL SERVER 2008R2 有一"坐标"表t,表结构如下: id           int, num       int 字段id是序号,递增且连续,字段nu ...

  9. python面试基本题(你需要的)

    1.冒泡排序 lis = [56,12,1,8,354,10,100,34,56,7,23,456,234,-58]def sortport():for i in range(len(lis)-1): ...

最新文章

  1. JDK1.6安装与环境变量设置详细图解
  2. JAVA SE、EE、ME,JRE、JDK,基本数据类型,访问修饰符、函数、封装的概念、UML类图、构造方法、this关键字、static关键字、方法重载、方法重写、包(packahe)
  3. 前端学习(3331):闭包的形式7
  4. 2016年5月26日晚上(妙味课堂js基础-2笔记二(DOM))
  5. Cocos2d-x建工程时避免copy文件夹和库
  6. 直插式电阻电容封装与尺寸图…
  7. 使用python,爆破加密的rar压缩文件
  8. 吴伯凡-认知方法论-如何犯一个“正确”的错误
  9. Modbus协议简单总结
  10. Java中将List分组到Map中算法(可用于android联系人拼音分组)
  11. Android手机为何不再卡顿?性能优化才是安卓起飞关键
  12. Pytorch Note40 词嵌入(word embedding)
  13. 评价模型——目标权重的确定
  14. layer打开iframe弹层,传递与接收参数
  15. 坚持#第89天~胡晨晨会给予我力量!
  16. CentOS 7 安装redis过程中gcc: Command not found错误
  17. 基于BERT+BiLSTM+CRF模型与新预处理方法的古籍自动标点
  18. distinct (去重)
  19. Java基础:Java语言简介
  20. VQA(图像问答)数据集结构及大致内容

热门文章

  1. twitter用户申诉_PHP Twitter用户时间轴提要渲染器
  2. 这场不一样的2017杭州云栖大会!我们在等你!
  3. 2022王者荣耀五五开黑节有哪些活动 具体活动一览
  4. pdf怎么转换成jpg或png格式的图片?
  5. 解决Chrome提示:“您的连接不是私密连接“,且无继续访问选项问题
  6. dell电脑装linux raid无法安装系统,系统定制安装遇到RAID卡无法安装的解决方案
  7. 本固枝荣--前端体验大会12月23日深圳场
  8. 记录一次计算两个日期间的工作日天数,排除节假日、周末等
  9. VICTOR维克多(胜利)羽毛球中低端羽拍导购
  10. 使用php吧excel数据存到数据库,php导入excel表,保存进数据库,使用phpexcel插件