01-複雜度2 Maximum Subsequence Sum(最大子列和問題變化)
一、題目:
01-複雜度2 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
作者: 陳越
單位: 浙江大學
時間限制: 200 ms
記憶體限制: 64 MB
二、程式碼
本人程式碼並沒有拿到測試滿分(25),只拿到23分,測試點2並列和對應相同i但是不同j,即尾是0這個測試點沒有通過。我一直沒有找到例子和程式碼的錯誤,希望看懂的發我測試樣例,後期更新
#include<iostream>
using namespace std;
void MaxSubsequenceSum(int A[],int N)
{
int flag = 0; //flag標誌量
int ThisSum = 0, MaxSum = 0; //當前和,序列最大和
int start = 0, end = 0,count = 0; //保留起點和終點
for(int i = 0;i < N; i++) //判斷全是負數
{
if(A[i] < 0)
count++;
if(count == N)
{
cout <<0<<" "<<A[0]<<" "<<A[N-1];
return;
}
}
//線上處理演算法
for(int i = 0;i < N; i++)
{
ThisSum = ThisSum + A[i];
if(ThisSum < 0)
{
ThisSum = 0;
if(!flag) // 這個標誌量主要用於start,標誌是否丟棄前面ThisSum
flag = 1;
if(A[i + 1] == 0) //判斷當前ThisSum<0並且下一個數為零的情況,對start和end更新
{
flag = 0;
end = start = i + 1;
}
}
if(ThisSum > MaxSum)
{
if(flag)
start = i;
MaxSum = ThisSum;
end = i;
flag = 0; //每次更新完start、end後,重置標誌變數
}
}
cout<<MaxSum<<" "<<A[start]<<" "<<A[end];
}
int main()
{
int n;
cin>>n;
int *A = new int[n];
for(int i = 0;i < n;i++)
cin>>A[i];
MaxSubsequenceSum(A,n);
}
相關推薦
01-複雜度2 Maximum Subsequence Sum(最大子列和問題變化)
一、題目: 01-複雜度2 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is de
01-複雜度2 Maximum Subsequence Sum(25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } wher
PTA 01-複雜度2 Maximum Subsequence Sum (25 分)
01-複雜度2 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, …, NK }. A continuous subsequence is defin
01-複雜度2 Maximum Subsequence Sum (25 分) 中國大學MOOC-陳越、何欽銘-資料結構-2018秋
01-複雜度2 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined t
01-複雜度2 Maximum Subsequence Sum (25 分)
這題和求最大子列和一樣,就是多了找出該最大子列和的首尾元素,若最大子列和Maxsum<0,就輸出該陣列的首尾元素a[0] 和 a[ n-1 ]. 第一個方法是二重迴圈 #include<bits/stdc++.h> using namespa
[資料結構]01-複雜度2 Maximum Subsequence Sum
#include<stdio.h> #include <string.h> #define size 100050 int begin = 0, end = 0; int b = 0, e = 0; //定義成全域性變數方便修改 int FindMax(int
pta作業:01-複雜度2 Maximum Subsequence Sum
#include"stdio.h" #include"stdlib.h" int main() {int num=0,max=0,sum=0; scanf("%d",&num); int *a=(int *)malloc(num*sizeof(int))
【PTA】——資料結構——01-複雜度2 Maximum Subsequence Sum
01-複雜度2 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is define
中國大學MOOC-陳越、何欽銘-資料結構-2015秋 01-複雜度2 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. T
PAT 01-複雜度2. Maximum Subsequence Sum (25)&&PAT 01-複雜度1. 最大子列和問題(20)
/* 01-複雜度2. Maximum Subsequence Sum (25) 時間限制 400 ms 記憶體限制 65536 kB 程式碼長度限制 8000 B 判題程式 Standard 作者 CHEN, Yue Given a sequence of K integ
01-複雜度2 Maximum Subsequence Sum (25分)
/* * 1. 最小的i和j * 2. 若序列全為0,輸出頭尾元素。 */ #include <iostream> using namespace std; int main(void) { bool isNegative(true); //記錄
01-複雜度2 Maximum Subsequence Sum
Given a sequence ofKKintegers {N_1N1,N_2N2, ...,N_KNK}. A continuous subsequence is defined to be {N_iNi,N_{i+1}Ni+1, ...,N_jNj} wh
PAT 1007 Maximum Subsequence Sum(最大子串和)
原題地址 求出給定數字串的最大子串和,以及這個最大子串和的首尾元素。(若有多個最大子串則取最靠左的那個) 解題思路 本題基本上是最大子串和的裸題,只是增加了一個輸出首尾元素的要求。
PAT - 1007 Maximum Subsequence Sum【最大子列和】
題目 Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { N
1007 Maximum Subsequence Sum (25 point(s))
1007 Maximum Subsequence Sum (25 point(s)) 部分未通過 22分 #include<iostream> #include<vector> #include<algorithm> using namespa
1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2, ..., NK }. A continuou
PAT--1007 Maximum Subsequence Sum (25 分)
1007 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N1, N2 , …, N,K }. A continuous subsequence is defined to be { N
1007 Maximum Subsequence Sum(25 分)(PAT甲級)
Problem Description: Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+
PAT (Advanced Level) 1007 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
PAT 1007 Maximum Subsequence Sum(dp)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to be { Ni, Ni+1, ..., Nj } wh