首页 > 其他分享 >hdu1003 Max Sum--DP

hdu1003 Max Sum--DP

时间:2022-12-06 19:41:33浏览次数:69  
标签:Case cnt sequence -- Max int hdu1003 max line


原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1003​


一:原题内容



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 an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).


 



Output


For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.


 



Sample Input


2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5


 



Sample Output


Case 1: 14 1 4 Case 2: 7 1 6


二:分析理解

一个简单动态规划。。。。


三:AC代码

#include<iostream>
#include<string.h>
#include<algorithm>

using namespace std;

int a[100010];

int main()
{
int T;
scanf("%d", &T);

for (int i = 1; i <= T; i++)
{
scanf("%d", &a[0]);

for (int j = 1; j <= a[0]; j++)
scanf("%d", &a[j]);

int left = 1;
int right = 1;
int cnt = a[1];
int flag = 1;
int max = a[1];

for (int k = 2; k <= a[0]; k++)
{
if (cnt < 0)
{
flag = k;
cnt = a[k];
}
else
cnt += a[k];

if (cnt > max)
{
left = flag;
right = k;
max = cnt;
}
}

printf("Case %d:\n%d %d %d\n", i, max, left, right);
if (i != T)
printf("\n");
}

return 0;
}



标签:Case,cnt,sequence,--,Max,int,hdu1003,max,line
From: https://blog.51cto.com/u_11937443/5916536

相关文章

  • hdu1024 Max Sum Plus Plus--DP
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1024​​一:原题内容ProblemDescriptionNowIthinkyouhavegotanACinIgnatius.L's"MaxSum"pro......
  • hdu1300 Pearls--DP
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1300​​一:原题内容ProblemDescriptionInPearlaniaeverybodyisfondofpearls.Onecompany,calle......
  • hdu1429 胜利大逃亡(续)--BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1429​​一:分析定义一个三维数组,标记该点是否走过,其中第三维代表在该路径上所获钥匙的标记。二:AC代码#define_CRT......
  • hdu1495 非常可乐--BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1495​​一:分析思路:预处理m<n<s,以后处理方便点初始状态,m,n杯中可乐体积为0,s杯中体积为s;然后分六种情况:1,......
  • hdu1253 胜利大逃亡--BFS & BFS的总结
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1253​​一:题意一个三维A*B*C,起点(0,0,0),终点(A-1,B-1,C-1),求在t时间内(包括t)能否到达?可以,输出花费的最少时间,否则......
  • hdu1254 推箱子--BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1254​​一:分析分两步,一是箱子走到终点,二是人得走到箱子的前一个位置。先是bfs_box在t点找到一个可行点tt,进而......
  • hdu1240 Asteroids!--DFS & BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1240​​一:题意三维空间,中o表示可以走,x表示不能走,给出行走的起始点和目的点的坐标,问最少多少步可以从起点到达目......
  • hdu1180 诡异的楼梯--BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1180​​需要注意这句话:Harry每秒只能停留在'.'或'S'和'T'所标记的格子内.#define_CRT_SECURE_NO_DEPRECATE#def......
  • hdu1242 Rescue--BFS
    原题链接:​​http://acm.hdu.edu.cn/showproblem.php?pid=1242​​一:题意x代表卫兵,a代表终点,r代表起始点,.代表路,#代表墙路花费一秒,x花费两秒问到达终点的最少时间思路:B......
  • 使用Spring Reactor优化推荐流程
    1.背景公司有一个推荐系统Rec,这个系统的主要功能是:向外部系统提供推荐接口根据请求获取推荐策略根据推荐策略完成推荐的召回、过滤、打分、排序阶段Rec作为微服务......