[DP]HOJ 1760The jackpot

传送门:The jackpot

The jackpot

My Tags   (Edit)
  Source : UVA
  Time limit : 1 sec   Memory limit : 32 M

Submitted : 5803, Accepted : 1688

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.
Thanks To

Problem setter: David Deharbe


解题报告:

此题为简单的求最大子段和。状态转移方程:dp[i+1]=arr[i]+max(dp[i],0);代码如下:

#include
#include
#include
using namespace std;
int dp[10005];
int main(){
    int n,s,ans;
    while(scanf("%d",&n)==1&&n){
        ans=-1;
        memset(dp,0,sizeof(dp));
        for(int i=0;ians)
                ans=dp[i+1];
        }
        if(ans>0)
            printf("The maximum winning streak is %d.\n",ans);
        else
            printf("Losing streak.\n");
    }
    return 0;
}


你可能感兴趣的:(2014寒假,动态规划)