首页 > 其他分享 >PTA_Maximum Subsequence Sum

PTA_Maximum Subsequence Sum

时间:2022-11-16 17:55:32浏览次数:44  
标签:case int sum subsequence Maximum Subsequence numbers Maxsize 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 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
   
#include <stdio.h>
#include <stdlib.h>

// 思路 在线搜索

int main(){
    long k;
    int t;
    int a,b,c;
    int flag = 0;
    int Maxsize = 0;
    int sum = 0;
    int f = 0;
    scanf("%ld",&k);
    while (k--) {
        scanf("%d",&t);
        if(f == 0){
            c = a = t;
            f = 1;
        }
        sum = sum + t;
        if(sum < 0){
            sum = 0;
            flag = 0;
        }else{
            if(sum == 0){
                f = 4;
            }
            if(flag == 0){
                a = t;
                flag = 1;
            }
        }
        if(sum > Maxsize){
            f = 2;
            Maxsize = sum;
            c = a;
            b = t;
        }
    }

    if(f == 2){
        printf("%d %d %d",Maxsize,c,b);
    }else if(f == 4){
        printf("%d %d %d",Maxsize,a,a);
    }else{
        printf("%d %d %d",Maxsize,c,t);
    }
    
    return 0;
}

 

标签:case,int,sum,subsequence,Maximum,Subsequence,numbers,Maxsize,Sum
From: https://www.cnblogs.com/xinrenbool/p/16896826.html

相关文章

  • CF631E Product Sum
    前言不知道为什么题解里的李超线段树都要分两种情况讨论,我觉得的大可不必,其实一遍就好了。分析设\(ans_i\)为\(i\)移动到其他位置时获得的最大取值,\(sum_i\)为\(......
  • [Dubbo] 整理 简化 配置Provider和Consumer(SpringBoot + Dubbo + Zookeeper 搭建环境)
    SpringBoot+Dubbo+Zookeeper搭建环境Dubbo2.7使用的AlibabaDubbo,后来@Service等注解被标识@Deprecated。现改用Dubbo3.0.6,出现了一些版本匹配的问题。可以......
  • Maximum Number of Non-overlapping Palindrome Substrings
    MaximumNumberofNon-overlappingPalindromeSubstrings、Youaregivenastring s anda positiveintegerk .Selectasetofnon-overlappingsubstringsfr......
  • LeetCode 167.TowSum
    双指针classSolution{public:vector<int>twoSum(vector<int>&numbers,inttarget){intl=0,r=numbers.size()-1,sum=0;while(l<r){......
  • 64. Minimum Path Sum
    Givena m x n gridfilledwithnon-negativenumbers,findapathfromtoplefttobottomrightwhich minimizes thesumofallnumbersalongitspath.No......
  • kafka-consumer-groups 命令行工具使用手册,Kafka 管理必备
    kafka-consumer-groups命令行工具使用手册该手册原文出自​​$KAFKA_HOME\bin\windows\kafka-consumer-groups.bat--help​​命令的输出结果,并由​​Redisant​​提供......
  • 2.搭建服务消费者user-consumer
    搭建服务消费者user-consumer1.创建user-consumer模块,导入依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spri......
  • 1007 Maximum Subsequence Sum
    题目:1007MaximumSubsequenceSumGivenasequenceof K integers{ N1​, N2​,..., NK​ }.Acontinuoussubsequenceisdefinedtobe{ Ni​, Ni+1​,........
  • CodeForces - 1092F Tree with Maximum Cost
    题意:给出一棵树,每个结点有一个权值。定义一棵树以ai为根节点的价值为 剩下每个结点到根节点的距离乘权值 之和。求这棵树的最大价值。解:随便选一个结点为根,从下到上统......
  • [ABC271E] Subsequence Path
    洛谷链接原题链接题目描述某地区有\(N\)个城镇,编号为1到\(N\),并且由\(M\)条公路连接,编号1到\(M\)。每条公路都是有向的;而且编号为\(i(1\lei\leM)\)......