hdu 1231
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 Huge input, scanf is recommended.
Hint
想想还是挺好的的题,dp吧,就是最开始的比较复杂,后面看了别人的代码 选择用数组就存,自己还是太弱了,
还需要好好努力呀,知道如何找第一个位置 就很简单了,以下是我的代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
typedef long long ll;
#define N 10005
using namespace std;
int a[N];
int dp[N];
int main()
{
int t;
int i,j;
int flag;
int maxsum=0;
int b[N];
while(scanf("%d",&t)!=EOF)
{
if(t==0)
break;
flag=0;
maxsum=0;
memset(a,0,sizeof(a));
for(i=1;i<=t;i++)
{
scanf("%d",&a[i]);
if(a[i]>=0)
flag=1;
}
memset(dp,0,sizeof(dp));
dp[0]=0;
int first=1;
for(i=1;i<=t;i++)
{
if(dp[i-1]+a[i]>=a[i])
{
dp[i]=dp[i-1]+a[i];
}
else
{
dp[i]=a[i];
first=i;
}
maxsum=max(maxsum,dp[i]);
b[i]=first;
}
int next;
if(flag==0)
printf("0 %d %d\n",a[1],a[t]);
else
{
for(i=1;i<=t;i++)
{
if(maxsum==dp[i])
{
next=i;
break;
}
}
//cout<<next<<" "<<a[0]<<endl;
printf("%d %d %d\n",maxsum,a[b[next]],a[next]);
}
} }
Hint
hdu 1231的更多相关文章
- HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)
C - 最大连续子序列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- 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 hdu 1231 最大连续子序列【dp】
HDU1003 HDU1231 题意自明.可能是真的进步了点,记得刚开始研究这个问题时还想了好长时间,hdu 1231还手推了很长时间,今天重新写干净利落就AC了. #include<iostr ...
- HDU 1231 最大连续子序列:水dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1231 题意: 给你一个整数序列,求连续子序列元素之和最大,并输出该序列的首尾元素(若不唯一,输出首坐标 ...
- 最大连续子序列 -- hdu -- 1231
http://acm.hdu.edu.cn/showproblem.php?pid=1231 最大连续子序列 Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU 1231 最大子序列
http://acm.hdu.edu.cn/showproblem.php?pid=1231 Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连 ...
- DP专题训练之HDU 1231 最大连续子序列
Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j < ...
- HDU 1231:最大连续子序列(DP)
pid=1231">最大连续子序列 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- hdu 1231 最大连续子序列 ,1003 Max Sum;
题目(1231) #include<stdio.h> #include<iostream> using namespace std; int main() { int K,nu ...
随机推荐
- eclipse几个注意的地方
1.eclipse+tomcat调试java web,eclipse中更新了代码,而tomcat中代码不同步更新 1)双击eclipse tomcat server,勾选"Modules a ...
- 数据库 MySql(二)
MySQL(二) 1.外键及连表 外键 一个特殊的索引,只能是指定内容 CREATE TABLE part1 ( nid INT NOT NULL auto_increment PRIMARY KEY ...
- python_day3
一.set的基本操作 >>> s1=set("abc123abc") >>> s1 #把字符串转化为set,去重 set([']) >&g ...
- nginx/Windows-1.9.3启动脚本
启动nginx.bat @echo off D: cd D:\Program Files\nginx-1.9.3 tasklist | findstr /i "nginx.exe" ...
- inline-block和block的区别
<html> <head> <style> body,ul,li,span{padding:0px;margin:0px;} </style> < ...
- 类似新浪 腾讯微博字数统计 控制js(区分中英文 符号)
<script> ; function Q(s) { return document.getElementById(s); } function checkWord(c) { len = ...
- 登录失败。该登录名来自不受信任的域,不能与 Windows 身份验证一起使用
登录失败.该登录名来自不受信任的域,不能与 Windows 身份验证一起使用 使用sever sql 远程连接数据库的时候遇到了这个问题,我用的是ADO.NET 实体数据模型,有web.config ...
- 使用get传参的时候,参数在后头获取不到或者出现别的错误。
把传递的参数使用encode转换一下,符合HTTP规定的编码,再使用. String encode = java.net.URLEncoder.encode("VSrYJoDat8z7Ad9 ...
- CAP理论(转)
add by zhj: CAP理论可以简单的理解为一致性,可用性,可分区性,这三者没有办法同时满足.我们使用的关系型数据库,比如MySQL,Postgresql是CA类型, 而Redis,MongoD ...
- 第九十七天请假 PHP TP框架 MVC模式
MVC : M->Model 模型(数据层) V->View 视图(视图层) C->Controller 控制器(逻辑层) M : 操作数据(连接数据 ...