[ An Ac a Day ^_^ ] hdu 1003 dp

超时还有可能是数组开小了……

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<math.h>
 5 #include<string.h>
 6 #include<string>
 7 #include<map>
 8 #include<set>
 9 #include<vector>
10 #include<queue>
11 #define M(a) memset(a,0,sizeof(a))
12 using namespace std;
13 typedef long long ll;
14 int num[100005];
15 int dp[100005];
16 int from[100005];
17 int main(){
18     int T,case_=1;
19     scanf("%d",&T);
20     while(T--){
21         int n;
22         scanf("%d",&n);
23         scanf("%d",&num[1]);
24         int nowmax=num[1],nowa=1,nowb=1;
25         int ans=num[1],a=1,b=1;
26         for(int i=2;i<=n;i++){
27             scanf("%d",&num[i]);
28             if(num[i]+nowmax<num[i]){
29                 nowmax=num[i];
30                 nowa=i;
31             }
32             else nowmax+=num[i];
33             if(ans<nowmax){
34                 ans=nowmax;
35                 a=nowa;
36                 b=i;
37             }
38         }
39         printf("Case %d:\n",case_++);
40         printf("%d %d %d\n",ans,a,b);
41         if(T!=0) puts("");
42     }
43     return 0;
44 }
45 /*
46
47 11
48 1 6
49 79 12 -22 -8 -2 -19 -7 5 -5 17 14 -22 7 -5 -13 -25 -24 21 12 10 13 12 22 20 -15 -12 -10 -15 -13 -17 14 2 -25 -22 11 -3 -15 18 23 15 8 -2 19 4 -18 -20 21 -1 1 19 20 -4 -9 5 -7 -19 9 20 -21 0 -22 -5 -13 15 0 -1 24 -17 8 8 -17 19 -4 -16 20 15 21 -11 -12 -23
50 67 -5 -23 24 21 11 6 21 11 -7 -25 -23 8 -7 5 -13 24 -24 13 1 -5 6 -11 -1 1 10 21 -9 -7 -15 -4 -25 7 -5 -3 11 20 16 7 14 7 -8 0 -23 -3 6 -24 8 -5 6 -22 -16 15 -19 -3 -1 -2 -2 20 24 -8 6 -19 -15 -25 -14 -25 13
51 17 1 -21 -14 13 -19 -18 -24 -21 -13 8 -2 -4 8 4 -4 -23 24
52 28 15 11 16 10 -16 -4 0 -25 -16 23 20 1 -7 -12 6 -20 -22 17 2 2 -23 -25 17 12 -21 6 -3 5
53 36 -6 22 1 -6 -5 1 5 -20 -15 -6 11 -5 -11 -10 3 16 20 -4 8 20 17 8 -7 9 -4 13 2 21 16 -20 -25 21 -13 3 -18 17
54 71 14 -22 24 -3 -4 14 -22 24 13 -22 -13 14 -4 20 -19 4 17 -9 0 -18 18 7 -19 -20 -22 -25 -16 -10 7 -15 -16 -17 -1 -7 22 -12 13 -14 8 -4 12 -16 -5 -4 17 -13 4 -21 -18 -8 -9 -13 -23 5 -25 -18 5 24 -14 5 -7 -24 -2 -12 24 -16 20 21 -2 -15 -11
55 64 -22 19 18 6 12 9 -5 10 -24 22 22 5 -3 -20 -21 18 24 3 9 7 13 -2 7 5 24 -12 6 -9 -10 12 -3 -21 -4 24 -7 -5 -8 22 -1 -16 -16 10 -11 -21 24 -3 3 -9 22 -7 6 -3 -6 6 23 -12 3 -17 -10 -9 -18 23 6 -9
56 71 17 10 -21 -10 -3 1 8 -21 16 -4 -23 -19 4 7 17 18 5 -25 -15 7 -2 -10 11 14 -11 -2 -11 11 14 20 19 -19 -18 7 -6 6 18 -20 12 -14 -6 -14 -13 -10 -23 -11 -24 9 6 -6 6 -25 8 14 -8 14 -17 2 -8 24 -11 -21 -18 5 6 4 -20 -11 3 -3 19
57 12 -25 -9 -20 5 15 15 -15 -10 -15 -17 -13 23
58 58 22 -20 -22 -25 1 -6 -7 -23 6 16 -17 -5 -23 -24 -9 -2 -16 23 7 14 16 19 -21 -5 6 10 -10 -23 0 -18 9 17 10 1 19 -23 15 7 -3 15 -17 -20 -23 22 -16 -1 -14 10 0 -17 2 -19 16 -6 -23 -25 -6 -12
59
60 */
时间: 2024-10-04 23:21:43

[ An Ac a Day ^_^ ] hdu 1003 dp的相关文章

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 &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

题目链接: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

HDU 1003:Max Sum(DP)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 142742    Accepted Submission(s): 33225 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(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经典题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

hdu 1003 Max Sum 最大字段和 dp

今天看了一上午dp.看不太懂啊.dp确实不简单.今天開始学习dp,搜了杭电的dp46道,慢慢来吧.白书上的写的 又不太具体,先写几道题目再说. .. 题目连接:id=516&page=1">点击打开链接 思路:就是当当前的和是小于0的时候就又一次计数.大于或者等于0的时候都相加... id=516&page=1">代码: /* Name: Copyright: Author: Date: 08/08/15 08:41 Description: */ #inc

HDU 1003 Max Sum &amp;&amp; HDU 1231 最大连续子序列 (DP)

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

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