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;
}

 

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