uva 10670 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.
大意:
裸的最大子段和问题。

//动态规划解法
#include <bits/stdc++.h>

using namespace std;
//fstream in,out;
int main()
{
    ios::sync_with_stdio(false);
    int ans,tmp,sum,n;
    while(cin>>n,n)
    {
        cin>>tmp;
        ans=sum=tmp;
        for(int i=2;i<=n;i++)
        {
            cin>>tmp;
            if(sum>0)
                sum+=tmp;
            else
                sum=tmp;
            ans=max(ans,sum);
        }
        if(ans>0)
            cout<<"The maximum winning streak is "<<ans<<"."<<endl;
        else
            cout<<"Losing streak."<<endl;
    }
    return 0;
}
//分治解法
#include <bits/stdc++.h>

using namespace std;
//fstream in,out;

int a[10001];
int maxsum(int a[],int x,int y)
{
    int i,m,v,l,r,ans;
    if(y-x==1)
        return a[x];
    m=x+(y-x)/2;
    int mx=maxsum(a,x,m);
    int my=maxsum(a,m,y);
    ans=max(mx,my);
    v=0;l=a[m-1];
    for(i=m-1;i>=x;i--)
    {
        v+=a[i];
        if(v>l)
            l=v;
    }
    v=0;r=a[m];
    for(i=m;i<y;i++)
    {
        v+=a[i];
        if(v>r)
            r=v;
    }
    return max(ans,l+r);
}
int main()
{
    ios::sync_with_stdio(false);
    int n;
    while(cin>>n,n)
    {
        for(int i=0;i<n;i++)
            cin>>a[i];
        int ans=maxsum(a,0,n);
        if(ans>0)
            cout<<"The maximum winning streak is "<<ans<<"."<<endl;
        else
            cout<<"Losing streak."<<endl;
    }
}

解答:
没啥好说的,都做烂了~~ 那个分治的解法可以写一写

你可能感兴趣的:(uva)