1007 Maximum Subsequence Sum (PAT(Advance))
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
求最大的连续子序列的和,并输出子序列的首尾元素,就是没注意这点,一直输出首尾索引号,一直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))的更多相关文章
- python编写PAT 1007 Maximum Subsequence Sum(暴力 分治法 动态规划)
python编写PAT甲级 1007 Maximum Subsequence Sum wenzongxiao1996 2019.4.3 题目 Given a sequence of K integer ...
- 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 ...
- 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 ...
- PAT 1007 Maximum Subsequence Sum(最长子段和)
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT Advanced 1007 Maximum Subsequence Sum
题目 1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., N**K }. A contin ...
- 1007 Maximum Subsequence Sum (25分) 求最大连续区间和
1007 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- 1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A ...
- PTA (Advanced Level) 1007 Maximum Subsequence Sum
Maximum Subsequence Sum Given a sequence of K integers { N1, N2, ..., NK }. A continuous su ...
- PAT 解题报告 1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
随机推荐
- Interfaces (C# Programming Guide)
https://msdn.microsoft.com/en-us/library/ms173156.aspx An interface contains definitions for a group ...
- 自定义的Notification
要创建一个自定义的Notification,可以使用RemoteViews.要定义自己的扩展消息,首先要初始化一个RemoteViews对象,然后将它传递给Notification contentVi ...
- python-----文件自动归类
如何移动文件? → 使用内置模块来实现 归类的规则是什么? → 手动(预设文件夹)/ 自动(创建文件夹) import shutil import os path = './' #由于这里是相对路径 ...
- Python基础第六天
一.内容 二.练习 练习1 题目:文件的增删改查 图示: 代码: import os def add(data): content = data[1] # 文件内容 file_name = data[ ...
- iptables的介绍
iptables介绍 iptables 1)iptables程序工作在内核的TCP/IP网络协议栈框架netfilter上,通过网络过滤可以实现入侵检测以及入侵防御功能,而不是单个协议当中. 2)ip ...
- POJ 2452 Sticks Problem (暴力或者rmq+二分)
题意:给你一组数a[n],求满足a[i] < a[k] < a[j] (i <= k <= j)的最大的 j - i . 析:在比赛时,我是暴力做的,虽然错了好多次,后来说理解 ...
- js 本地存储 localStorage 之 angular
今天项目中用到 php yii框架 用的不是 angular路由 所以用rootScope传值是不行的 我就用到了 localStorage 本地持久化存储 如下 set 顾名思义是设置 值 loca ...
- 使用 script 的 module 属性实现 es6 以上的兼容
几个月前看到了这篇文章 https://philipwalton.com/articles/deploying-es2015-code-in-production-today/,给了我很大的启发,本来 ...
- $Hdu1381\ Crazy\ Search$
前置芝士 :string 的 基本用法 string s = "hello world" ; string tmp(s,0,5) ; cout << tmp <& ...
- ACM_01背包
背包1 Time Limit: 2000/1000ms (Java/Others) Problem Description: 有n个重量和价值分别为Wi,Vi的物品,现从这些物品中挑选出总量不超过 W ...