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

解題思路:

  • 首先我沒有想到動態規劃,就先使用普通的算法來寫的,但是隻能達到部分正確。

  • 坑:需要輸出的不是起始和終點的下標,而是起始和終點的數值!( 我暈@A@)

  • 坑:看原始數列是否都是負數,如果都是負數,就按照要求輸出“0 首 尾”即可;如果原始序列中有一個是0(只要有一個就成),其他是負數或者0,那麼,輸出“0 0 0”

 

#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
	int k, num,i, leftTemp =0 , maxSum = 0, sum =0;
	vector<int> numbers;
	
	scanf("%d", &k);
	for(int i=0; i<k; i++)
	{
		scanf("%d", &num);
		numbers.push_back(num);
	}
	
	for(i=0; i<numbers.size(); i++)
		if(numbers[i] > 0) break;
	
	if(i == numbers.size())		//全部都是非正數	
	{
		printf("%d %d %d", 0,numbers[0], numbers[k-1]);	//	sum, first, last 
		return 0;
	}
	
	vector<int> minLeft;
	vector<int> minRight;	
	for(i=0; i<numbers.size(); i++)
	{
		if(sum<0)
		{
			sum = numbers[i];
			leftTemp = i;
		}
		else
			sum += numbers[i];
		
		if(sum >= maxSum) 
		{
			maxSum = sum;
			minLeft.push_back(leftTemp);
			minRight.push_back(i);
		}		
	}
		
	int x=0, flag=0;
	for(i=0; i<minLeft.size(); i++)
	{
		if(flag == 1)	break;
		for(int j=i; j<minLeft.size(); j++)
		{
			if(minLeft[i] != minLeft[j])
			{
				x = j;
				flag=1;
				break;
			}
		}
	}
	
	int result_x = minLeft[0];
	int result_y1 = minRight[x-1];
	int result_y2 = minRight[minRight.size()-1];
	
	if(flag == 1)
		printf("%d %d %d", maxSum, numbers[result_x], numbers[result_y1]);
	else 
		printf("%d %d %d", maxSum, numbers[result_x], numbers[result_y2]);
	return 0;

}

使用動態規劃(DP)來寫的代碼:

 

#include<stdio.h>
const int maxn = 10010;

int main(){
    int n, num[maxn], pos = 0, dp[maxn] = {0}, ans[maxn][2];
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d", &num[i]);
        if(num[i] >= 0) pos++;
    }
    //輸入的數全0 
    if(pos == 0){
        printf("0 %d %d\n", num[0], num[n - 1]);
    }else{
        dp[0] = num[0];
        ans[0][0] = ans[0][1] = num[0]; //用於保存結果 
        for(int i = 1; i < n; i++){
            if(dp[i - 1] >= 0){
                dp[i] = dp[i - 1] + num[i];
                ans[i][1] = num[i];     //加進來了,起點爲前一個點的起點,終點設置爲當前遍歷的元素 
                ans[i][0] = ans[i - 1][0];
            }else{
                dp[i] = num[i];
                ans[i][0] = ans[i][1] = num[i];     //沒有加進來,起點設置爲當前點,終點也設置爲當前點 
            }
        }
        //遍歷一遍dp數組,找出最大值
        int id, max = -1;
        for(int i = 0; i < n; i++){
            if(dp[i] > max){
                max = dp[i];
                id = i;
            }
        } 
        printf("%d %d %d\n", max, ans[id][0], ans[id][1]);
    }
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章