[HDOJ1231]最大连续子序列
混了好几个地方的博客,还是觉得博客园比较靠谱,于是决定在这里安家落户了。本人本科生一个,希望各位巨巨多多指教~
Hello World!
单独一个象征性的问候实在是太low了,还是决定来点实质性的。。。比如水个题什么的。
希望能交到更多朋友~
---------------------------------------------------------------------------------------------------------------------------------------------------------------
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1231
给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ...,
Nj },其中 1 <= i <= j <= K。最大连续子序列是所有连续子序列中元素和最大的一个,
例如给定序列{ -2, 11, -4, 13, -5, -2 },其最大连续子序列为{ 11, -4, 13 },最大和
为20。
在今年的数据结构考卷中,要求编写程序得到最大和,现在增加一个要求,即还需要输出该
子序列的第一个和最后一个元素。
水题就要水着做,此题花式AC,现提供蛋疼方法如下:
#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
int i, k, n, arr[];
int l, r, maxSum, thisSum, rflag, lflag, zflag, pflag, pos;
while(scanf("%d", &n) != EOF && n)
{
k = ;
pos = ;
maxSum = ;
thisSum = ;
rflag = ;
pflag = ;
zflag = ;
for(i = ; i < n; i++)
{
scanf("%d", &arr[i]);
if(arr[i] == )
{
zflag = ;
}
if(arr[i] > )
{
pos = pos + ;
pflag = ;
}
}
r = arr[];
l = arr[];
for(i = ; i < n; i++)
{
thisSum += arr[i];
if(rflag)
{
r = arr[i-];
}
rflag = ;
if(thisSum > maxSum)
{
maxSum = thisSum;
rflag = ;
lflag = ;
}
else if(thisSum < )
{
k = i + ;
thisSum = ;
lflag = ;
}
if(lflag)
{
l = arr[k];
}
}
if(zflag && !pos)
{
printf("%d %d %d\n", , , );
continue;
}
if(!pflag)
{
printf("%d %d %d\n", , arr[], arr[n-]);
continue;
}
else
{
printf("%d %d %d\n", maxSum, l, r);
}
}
return ;
}
正确ac姿势:
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; const int maxn = ;
int a[maxn];
int n; int main() {
// freopen("in", "r", stdin);
while(~scanf("%d", &n) && n) {
for(int i = ; i < n; i++) {
scanf("%d", &a[i]);
}
int mx = -;
int cr = ;
int tp = ;
int bg;
int ed;
for(int i = ; i < n; i++) {
cr += a[i];
if(cr > mx) {
mx = cr;
bg = tp;
ed = i;
}
if(cr < ) {
cr = ;
tp = i + ;
}
}
if(mx < ) {
printf("%d %d %d\n", , a[], a[n-]);
}
else {
printf("%d %d %d\n", mx, a[bg], a[ed]);
}
}
}
[HDOJ1231]最大连续子序列的更多相关文章
- lintcode 最长上升连续子序列 II(二维最长上升连续序列)
题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/ 最长上升连续子序列 I ...
- 最大连续子序列乘积(DP)
题目来源:小米手机2013年校园招聘笔试题 题目描述: 给定一个浮点数序列(可能有正数.0和负数),求出一个最大的连续子序列乘积. 输入: 输入可能包含多个测试样例.每个测试样例的第一行仅包含正整数 ...
- 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,连续子序列最大和,水
1.HDU-1231 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=1231 3.总结:水 题意:连续子序列最大和 #include<iostre ...
- HDU 1231:最大连续子序列 解题报告
第一次写博客, 自己总结写出了一道题感觉值得保存. 自己总结的规律:求最大连续子序列, 可以先求包括第N项在内的前N项最大值, (因为每一项都求过后, 前N项最大值最大的那一个元素所连续的序列即为最大 ...
- [ACM_其他] 总和不小于S的连续子序列的长度的最小值——尺缩法
Description: 给定长度为n的整数数列,A[0],A[1],A[2]….A[n-1]以及整数S,求出总和不小于S的连续子序列的长度的最小值.如果解不存在,则输出0. Input: 输入数据有 ...
- UVa 108 - Maximum Sum(最大连续子序列)
题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...
- HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)
C - 最大连续子序列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
随机推荐
- Eclipse安装插件支持jQuery智能提示
Eclipse安装插件支持jQuery智能提示 最近工作中用到jQuery插件,需要安装eclipse插件才能支持jQuery智能提示,在网上搜索了一下,常用的有三个插件支持jQuery的智能提示:1 ...
- angularjs 相关资料
1.angularjs 框架结构 bootstrap process :http://www.oschina.net/translate/angularjs-the-next-big-thing?pr ...
- android 学习随笔四(数据库存储)
SQLite数据库(sqliteexpert工具),sqlite数据库是轻量级数据库,对数据类型要求不是很严格,在数据库中处理是按verchar类型处理,一般定义表字段时还是要求严格按照数据类型定义, ...
- linux设备驱动归纳总结(七):1.时间管理与内核延时【转】
本文转载自:http://blog.chinaunix.net/uid-25014876-id-100005.html linux设备驱动归纳总结(七):1.时间管理与内核延时 xxxxxxxxxxx ...
- 【python cookbook】【字符串与文本】8.编写多行模式的正则表达式
问题:用正则表达式对一段文本块做匹配,但是希望在进行匹配时能够跨越多行 解决方案: 1.正则表达式添加对换行符的支持: 2.re.compile()函数一个有用的标记-re.DOTALL使得正则表达式 ...
- PHP判断客户端是PC web端还是移动手机端方法
PHP判断客户端是PC web端还是移动手机端方法需要实现:判断手机版的内容加上!c550x260.jpg后缀变成缩略图PHP用正则批量替换Img中src内容,用正则表达式获取图片路径实现缩略图功能 ...
- PHP memcached 扩展的安装
PHP memcached 扩展的安装 1.下载相关的源码包(软件版本自己选择) memcached-1.4.15 , http://memcached.org/ //Memcached服务端. me ...
- 25、oracle(一)
1)了解oracle背景,概念和特点 2)掌握oracleSQL对单表各种查询操作 3)掌握oracleSQL中函数的使用 4)掌握数值型number,字符串型varchar2,日期型date,条件判 ...
- HTML5与CSS3权威指南
第一章:Web时代的变迁 HTML5放心使用的理由: 兼容性:HTML5在老板本的浏览器上也可以正常运行: 实用性:HTML5内部并没有封装什么很复杂的,不切实际的功能,而只是封装了简单实用的功能: ...
- 文件名唯一(A.txt => An.txt)
/// <summary> /// 文件名唯一(A.txt => An.txt) /// </summary> /// <param name="full ...