HDU - 1003 Max Sum(DP经典题2)

题意:给出一串数,求最大和的字串和起始终点位置。

与导弹问题大同小异,有状态转移方程就很好做了。

状态转移方程:d[i]=(d[i-1]+a[i]>a[i])?d[i-1]+a[i]:a[i];

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4
 5 const int maxn=100000+10;
 6
 7 int dp[maxn],num[maxn];
 8
 9 int main(){
10     int t,n,flag=1;
11     cin>>t;
12     while(t--){
13         cin>>n;
14         for(int i=1;i<=n;i++)
15         cin>>num[i];
16         dp[1]=num[1];
17         for(int i=2;i<=n;i++){
18             if(dp[i-1]<0) dp[i]=num[i];//之前的数之和已经小于0了,所以直接从新赋值
19             else dp[i]=dp[i-1]+num[i];//不然就继续加数
20         }
21         int max=dp[1],end=1;
22         for(int i=2;i<=n;i++){
23             if(dp[i]>max){
24                 max=dp[i];
25                 end=i;
26             }
27         }
28         int start=end;
29         int sum=0;
30         for(int i=end;i>=1;i--){
31             sum=sum+num[i];
32             if(sum==max) start=i;
33         }
34         printf("Case %d:\n",flag++);
35         printf("%d %d %d\n",max,start,end);
36         if(t) printf("\n");
37     }
38     return 0;
39 }
时间: 2024-10-13 00:01:21

HDU - 1003 Max Sum(DP经典题2)的相关文章

HDU 1003 Max Sum(dp,最大连续子序列和)

Max Sum Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input

HDU 1003 Max Sum(经典DP,)

Max Sum Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input contains

hdu 1003 Max Sum(dp)

Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input contains

HDU 1003 Max Sum (动规)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 141547    Accepted Submission(s): 32929 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

HDU 1003 Max Sum 最大连续子序列的和

Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14. Input The first line of the input contains

[ACM] hdu 1003 Max Sum(最大子段和模型)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 135262    Accepted Submission(s): 31311 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

HDU - 1003 - Max Sum &amp;&amp; POJ - 1050 - To the Max (经典DP问题)

题目传送:HDU - 1003 思路:最大子序列和 dp[i]= a[i]   (dp[i-1]<0) dp[i]= dp[i-1]+a[i]   (dp[i-1]>=0) AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include <cmath> #include <queue> #include

HDU 1003 Max Sum(DP)

题目地址:HDU 1003 DP好弱..先补补DP的基础... 这题就是记录起始点与终止点,然后每当发现一个更大的,就更新.从左往右扫一遍就行了. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #

hdu 1003 Max Sum 简单DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 转移方程:dp[i]=max(dp[i-1]+a[i],a[i]) 虽然是dp 但不用真的申请一个dp数组 #include <cstdio> #include <cstdlib> #include <ctime> #include <iostream> #include <cmath> #include <cstring> #in