1007 Maximum Subsequence Sum (25 分)
 

Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to be { N​i​​, N​i+1​​, ..., N​j​​ } 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


求最大的连续子序列的和,并输出子序列的首尾元素,就是没注意这点,一直输出首尾索引号,一直WA
思路有两种:
1)子序列的和sum = sum[j] - sum[i] (j >= i),所以只需在求每个sum[j]的时候,找到j前面最小的那个sum[i],即可,这里的最小的sum[i]应该与求sum[j]的时候同时维护,否则就是暴力破解。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXN 10005
struct node{
int num;
int l;
int r;
int l_sum;
} seq[MAXN];
int sum[MAXN];
int main()
{
int n;
int flag = ;
int min;
int index;
min = ;
index = -;
scanf("%d", &n);
for (int i = ; i < n; i++){
scanf("%d", &seq[i].num);
if(seq[i].num >= )
flag = ;
sum[i] = sum[i - ] + seq[i].num;
seq[i].l_sum = sum[i] - min;
seq[i].l = index + ;
seq[i].r = i;
if(sum[i] < min){
min = sum[i];
index = i;
}
}
if(!flag){
printf("0 %d %d\n", seq[].num, seq[n - ].num);
}
else{
int max;
int l, r;
max = -;
for (int i = ; i < n; i++){
if(max < seq[i].l_sum){
max = seq[i].l_sum;
l = seq[i].l;
r = seq[i].r;
}
}
printf("%d %d %d\n", max, seq[l].num, seq[r].num);
}
system("pause");
return ;
}

2)见https://blog.csdn.net/liuchuo/article/details/52144554



1007 Maximum Subsequence Sum (PAT(Advance))的更多相关文章

  1. python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)

    python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...

  2. PAT甲 1007. Maximum Subsequence Sum (25) 2016-09-09 22:56 41人阅读 评论(0) 收藏

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  3. PAT 甲级 1007 Maximum Subsequence Sum (25)(25 分)(0不是负数,水题)

    1007 Maximum Subsequence Sum (25)(25 分) Given a sequence of K integers { N~1~, N~2~, ..., N~K~ }. A ...

  4. PAT 1007 Maximum Subsequence Sum(最长子段和)

    1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  5. PAT Advanced 1007 Maximum Subsequence Sum

    题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...

  6. 1007 Maximum Subsequence Sum (25分) 求最大连续区间和

    1007 Maximum Subsequence Sum (25分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  7. 1007 Maximum Subsequence Sum (25 分)

    1007 Maximum Subsequence Sum (25 分)   Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A ...

  8. PTA (Advanced Level) 1007 Maximum Subsequence Sum

    Maximum Subsequence Sum Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous su ...

  9. PAT 解题报告 1007. Maximum Subsequence Sum (25)

    Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...

随机推荐

  1. Docker Image发布

    Docker Image发布 方法1:导出镜像 #docker save -o centos-httpd-docker-image.tar centos:httpd 使用加载本地镜像 docker l ...

  2. 7-74 JavaScript 事件

    7-74 JavaScript 事件 学习要点 掌握常用的javaScript事件 基本概念 事件是一些特定动作发生时所发出的信号,JavaScript中的事件是可以被 JavaScript 侦测到的 ...

  3. scapy基础-网络数据包结构

    网络层次模型,数据包的组成是学习scapy的基础,下文主要关注模型中各个层次的用途,ethernet II和ip包数据结构.    1.五层模型简介 名称 作用 包含协议 应用层 面向程序对程序的传输 ...

  4. 红米note手机连接mac系统

    到http://www.android.com/filetransfer/   下androidfiletransfer.dmg文件,安装好这个软件,然后再连接usb就可以用这个软件管理手机内存卡和s ...

  5. Redis: Redis on Windows Setup

    ylbtech-Redis: Redis on Windows Setup 1.返回顶部 1. 2. 3. 4. 5. 6. 7. 8. 9. 2.返回顶部   3.返回顶部   4.返回顶部   5 ...

  6. 【158】◀▶ Linux-Bash学习

    鸟哥的 Linux 私房菜      Linux 的 26 个命令      Shell 脚本教程      Linux 命令大全 目录——按文件顺序: echo:显示变量内容 printf:格式化输 ...

  7. E20170602-ts

    questionnaire  n. 调查问卷; 调查表;  アンケート不是英语 collection   n. 征收; 收集,采集; 收藏品; 募捐; association   n. 联想; 协会, ...

  8. vue项目开发中踩过的坑

    一.路由 这两天移动端的同事在研究vue,跟我说看着我的项目做的,子路由访问的时候是空白的,我第一反应是,不会模块没加载进来吧,还是....此处省略一千字... 废话不多说上代码 路由代码 { pat ...

  9. SQL 增加, 删除父子级带事务的存储过程

    if (object_id('proc_DeleteFile', 'P') is not null)    drop proc proc_DeleteFile gocreate  PROCEDURE ...

  10. ASP.NET MVC5 之 客户端实现文件的下载

    MVC 实现下载功能主要借助于 File 属性: //下载文件接口 public ActionResult GetTrackTempIsc(ICSModels icsModels) { bool fl ...