题目链接:

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. 【PyQt5 学习记录】006:重写窗口事件及QMessageBox

    #!/usr/bin/env python import sys from PyQt5.QtWidgets import (QApplication, QMainWindow, QMessageBox ...

  2. Drupal8学习之路--官网文档碎碎记--主题篇

    主要记录一些琐碎的知识点. 1.“In Drupal 8 drupal_add_css(), drupal_add_js() and drupal_add_library()were removed ...

  3. Laravel 支付宝异步通知 419报错

    支付宝在支付是有服务器通知和网页通知,一个在前端展示,一个在后台操作, laravel框架自带csrf_token验证. 所以我们需要把支付的路由跳过验证 可以在中间键的csrf配置中更改

  4. <VS2010>混合模式程序集是针对“v2.0”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集

    在把以前写的代码生成工具从原来的.NET3.5升级到.NET4.0时,将程序集都更新后,一运行程序在一处方法调用时报出了一个异常: 混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有 ...

  5. phoenix使用vue

    phoenix使用vue mix phoenix.new ass2 Fetch and install dependencies? [Yn] y 修改 package.json { "rep ...

  6. Listview点击已读使用getBadgeView标示

    重:每个ListItem是属于ListItem自己的,不能够放到ViewHolder中,而是数据源每项的. @Override public View getView(int position, Vi ...

  7. 使用docker安装使用gitlab

    1.下载镜像 gitlab/gitlab-ce:latest            当前gitlab最新版本为10.0.4 2.在服务器上创建目录 mkdir -p /home/work/ins/co ...

  8. map合并,相同键对应的值相加

    最近在做统计钱的计算时遇到的一个需求,需要将一个大类别下的每一种钱进行特定的运算然后获得六年的钱,最后将这些钱按照年份进行汇总,获得总得大类型的六年的钱,在这个过程中采用了这种方法,每次算得钱放在ma ...

  9. 滑动cell的时候执行动画效果

    滑动cell的时候执行动画效果 效果图: 源码: // // ViewController.m // AniTab // // Created by XianMingYou on 15/2/26. / ...

  10. Python3.5 执行发邮件Exchangelib(=)

    fyl Python发邮件的代码如下: 只需要填写好加粗字体,即可正常使用. from exchangelib import DELEGATE, Account, Credentials, Messa ...