1007. Maximum Subsequence Sum (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } where 1 <= i <= j <= K. The Maximum Subsequence is the continuous subsequence which has the largest sum of its elements. For example, given sequence { -2, 11, -4, 13, -5, -2 }, its maximum subsequence is { 11, -4, 13 } with the largest sum being 20.

Now you are supposed to find the largest sum, together with the first and the last numbers of the maximum subsequence.

Input Specification:

Each input file contains one test case. Each case occupies two lines. The first line contains a positive integer K (<= 10000). The second line contains K numbers, separated by a space.

Output Specification:

For each test case, output in one line the largest sum, together with the first and the last numbers of the maximum subsequence. The numbers must be separated by one space, but there must be no extra space at the end of a line. In case that the maximum subsequence is not unique, output the one with the smallest indices i and j (as shown by the sample case). If all the K numbers are negative, then its maximum sum is defined to be 0, and you are supposed to output the first and the last numbers of the whole sequence.

Sample Input:

10
-10 1 2 3 4 -5 -23 3 7 -21

Sample Output:

10 1 4

这题做了好几次了。。。这次依然没有自己想到,但其实挺简单的。

思路:仔细一想也是动态规划的思想,从num[1]-->num[n],维护一个连续区间最大值{sum,l,r}。
#include<bits/stdc++.h>
using namespace std;
#define N 10005 int num[N]; int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",&num[i]);
int l=,r=,resl,resr,res=-,tmp=;
for(int i=;i<n;i++)
{
tmp+=num[i];
r=i;
if(tmp<) //如果求和的值tmp<0,那么弃掉前面的求和,在继续递推
{
l=r=i+;
tmp=;
}
else //如果tmp>0,那么后面可能找到更大的求和
{
if(tmp>res)
{
res=tmp;
resl=l;
resr=r;
}
}
}
if(res==-)
printf("%d %d %d\n",,num[],num[n-]);
else
printf("%d %d %d\n",res,num[resl],num[resr]);
return ;
}
 

patest_1007_Maximum Subsequence Sum_(dp)(思维)的更多相关文章

  1. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  2. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  3. POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

    POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...

  4. D. Yet Another Problem On a Subsequence 解析(DP)

    Codeforce 1000 D. Yet Another Problem On a Subsequence 解析(DP) 今天我們來看看CF1000D 題目連結 題目 略,請直接看原題 前言 這題提 ...

  5. HDU4632:Palindrome subsequence(区间DP)

    Problem Description In mathematics, a subsequence is a sequence that can be derived from another seq ...

  6. poj2533--Longest Ordered Subsequence(dp:最长上升子序列)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 33943   Acc ...

  7. POJ 2533-Longest Ordered Subsequence(DP)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 34454   Acc ...

  8. POJ 2533 Longest Ordered Subsequence(DP 最长上升子序列)

    Longest Ordered Subsequence Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 38980   Acc ...

  9. HDU 1159 Common Subsequence【dp+最长公共子序列】

    Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

随机推荐

  1. HDU1069 Monkey and Banana —— DP

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1069 Monkey and Banana Time Limit: 2000/1000 MS ...

  2. Android控件之HorizontalScrollView 去掉滚动条

    在默认情况下,HorizontalScrollView控件里面的内容在滚动的情况下,会出现滚动条,为了去掉滚动条, 只需要在<HorizontalScrollView/>里面加一句 and ...

  3. VMware克隆虚拟机后网络配置

    修复克隆虚拟机文件 # vi /etc/udev/rules.d/70-persistent-net.rules # PCI device 0x8086:0x100f (e1000) SUBSYSTE ...

  4. 给Xcode增加复制行、删除行快捷键的方法

    运行: sudo chmod 666 /Applications/Xcode.app/Contents/Frameworks/IDEKit.framework/Resources/IDETextKey ...

  5. Ubuntu TFTP 服务

    /******************************************************************** * Ubuntu TFTP 服务 * 说明: * 在Ubun ...

  6. AutoIT: 如何设置GUICtrlCreateCombo选项为不可修改状态

    GUICtrlCreateCombo中选中的选项是可修改的,也可设置为不可修改状态. #include<ComboConstants.au3> GUICtrlCreateLabel(, , ...

  7. r.json()

    requests模块中,r.json()为Requests中内置的JSON解码器 其中只有response返回为json格式时,用r.json()打印出响应的内容, 如果response返回不为jso ...

  8. bzoj4390

    树上差分 感觉挺巧妙的... 每次更新就是在u,v上+1,x是lca(u,v),在x和fa[x]上-1,那么每个点的权值就是子树和,正确性yy一下就行了 不过树状数组的常数真是小,改成前缀和才快了20 ...

  9. javascript判断页面是否在移动设备上打开

    var ua = navigator.userAgent; var ipad = ua.match(/(iPad).*OS\s([\d_]+)/), isIphone =!ipad && ...

  10. Ruby Encoding类

    Encoding类  内部编码  IO对象内部处理时候的编码   外部编码  IO对象对外输出的时候的编码  输入  外部字符与自己的外部编码对比(没设定的默认                     ...