混了好几个地方的博客,还是觉得博客园比较靠谱,于是决定在这里安家落户了。本人本科生一个,希望各位巨巨多多指教~

  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]最大连续子序列的更多相关文章

  1. lintcode 最长上升连续子序列 II(二维最长上升连续序列)

    题目链接:http://www.lintcode.com/zh-cn/problem/longest-increasing-continuous-subsequence-ii/ 最长上升连续子序列 I ...

  2. 最大连续子序列乘积(DP)

    题目来源:小米手机2013年校园招聘笔试题 题目描述: 给定一个浮点数序列(可能有正数.0和负数),求出一个最大的连续子序列乘积. 输入: 输入可能包含多个测试样例.每个测试样例的第一行仅包含正整数 ...

  3. DP专题训练之HDU 1231 最大连续子序列

    Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j < ...

  4. HDU 1231 最大连续子序列(水题)

    题目链接: 传送门 最大连续子序列 Time Limit: 1000MS     Memory Limit: 32768 K Description 给定K个整数的序列{ N1, N2, ..., N ...

  5. HDU-1231 简单dp,连续子序列最大和,水

    1.HDU-1231 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=1231 3.总结:水 题意:连续子序列最大和 #include<iostre ...

  6. HDU 1231:最大连续子序列 解题报告

    第一次写博客, 自己总结写出了一道题感觉值得保存. 自己总结的规律:求最大连续子序列, 可以先求包括第N项在内的前N项最大值, (因为每一项都求过后, 前N项最大值最大的那一个元素所连续的序列即为最大 ...

  7. [ACM_其他] 总和不小于S的连续子序列的长度的最小值——尺缩法

    Description: 给定长度为n的整数数列,A[0],A[1],A[2]….A[n-1]以及整数S,求出总和不小于S的连续子序列的长度的最小值.如果解不存在,则输出0. Input: 输入数据有 ...

  8. UVa 108 - Maximum Sum(最大连续子序列)

    题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&pa ...

  9. HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)

    C - 最大连续子序列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

随机推荐

  1. 数据库性能优化:SQL索引

    SQL索引在数据库优化中占有一个非常大的比例, 一个好的索引的设计,可以让你的效率提高几十甚至几百倍,在这里将带你一步步揭开他的神秘面纱. 1.1 什么是索引? SQL索引有两种,聚集索引和非聚集索引 ...

  2. AndroidUI自动化测试工具-UIautomator

    转自:http://www.cnblogs.com/rexmzk/archive/2012/12/26/2834380.html 最近公司在开展Android的自动化测试,美国那边的开发人员利用And ...

  3. 【转】ubuntu64,ndk-r9 编译 ffmpeg 2.1.1的config文件

    #!/bin/bash NDK_ROOT=/home/wjh/fox/android-ndk-r9c/ PREBUILT=${NDK_ROOT}toolchains/arm-linux-android ...

  4. android 学习随笔二十四(动画:帧动画)

    帧动画,一张张图片不断的切换,形成动画效果 * 在drawable目录下定义xml文件,子节点为animation-list,在这里定义要显示的图片和每张图片的显示时长 * FrameAnimatio ...

  5. 【python cookbook】【字符串与文本】4.文本模式的匹配和查找

    问题:按照特定的文本模式进行匹配或查找 解决方法: 1.简单的文字匹配,只需使用str.find().str.startswith().str.endswith()或类似的函数即可: 2.复杂的匹配, ...

  6. CMD设IP

    netsh interface ip set address name="本地连接"  source=static/dhcp(静态/动态) addr=192.168.3.5 mas ...

  7. IO流认识

    处理流是“连接”在已存在的流(节点流或处理流)之上,通过对数据的处理为程序提供更强大的读写能力.  BufferedWriter/BufferedReader(缓冲流)是处理流中的一种 OutputS ...

  8. Redis 安装 启动 连接 配置 重启

    Linux下安装 ]# wget http://download.redis.io/releases/redis-2.8.17.tar.gz ]# .tar.gz ]# cd redis- ]# ma ...

  9. java 23种设计模式及具体例子 收藏有时间慢慢看

    设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了可重用代码.让代码更容易被他人理解.保证代 码可靠性. 毫无疑问,设计模式 ...

  10. AtomicInteger类保证线程安全的用法

    J2SE 5.0提供了一组atomic class来帮助我们简化同步处理.基本工作原理是使用了同步synchronized的方法实现了对一个long, integer, 对象的增.减.赋值(更新)操作 ...