HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个,
例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和
为20。
在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该
子序列的第一个和最后一个元素。
Input
Output
素,中间用空格分隔。如果最大连续子序列不唯一,则输出序号i和j最小的那个(如输入样例的第2、3组)。若所有K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。
Sample Input
6
-2 11 -4 13 -5 -2
10
-10 1 2 3 4 -5 -23 3 7 -21
6
5 -8 3 2 5 0
1
10
3
-1 -5 -2
3
-1 0 -2
0
Sample Output
20 11 13
10 1 4
10 3 5
10 10 10
0 -1 -2
0 0 0
Hint
Hint Huge input, scanf is recommended.
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int a[maxn];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
if(!n)
break;
bool flag=true;
// memset(a,0,sizeof(a));
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
if(a[i]>=)
flag=false;
}
if(flag){
printf("0 %d %d\n",a[],a[n]);
continue;
}
int ans,sum,head,tail,ans_head,ans_tail;
ans=sum=a[];
ans_head=ans_tail=head=tail=;
for(int i=;i<=n;i++){
if(sum>){
sum+=a[i];
tail=i;
}
if(sum<=){
sum=a[i];
head=tail=i;
}
if(sum>ans){
ans=sum;
ans_head=head;
ans_tail =tail;
}
} printf("%d %d %d\n",ans,a[ans_head],a[ans_tail]); }
return ;
}
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Input
Output
Sample Input
2
5 6 -1 5 4 -7
7 0 6 -1 1 -6 7 -5
Sample Output
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int a[maxn];
int main(){
int n;
int t;
int cnt=;
scanf("%d",&t);
while(t--){
cnt++;
scanf("%d",&n);
// bool flag=true;
// memset(a,0,sizeof(a));
for(int i=;i<=n;i++){
scanf("%d",&a[i]); }
int ans,sum,head,tail,ans_head,ans_tail;
ans=sum=a[];
ans_head=ans_tail=head=tail=;
for(int i=;i<=n;i++){
if(sum>=){
sum+=a[i];
tail=i;
}
if(sum<){
sum=a[i];
head=tail=i;
}
if(sum>ans){ ans=sum;
ans_head=head;
ans_tail =tail;
}
}
printf("Case %d:\n",cnt);
printf("%d %d %d\n",ans,ans_head,ans_tail); if(t!=) printf("\n"); }
return ;
}
HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)的更多相关文章
- HDU 1231 最大连续子序列 --- 入门DP
HDU 1231 题目大意以及解题思路见: HDU 1003题解,此题和HDU 1003只是记录的信息不同,处理完全相同. /* HDU 1231 最大连续子序列 --- 入门DP */ #inclu ...
- HDU 1231.最大连续子序列-dp+位置标记
最大连续子序列 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- HDU 1003 Max Sum && HDU 1231 最大连续子序列 (DP)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- HDU 1231 最大连续子序列:水dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1231 题意: 给你一个整数序列,求连续子序列元素之和最大,并输出该序列的首尾元素(若不唯一,输出首坐标 ...
- DP专题训练之HDU 1231 最大连续子序列
Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j < ...
- HDU 1231 最大连续子序列(水题)
题目链接: 传送门 最大连续子序列 Time Limit: 1000MS Memory Limit: 32768 K Description 给定K个整数的序列{ N1, N2, ..., N ...
- HDU 1231 最大连续子序列 (dp)
题目链接 Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= ...
- HDU 1231 最大连续子序列 (动态规划)
最大连续子序列 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- hdu 1003 hdu 1231 最大连续子序列【dp】
HDU1003 HDU1231 题意自明.可能是真的进步了点,记得刚开始研究这个问题时还想了好长时间,hdu 1231还手推了很长时间,今天重新写干净利落就AC了. #include<iostr ...
随机推荐
- 服务器网站报错:由于扩展配置问题无法提供您请求的页面,请添加MIME映射,针对mp4,flv文件类型无法打开。
确保IIS正常 服务器增加mp4格式的MIME 类型映射设置的具体步骤是: “开始” > “控制面板” > “管理工具” >“Internet 信息服务(IIS管理器)”,“MIME ...
- 多个TableView的练习
效果图: 左边图片的代码: // // SecViewController.m // UI__多个TableView练习 // // Created by dllo on 16/3/17. // Co ...
- OneZero第一次会议(非正式)
会议时间:2016年3月20日 15:50~16:50 会议成员:冉华(http://www.cnblogs.com/ranh941/) 张敏(http://www.cnblogs.com/zhang ...
- hdu4081 次小生成树
题意:有n个点,n-1条边.现在徐福可以让一条边无消耗建立,即魔法边.B表示除魔法边之外的的其他边的消耗值和,A表示这条魔法边相连的2个集合中都选一点,这两点的最大值,现在要求A/B最大. 方法:因为 ...
- 金融自助设备WOSA/XFS规范介绍
wosa(全称是windows开放式系统体系结构 windows open system architecture),是微软公司提出的一种在windows操作系统下的软件架构. wosa/xfs是基于 ...
- 【ZOJ 3502】Contest
题 题意 n个问题,解决的顺序影响正确的概率,无论之前解决的问题是否答对,当前问题 j 答对概率为max{a[i][j]} (i为解决过的问题).求答对题目的最大期望和对应的答题顺序.T组测试,T ( ...
- ElasticSearch插件安装Head、Kopf与Bigdesk
ElasticSearch-Head ElasticSearch-Head 是一个与Elastic集群(Cluster)相交互的Web前台. ES-Head的主要作用 它展现ES集群的拓扑结构,并且可 ...
- POJ2135 Farm Tour
Farm Tour Time Limit: 2MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u Description ...
- java 文件读取大全
1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile { /** * 以字节为单位读取文件,常用 ...
- ASP.NET MVC4中调用WEB API的四个方法
http://tech.it168.com/a2012/0606/1357/000001357231_all.shtml [IT168技术]当今的软件开发中,设计软件的服务并将其通过网络对外发布,让各 ...