题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1003

Max Sum

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

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
 
分析:
题目的数据很水啊
输入6 2 7 -9 5 4 3,答案应该是 12 1 6 的结果12 3 6竟然能过!!!!!!!
ac代码
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<algorithm>
using namespace std;
typedef long long LL;
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]={{,},{,-},{,},{-,}};
#define max_v 100005
int a[max_v];
int dp[max_v];
int main()
{
int t;
cin>>t;
int c=;
while(c<=t)
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
memset(dp,,sizeof(dp));
//dp[i] 以第i个数结尾的序列的最大字段和
dp[]=a[];
for(int i=;i<=n;i++)
{
if(dp[i-]<)
dp[i]=a[i];
else
dp[i]=dp[i-]+a[i];
}
int index1=,index2=; //找尾 最大dp[i]对应的i就是尾
int temp=dp[];
for(int i=;i<=n;i++)
{
if(temp<dp[i])
{
temp=dp[i];
index2=i;
}
} //找头 从尾往前面加,加到和为0就是头
for(int i=index2,x=;x!=temp;i--)
{
x+=a[i];
index1=i;
}
int sum=;
for(int i=index2-;i>=;i--)
{
sum+=a[i];
if(sum==)
index1=i;
} printf("Case %d:\n",c);
printf("%d %d %d\n",temp,index1,index2);
if(c<t)
printf("\n");
c++;
}
return ;
}

另外一种找头的方法:

#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<algorithm>
using namespace std;
typedef long long LL;
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]={{,},{,-},{,},{-,}};
#define max_v 100005
int a[max_v];
int dp[max_v];
int main()
{
int t;
cin>>t;
int c=;
while(c<=t)
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
memset(dp,,sizeof(dp));
//dp[i] 以第i个数结尾的序列的最大字段和
dp[]=a[];
for(int i=;i<=n;i++)
{
if(dp[i-]<)
dp[i]=a[i];
else
dp[i]=dp[i-]+a[i];
}
int index1=,index2=; //找尾 最大dp[i]对应的i就是尾
int temp=dp[];
for(int i=;i<=n;i++)
{
if(temp<dp[i])
{
temp=dp[i];
index2=i;
}
} index1=index2;
for(int i=index1;i>=;i--)
{
if(dp[i]>=)
index1=i;
else
break;
} printf("Case %d:\n",c);
printf("%d %d %d\n",temp,index1,index2);
if(c<t)
printf("\n");
c++;
}
return ;
}

HDU1003 最大子段和 线性dp的更多相关文章

  1. 『最大M子段和 线性DP』

    最大M子段和(51nod 1052) Description N个整数组成的序列a[1],a[2],a[3],-,a[n],将这N个数划分为互不相交的M个子段,并且这M个子段的和是最大的.如果M &g ...

  2. 洛谷P1115 最大子段和 (线性DP)

    经典的线性DP例题,用f[i]表示以第i个位置结尾的最大连续子段和. 状态转移方程:f[i]=max(f[i],f[i-1]+a[i]); 这里省去了a数组,直接用f数组读数据,如果f[i-1]< ...

  3. 线性DP总结(LIS,LCS,LCIS,最长子段和)

    做了一段时间的线性dp的题目是时候做一个总结 线性动态规划无非就是在一个数组上搞嘛, 首先看一个最简单的问题: 一,最长字段和 下面为状态转移方程 for(int i=2;i<=n;i++) { ...

  4. POJ 2479-Maximum sum(线性dp)

    Maximum sum Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33918   Accepted: 10504 Des ...

  5. poj 1050 To the Max(线性dp)

    题目链接:http://poj.org/problem?id=1050 思路分析: 该题目为经典的最大子矩阵和问题,属于线性dp问题:最大子矩阵为最大连续子段和的推广情况,最大连续子段和为一维问题,而 ...

  6. 动态规划_线性dp

    https://www.cnblogs.com/31415926535x/p/10415694.html 线性dp是很基础的一种动态规划,,经典题和他的变种有很多,比如两个串的LCS,LIS,最大子序 ...

  7. 线性DP 学习笔记

    前言:线性DP是DP中最基础的.趁着这次复习认真学一下,打好基础. ------------------ 一·几点建议 1.明确状态的定义 比如:$f[i]$的意义是已经处理了前$i个元素,还是处理第 ...

  8. LightOJ1044 Palindrome Partitioning(区间DP+线性DP)

    问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...

  9. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

随机推荐

  1. Django基础五之django模型层(一)单表操作

    一 ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人 ...

  2. drupal7,注册成功之后想跳转到指定页面,该怎么破?

     1.hook sigup form alter,修改跳转地址 .还没试过 2.安装一下logintoboggan模块,里面有个注册后跳转到哪个页面的设置 这个对于不写代码的是比较方便的方法    3 ...

  3. string.replace替换

    var str = 'abcadeacf'; var str1 = str.replace('a', 'o'); alert(str1); // 打印结果: obcadeacf var str2 = ...

  4. CSS选择器之伪类选择器(元素)

    :first-child 选择某个元素的第一个子元素(IE6不支持) :last-child 选择某个元素的最后一个子元素 :first-of-type [CSS3]选择一个上级元素下的第一个同类子元 ...

  5. Android MVP Plugin,一键完成MVP结构代码编写

    推荐一个Gradle的学习系列,Gradle相关的知识一直很匮乏,难得发现一个不错的系列: http://www.cnblogs.com/davenkin/p/gradle-learning-1.ht ...

  6. maven 生命周期、生命周期阶段、插件、目标

    生命周期maven的生命周期是抽象的,它本身不做任何实际的工作.实际的工作都由插件来完成.生命周期好比接口,插件好比实现类.maven 有三个独立的生命周期,clean.default.site. 生 ...

  7. 回归JavaScript基础(四)

    主题:JavaScript变量.作用域和内存问题 JavaScript的变量和别的语言比起来是与众不同的.说道变量,不得不谈他的作用域.同很多语言一样,JavaScript开发者也不用担心开发中内存的 ...

  8. 动态修改JDBC数据源配置

    因项目需要能动态修改数据源的配置,及修改后不用重启整个应用.使用的数据源是apache的BasicDataSource,网上千篇一律的是如下实现: BasicDataSource bds=getDat ...

  9. docker的网络基础配置

    一.端口映射实现访问容器 当容器中运行一些网络应用,要让外部访问这些应用时,可以通过-P或-p参数来指定端口映射.当使用-P标记时,Docker会随机映射一个49000~49900的端口至容器内部开放 ...

  10. 《SQL Server 2008从入门到精通》20180627

    数据库范式理论 范式理论是为了建立冗余较小结构合理的数据库所遵循的规则.关系数据库中的关系必须满足不同的范式.目前关系数据库有六种范式:第一范式(1NF).第二范式(2NF).第三范式(3NF).BC ...