UVA 10684 || HOJ 1760 The jackpot
来源:互联网 发布:知秋小说猛兽 编辑:程序博客网 时间:2024/05/22 15:03
这题是要求最大子序列和。在这一遍扫描数组当中,从左到右记录当前子序列的和dp[i],若这个和不断增加,那么最大子序列的和max1也不断增加(不断更新max)。如果往前扫描中遇到负数,那么当前子序列的和将会减小。此时dp[i] 将会小于max1,当然max1也就不更新。如果dp[i]降到0时,说明前面已经扫描的那一段就可以抛弃了,这时将dp[i]赋值为a[i]。然后,dp[i]将从后面开始将这个子段进行分析,若有比当前max1大的子段,继续更新max1。这样一趟扫描结果也就出来了。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int dp[10010],a[10010];int main(){ int n ; while(~scanf("%d",&n)) { if(n == 0) return 0; for(int i = 0 ; i < n ; i++) scanf("%d",&a[i]); int max1; max1 = dp[0] = a[0]; for(int i = 1 ; i < n ; i++) { if(dp[i-1]>0) dp[i] = dp[i-1] + a[i]; else dp[i] = a[i] ; max1 = max(max1 ,dp[i]); } if(max1 <= 0) printf("Losing streak.\n"); else printf("The maximum winning streak is %d.\n",max1); } return 0;}
0 0
- UVA 10684 || HOJ 1760 The jackpot
- Hoj 1760 The jackpot
- [DP]HOJ 1760The jackpot
- UVA 10684 The jackpot
- UVa:10684 The jackpot
- UVa 10684 - The jackpot
- uva 10684 The jackpot
- 【UVa】10684 - The jackpot
- hoj 1760 The jackpot一维
- uva 10670 The jackpot
- TOJ 1782.The jackpot
- UVa 141 & HOJ 1186 - The Spot Game
- hit oj 1760 The jackpot (最大子段和)
- UVA10684 The jackpot 贪心/dp
- HOJ The Colored Cubes
- HOJ 3074 The Position
- HOJ 2785 The Game
- Hoj 1140 The Game
- java.lang.IllegalMonitorStateException
- 图片+css实现半透明引导页
- 1026_程序运行时间(15)
- 1027_打印沙漏(20)
- Home Remedies To Improve Your Memory
- UVA 10684 || HOJ 1760 The jackpot
- HDU 2067 小兔的棋盘
- uva816
- 基本图论定义与术语(Basic Definition and Glossary in Graph The)
- 1029_旧键盘(20)
- java--正则表达式初步学习
- deepinlinux下安卓开发总结
- Python入门
- MAC下的java环境