pat1007. Maximum Subsequence Sum (25)
1007. Maximum Subsequence Sum (25)
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 <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <iostream>
using namespace std;
#define size 10000
int line[size+];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int k;
int i,j,l,fl,fr;
bool t=false;
scanf("%d",&k);
for(i=;i<k;i++){
scanf("%d",&line[i]);
if(line[i]>=){
t=true;
}
}
/*if(!t){
cout<<0<<" "<<line[0]<<" "<<line[k-1]<<endl;
return 0;
}*/
long long tempmax=line[],fmax=line[];
l=;
fl=fr=; //这句话没加 case3(从0开始)过不了
line[k]=;
for(i=;i<=k;i++){
if(tempmax>=){
if(tempmax>fmax){
fmax=tempmax;
fl=l;
fr=i-;
}
}else{
tempmax=;
l=i;
}
tempmax+=line[i];
}
if(fmax<){
fmax=;
fl=;
fr=k-;
}
cout<<fmax<<" "<<line[fl]<<" "<<line[fr]<<endl;
return ;
}
方法二要比方法三简明,更容易理解。
方法二:
#include <stdio.h>
#include <string.h>
int num[];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n;
scanf("%d",&n);
int i,j=;
for(i=;i<n;i++){
scanf("%d",&num[i]);
if(num[i]<){
j++;
}
}
if(j==n){
printf("%d %d %d\n",,num[],num[n-]);
}
else{
int tempsum=;
int tempi=,tempj;
int sum,l,r;
sum=l=;
r=n-;
for(i=;i<n;i++){
if(tempsum>=){
tempsum+=num[tempj=i];
}else{
tempsum=num[tempi=tempj=i];
}
if(tempsum>sum||(tempsum==&&r==n-)){//注意单个0的情况
sum=tempsum;
l=tempi;
r=tempj;
}
}
printf("%d %d %d\n",sum,num[l],num[r]);
}
return ;
}
方法三:
#include <stdio.h>
#include <string.h>
int num[];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
int n;
scanf("%d",&n);
int i,j=;
for(i=;i<n;i++){
scanf("%d",&num[i]);
if(num[i]<){
j++;
}
}
if(j==n){
printf("%d %d %d\n",,num[],num[n-]);
}
else{
num[n]=;
int tempsum=;
int tempi,tempj;
int sum,l,r;
sum=l=;
r=n-;
for(i=;i<=n;i++){
if(tempsum>){
if(tempsum>sum){//当i元素前面的局部和>0时,有可能更新
sum=tempsum;
l=tempi;
r=tempj;
}
tempsum+=num[tempj=i];
}else{//否则,i元素前面的局部和<=0
if(tempsum==&&sum==&&tempi==tempj&&l!=r){//i元素前面的局部和==0,并且局部和只有一个元素,并且最终和未更新,则更新最新和
l=r=tempi;
tempsum+=num[tempj=i];
}
else{//其他情况,一律不更新
tempi=tempj=i;
tempsum=num[i];
}
}
}
printf("%d %d %d\n",sum,num[l],num[r]);
}
return ;
}
pat1007. Maximum Subsequence Sum (25)的更多相关文章
- 中国大学MOOC-陈越、何钦铭-数据结构-2015秋 01-复杂度2 Maximum Subsequence Sum (25分)
01-复杂度2 Maximum Subsequence Sum (25分) Given a sequence of K integers { N1,N2, ..., NK }. ...
- PAT1007:Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 【DP-最大子串和】PAT1007. Maximum Subsequence Sum
1007. Maximum Subsequence Sum (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 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 ...
- PTA 01-复杂度2 Maximum Subsequence Sum (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/663 5-1 Maximum Subsequence Sum (25分) Given ...
- 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 ...
- 1007. Maximum Subsequence Sum (25)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, ...
随机推荐
- 构建空Datatable
方法一 DT = new DataTable(); DataRow DR = DT.NewRow(); //构建列(名称,数据类型) DT.Columns.Add("代码", Ty ...
- 内存布局------c++程序设计基础、编程抽象与算法策略
图中给出了在一个典型c++程序中如何组织内存的框架.程序中的指令(在底层都是按位存储的).全局变量.静态对象和只读常量往往被存储在静态去(static area)(第二个图中的数据段.代码段.值得注意 ...
- Codeforces Round #551 (Div. 2)A. Serval and Bus
A. Serval and Bus time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- SimpleDateFormat线程不安全及解决办法
原文链接:https://blog.csdn.net/csdn_ds/article/details/72984646 以前没有注意到SimpleDateFormat线程不安全的问题,写时间工具类,一 ...
- Idea 软件Project项目的jar依赖关系设置方法
1.查看所依赖的jar文件 (1)File--->Project Structure (2)Modules--->project01---->dependencies,可见所缺少的j ...
- 008 Android activity实现多个界面的相互跳转(主要利用Intent)
1.activity介绍 一个activity就把他理解成一个页面 2.新建activity流程 如图所示在com.lucky.test06的目录下,右击new--->Activity---&g ...
- 老男孩python作业2-购物车程序
购物车程序要求: 1.启动程序后,输入用户名密码后,如果是第一次登录,让用户输入工资,然后打印商品列表 2.允许用户根据商品编号购买商品 3.用户选择商品后,检测余额是否够,够就直接扣款,不够就提醒 ...
- table中thead固定一直在最上面
<link rel="shortcut icon" href="favicon.ico"> <link href="css/boot ...
- 未在本地计算机上注册microsoft.jet
未在本地计算机上注册microsoft.jet http://www.microsoft.com/zh-cn/download/confirmation.aspx?id=13255
- VBS映射网络驱动器 映射网络驱动器
Dim objNetwork Set objNetwork = CreateObject("Wscript.Network") if objNetwork.EnumNetworkD ...