UVA 10670 the Jackpot

Source: Internet
Author: User

Original question:
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, he can identify patterns of consecutive wins and El Aborate a Win-win strategy. But Manuel, as smart as he thinks he was, does not know how to program computers. So he hired the write programs that would assist him in elaborating his strategy. Your first task is to write a program this identifies the maximum possible gain out of a sequence of bets. A bet is an amount of money and are either winning (and this is recorded as a positive value), or losing (and this is Recor Ded 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 was an integer greater than 0 and less than 1000.
The input is terminated with N = 0.
Output
For each given input set, the output would echo a line with the corresponding solution. If the sequence
Shows no possibility to win money and 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.
Effect:
Bare maximum sub-segments and problems.

//dynamic Programming solution #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 by divide-and-conquer solution;
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;
    }
}

Answer:
Nothing to say, all do rotten ~ ~ The solution of the division can write a

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.