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 ...
随机推荐
- 《TCP/IP详解卷1:协议》第11章 UDP:用户数据报协议-读书笔记
章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...
- Quartz.net的cron表达式
写在前面 前面有一篇文章用到了quartz.net,在设置定时时间的时候,使用了cron表达式,这里记录几种常见设置方式,方便对照使用. 详情 在这篇文章:Quartz.Net在windows服务中的 ...
- Javascript 使用小案例
十四.cookie相关 1 <!DOCTYPE html> <html> <head> <script> function setCookie(cnam ...
- OC----简单的购物系统----
今天下午OC上机考试,虽然考试的时候没写完, 但是课下写完了. main.m #import <Foundation/Foundation.h> #import "Shops.h ...
- poj1679 次小生成树
prim方法:先求过一遍prim,同时标记使用过得边.然后同时记录任意2点间的最大值. 每次加入一条新的边,会产生环,删去环中的最大值即可. #include<stdio.h> #incl ...
- 【ZOJ 1221】Risk
题 题意 给你20个城市的相邻关系,求给定任意两个城市的最短距离 分析 求任意两个城市最短距离,就是用floyd算法,我脑残忘记了k是写在最外层的. 代码 #include<stdio.h> ...
- RequestMethod 相关
Http协议的Delete和Put方法是做什么的?怎么用? RequestMethod 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 一般来说,Web服务器默认的只支持Pos ...
- jquery中datagrid中getSelected和getSelections的应用
http://blog.sina.com.cn/s/blog_8e50ede90101fff9.html 刚开始使用jquery的datagrid就知道如果要对特定的一行进行编辑,可以是 $('#on ...
- tp 多语言支持
tp支持多语言 通过get来改变语言的 http://localhost/tp/index.php/Admin/User/add/hl/zh-cn http://localhost/tp/index. ...
- git工作量统计
#!/bin/bash function count() { local insert=0 local delete=0 while read line ;do current=`echo $line ...