首页 > 其他分享 >1007 Maximum Subsequence Sum

1007 Maximum Subsequence Sum

时间:2022-11-12 16:57:23浏览次数:56  
标签:int sum Maximum value start Subsequence 1007 subsequence dp

题目:1007 Maximum Subsequence Sum

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 Knumbers 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

 

思路:

1、动态规划状态

dp[i].value 存放以a[i]结尾的连续序列的最大和  

dp[i].start存放以a[i]结尾的最大连续序列的第一个数的下标 

 

2、低级错误:输出字符串得要用“”双引号

cout<<'0 ' -> cout<<"0 "

 

代码:

 

#include<stdio.h>
#include<iostream>
using namespace std;
struct DP{
    int start, value;   
}dp[10005]; // dp[i].value 表示以下标为i的数组元素为结尾的子串的最大和
int main(){
    int k,index=0;
    int a[10005];
    cin>>k;
    for(int i=0; i<k; i++){
        cin>>a[i];
    }
    dp[0].start = 0;
    dp[0].value = a[0];
    for(int i=1; i<k; i++){
        if(dp[i-1].value + a[i] > a[i]){
            dp[i].value = dp[i-1].value + a[i];
            dp[i].start = dp[i-1].start;
        }else{
            dp[i].value = a[i];
            dp[i].start = i;
        }
    }
    
    for(int i=1; i<k; i++){
        if(dp[i].value > dp[index].value){
            index = i;
        }
    }
    
    if(dp[index].value < 0){
        cout<<"0 "<<a[0]<<' '<<a[k-1];
    }else{
        cout<<dp[index].value<<' '<<a[dp[index].start]<<' '<<a[index];
    }
    return 0;
}

 

标签:int,sum,Maximum,value,start,Subsequence,1007,subsequence,dp
From: https://www.cnblogs.com/yccy/p/16884101.html

相关文章

  • CodeForces - 1092F Tree with Maximum Cost
    题意:给出一棵树,每个结点有一个权值。定义一棵树以ai为根节点的价值为 剩下每个结点到根节点的距离乘权值 之和。求这棵树的最大价值。解:随便选一个结点为根,从下到上统......
  • #10078. 「一本通 3.2 练习 4」新年好
     从1出发访问5个给定点,最小化路程 枚举5个点的排列,然后单源最短路#include<iostream>#include<cstring>#include<queue>usingnamespacestd;structT{......
  • [ABC271E] Subsequence Path
    洛谷链接原题链接题目描述某地区有\(N\)个城镇,编号为1到\(N\),并且由\(M\)条公路连接,编号1到\(M\)。每条公路都是有向的;而且编号为\(i(1\lei\leM)\)......
  • #10077. 「一本通 3.2 练习 3」最短路计数
    问1~n的最短路有几个 #include<iostream>#include<cstring>#include<queue>usingnamespacestd;constintN=1e6+2,M=2e6+2;constintinf=0x3f3f3f3f,m......
  • #10075. 「一本通 3.2 练习 1」农场派对
    图上每个点有一头牛,现在牛群聚集到点X上聚会,然后又回到各自的点,而且牛只走最短路径问所有最短路中最长的一条(路径包含来回) 正反跑一次 spfa(X), spfa(i), an......
  • #10074. 「一本通 3.2 例 3」架设电话线
    在加权无向图上求出一条从1号结点到N号结点的路径,使路径上第K+1大的边权尽量小 二分答案md,判断1~n是否存在一条路径,花费不超过md把w<=md的边看作0,否则看作1......
  • python apscheculer 报错 skipped: maximum number of running instances reached (1
    apscheduler定时任务报错skipped:maximumnumberofrunninginstancesreached(1)原因是默认max_instances最大定时任务是1个,可以通过在add_job中调max_instances增加......
  • Little Girl and Maximum Sum CodeForces - 276C - 差分
    给定一个数列\(a={a_1,a_2,...,a_n}\)以及\(q\)次查询。其中第\(i\)次查询如同:\(l_i,r_i\),意指求\(\sum_{j=l_i}^{r_i}{a_j}\)。但是查询前可以对数列任意排......
  • 20221007_T1A-_贪心/树形dp
    题意给定一个树,求经过\(k\)个不同点所需要的步骤。以及给出一个方案。题解赛时得分:5/100不知道赛时哪里写错了。能想到找出以1开始的直径,直径上的点是必定会走的......
  • 611007 CAD 图案填充面域表格文字
    本节课讲解7CAD图案填充面域表格文字。1.图案填充快捷键为【H】,下面为【渐变色填充】,操作方式是一样的。2.创建矩形,输入【H】,进行对象的选择,面域快捷键【REG】。3.......