ACM-DP最大连续子——hdu1231
***************************************转载请注明出处:http://blog.csdn.net/lttree***************************************
最大连续子序列
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17941 Accepted Submission(s): 7941
Nj },当中 1 <= i <= j <= K。
最大连续子序列是全部连续子序列中元素和最大的一个,
比如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和
为20。
在今年的数据结构考卷中,要求编敲代码得到最大和,如今添加一个要求,即还须要输出该
子序列的第一个和最后一个元素。
素。中间用空格分隔。
假设最大连续子序列不唯一,则输出序号i和j最小的那个(如输入例子的第2、3组)。
若全部K个元素都是负数,则定义其最大和为0,输出整个序列的首尾元素。
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
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.
- /****************************************
- *****************************************
- * Author:Tree *
- *From :http://blog.csdn.net/lttree *
- * Title : 最大连续子序列 *
- *Source: hdu 1231 *
- * Hint : dp *
- *****************************************
- ****************************************/
- #include <stdio.h>
- int a[10001],sum[10001],pre[10001];
- int main()
- {
- int n,i;
- int Max,Max_i;
- // isnegtive来推断是否全部数都小于0
- bool isnegtive;
- while( scanf("%d",&n)!=EOF && n)
- {
- isnegtive=false;
- for(i=0;i<n;++i)
- {
- scanf("%d",&a[i]);
- if( a[i]>=0 ) isnegtive=true;
- }
- // 假设全部数都小于0,后面不用算,直接输出
- if( !isnegtive )
- {
- printf("0 %d %d\n",a[0],a[n-1]);
- continue;
- }
- // 计算最大序列和
- sum[0]=pre[0]=a[0];
- for( i=1;i<n;++i )
- {
- if( sum[i-1]+a[i]>a[i] )
- {
- sum[i]=sum[i-1]+a[i];
- pre[i]=pre[i-1];
- }
- else
- sum[i]=pre[i]=a[i];
- }
- // 寻找最大子序列和。存下下标
- Max=-999999;
- for( i=0;i<n;++i )
- {
- if( sum[i]>Max )
- {
- Max=sum[i];
- Max_i=i;
- }
- }
- printf("%d %d %d\n",Max,pre[Max_i],a[Max_i]);
- }
- return 0;
- }
版权声明:本文博客原创文章,博客,未经同意,不得转载。
ACM-DP最大连续子——hdu1231的更多相关文章
- hdu1003 最大连续子序和
Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...
- 【剑指offer】最大和连续子阵列
个開始,到第3个为止).你会不会被他忽悠住? 输入: 输入有多组数据,每组測试数据包括两行. 第一行为一个整数n(0<=n<=100000),当n=0时,输入结束.接下去的一行包括n个整数 ...
- LeetCode 581. Shortest Unsorted Continuous Subarray (最短无序连续子数组)
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- [LeetCode] Shortest Unsorted Continuous Subarray 最短无序连续子数组
Given an integer array, you need to find one continuous subarray that if you only sort this subarray ...
- Java课程课后作业190315之从文档中读取随机数并得到最大连续子数组
从我上一篇随笔中,我们可以得到最大连续子数组. 按照要求,我们需要从TXT文档中读取随机数,那在此之前,我们需要在程序中写入随机数 import java.io.File; import java.i ...
- UVALive 3938 - "Ray, Pass me the dishes!" - [最大连续子列和+线段树]
题目链接:https://cn.vjudge.net/problem/UVALive-3938 参考刘汝佳书上说的: 题意: 给出一个长度为n的序列, 再给出m个询问, 每个询问是在序列 $[a,b] ...
- Leetcode 581.最短无序连续子数组
最短无序连续子数组 给定一个整数数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序,那么整个数组都会变为升序排序. 你找到的子数组应是最短的,请输出它的长度. 示例 1: 输入: [2, ...
- LeetCode 581. 最短无序连续子数组(Shortest Unsorted Continuous Subarray)
581. 最短无序连续子数组 581. Shortest Unsorted Continuous Subarray 题目描述 给定一个整型数组,你需要寻找一个连续的子数组,如果对这个子数组进行升序排序 ...
- 【LeetCode】1438. 绝对差不超过限制的最长连续子数组 Longest Continuous Subarray With Absolute Diff Less Than or Equal t
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址:https://leetco ...
随机推荐
- HDU 3315 My Brute(费用流)
职务地址:HDU 3315 这个题的思路全然是自己想出来的,自我感觉挺巧妙的. . .(大牛勿喷.. . )对大胆建图又多了一份信心. 详细思路是构造一个二分图,Si连源点.Xi连汇点,流量都是1,费 ...
- linux解压多个文件
方法很多种, 根据实际文件类型,位置情况进行变通: 1. for查询:for tar in *.tar.gz; do tar xvf $tar; done2. 列出文件列表,然后xargs 逐一解压: ...
- 在阿里云上布置git server
前言 东莞,晴,26至32度. 一直以为都是使用SVN Server作为私用的版本号控制器.随着Git的大行其道.近期由于项目须要,也试着在阿里云上部署Git Server.这里由于团队人员少.我採用 ...
- Andriod开发之浏览器开发(WebView)
1.创建一个WebView在XML文件 <WebView android:id="@+id/webView" android:layout_width="match ...
- memcached 分布式聚类算法
memcached 分布式集群,该决定必须书面开发商自己.和redis 由分布式server决定.上 memcached 有两个选项用于分布式.第一个是:模运算 另一种是:一致性hash 分布式算法. ...
- Intel baytrail-t support Linux?
点击这里查看文章 有空试试---唉... Ubuntu (Linux) on Atom Z3700 Series ASUS Transformer Book T100 is particularly ...
- hibernate 批量处理数据
批量处理数据是指处理大量数据的一个单独的事务. 在应用层批处理操作, 主要有以下方式: 通过 Session 通过 HQL 通过 StatelessSession 通过 JDBC API(仅仅要会用这 ...
- JDK基本介绍
JDK这是Java Development Kit 缩写,中国被称为Java开发套件.由SUN该公司提供.这是Java应用程序开发提供了编译和执行环境,所有的Java写程序都依赖于它. JDK能够将J ...
- git tag使用
#git tag command git tag [-a | -s | -u <key-id>] [-f] [-m <msg> | -F <file>] ...
- tomcat 重启进程
查看端口: ps -aux | grep tomcat 发现并没有8080端口的Tomcat进程. 使用命令:netstat –apn 查看所有的进程和端口使用情况.发现下面的进程列表,其中最后一栏是 ...