UVA10684 The jackpot【前缀和+最值】

As Manuel wants to get rich fast and without too much work, he decided to make a career in gambling. Initially, he plans to study the gains and losses of players, so that, he can identify patterns of consecutive wins and elaborate a win-win strategy. But Manuel, as smart as he thinks he is, does not know how to program computers. So he hired you to write programs that will assist him in elaborating his strategy.
    Your first task is to write a program that identifies the maximum possible gain out of a sequence of bets. A bet is an amount of money and is either winning (and this is recorded as a positive value), or losing (and this is recorded as a negative value).
Input
The input set consists of a positive number N ≤ 10000 , that gives the length of the sequence, followed by N integers. Each bet is an integer greater than 0 and less than 1000.
    The input is terminated with N = 0.
Output
For each given input set, the output will echo a line with the corresponding solution. If the sequence shows no possibility to win money, then the output is the message ‘Losing streak.’
Sample Input
5
12 -4
-10 4
9
3
-2 -1 -2
0
Sample Output
The maximum winning streak is 13.
Losing streak.

问题链接UVA10684 The jackpot
问题简述:输入一个正整数n,再输入n个整数,求最大子序列和。
问题分析
    这个问题看似一个动态规划问题,可以用动态规划来解决,确实是可以的,但是代码比较繁琐。还有一种解法是采用分治法来实现,可以参考归并排序的思想来实现,但是代码也比较繁琐。
    也有更简单的解法,就是使用前缀和的概念来解,可以不使用数组来存储,维护前缀和的最小值和结果即可。
程序说明:给2个程序,使得问题更容易理解。
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10684 The jackpot */

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    while(~scanf("%d", &n) && n) {
        int a, sum = 0, lmin = 0, ans = INT_MIN;
        for(int i = 1; i <= n; i++) {
            scanf("%d", &a);
            sum += a;
            ans = max(ans, sum - lmin);
            lmin = min(lmin, sum);
        }

        if (ans > 0)  printf("The maximum winning streak is %d.\n", ans);
        else  printf("Losing streak.\n");
    }

    return 0;
}

AC的C++语言程序如下:

/* UVA10684 The jackpot */

#include <bits/stdc++.h>

using namespace std;

const int N = 10000;
int psum[N + 1];

int main()
{
    int n, a;
    while(~scanf("%d", &n) && n) {
        psum[0] = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a);
            psum[i] = psum[i - 1] + a;
        }

        int ans = INT_MIN;
        for(int i = 1; i <= n; i++)
            for(int j = i + 1; j <=n; j++)
                ans = max(ans, psum[j] - psum[i]);

        if (ans > 0)  printf("The maximum winning streak is %d.\n", ans);
        else  printf("Losing streak.\n");
    }

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