UVA 10684 The jackpot
来源:互联网 发布:php is not running 编辑:程序博客网 时间:2024/05/20 17:59
简单DP,设dp[i]为到终点为i的最大连续和,则dp[i] = max(dp[i - 1] + A[i] , A[i]),时空复杂度为O(n)
其实可以把dp这个数组省掉,用变量B来表示前面的连续和,如果B < 0 那么很显然加上A[i]肯定比A[i]小,所以把B置为A[i] 否则则累加上A[i],在这个过程中求最大的B.
/* * UVA10684.cpp * * Created on: Jun 7, 2013 * Author: root */#include <iostream>#include <limits.h>#include <cstdio>using namespace std;const int maxn = 10010;int A[maxn], dp[maxn];int n;int getMax(){int ret = INT_MIN;int B = 0;for(int i = 0; i < n; ++i){if(B <= 0){B = A[i];}else{B += A[i];}ret = max(ret, B);}return ret;}int dp_solve(){dp[0] = A[0];int ret = dp[0];for(int i = 1; i < n; ++i){dp[i] = max(dp[i - 1] + A[i], A[i]);ret = max(ret, dp[i]);}return ret;}int main(){while(scanf("%d" , &n) && n){for(int i = 0; i < n; ++i){scanf("%d", &A[i]);}int ans = dp_solve();if(ans > 0){printf("The maximum winning streak is %d.\n", ans);}else{printf("Losing streak.\n");}}return 0;}
- UVA 10684 The jackpot
- UVa:10684 The jackpot
- UVa 10684 - The jackpot
- uva 10684 The jackpot
- 【UVa】10684 - The jackpot
- UVA 10684 || HOJ 1760 The jackpot
- uva 10670 The jackpot
- Hoj 1760 The jackpot
- TOJ 1782.The jackpot
- [DP]HOJ 1760The jackpot
- UVA10684 The jackpot 贪心/dp
- hoj 1760 The jackpot一维
- 两个AC解 - 作弊中头奖(Hit the Jackpot)
- HOJ1760 The jackpot--------最大子段和问题
- hit oj 1760 The jackpot (最大子段和)
- HDU1920 Jackpot
- POJ 2103 Jackpot
- hdu 1920 Jackpot
- C程序设计语言 Exercise 2-10
- 基本排序之冒泡排序
- 离职原因及手续办理
- 你必须知道的javascript(易忘语法)
- NYOJ 685 查找字符串
- UVA 10684 The jackpot
- WebRtc 音频引擎-linux demo
- ubuntu12.04搭建android开发环境
- oracle11g AUD$维护--转自lwei_998的专栏
- linux idr机制
- X86架构内存管理寄存器
- Windows驱动_USB驱动之二
- 和我一起来学iOS(四)UIView及其子类(下)UITableView
- Libevent最佳I/O复用(Multiplexing)机制的确定