题目链接:

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. 【代码笔记】iOS-iOS的目录

    一.iOS中的沙盒机制 · iOS应用程序只能对自己创建的文件系统读取文件,这个独立.封闭.安全的空间,叫做沙盒.它一般存放着程序包文件(可执行文件).图片.音频.视频.plist文件.sqlite数 ...

  2. parseInt OR Number进行数字的转换

    在js中,字符串转为数字类型是比较常见的,平时用的比较多的是parseFloat和parseInt这两个方法.当然,除了这个方法之外还有一个Number:都是转为数字类型,有什么差别? 可以简单的说N ...

  3. 专访周金可:我们更倾向于Greenplum来解决数据倾斜的问题

    周金可,就职于听云,维护MySQL和GreenPlum的正常运行,以及调研适合听云业务场景的数据库技术方案. 听云周金可 9月24日,周金可将参加在北京举办的线下活动,并做主题为<GreenPl ...

  4. CVE-2017-17215 - 华为HG532命令注入漏洞分析

    前言 前面几天国外有个公司发布了该漏洞的详情.入手的二手 hg532 到货了,分析测试一下. 固件地址:https://ia601506.us.archive.org/22/items/RouterH ...

  5. MySql与SqlServer的区别

    1.SQL Server 是Microsoft 公司推出的关系型数据库管理系统.具有使用方便可伸缩性好与相关软件集成程度高等优点,可跨越从运行Microsoft Windows 98 的膝上型电脑到运 ...

  6. 5maven工程莫名其妙只在项目名称那里有一个红叉

    manven工程里面没有报错的地方,编译也没有问题,只是项目名称那里有一个红叉.   解决办法:   右击项目-->maven-->update project   注意: 这种方法有时可 ...

  7. Pycharm Html CSS JS 快捷方式创建元素

    div#div1>ol>li.id*4 tab键 <div id="div1"> <ol> <li id="id"&g ...

  8. 搜索菜单栏侧滑效果控件SearchView

    搜索菜单栏侧滑效果控件SearchView 本人视频教程系类   iOS中CALayer的使用 效果1: 效果2: 项目中用到的图片 bgImg@2x.png: 源码: SearchView.h + ...

  9. 模仿SDWebImage实现异步加载图片

    模仿SDWebImage实现异步加载图片 SDWebImage想必大家都不陌生吧,要实现它的图片异步加载功能这个还是很简单的. 注意:此处我只实现了异步加载图片,并没有将文件缓存到本地的打算哦:) 源 ...

  10. [翻译] SCRecorder

    SCRecorder https://github.com/rFlex/SCRecorder An easy Vine/Instagram like video and/or audio record ...