[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 ...
随机推荐
- Android 多线程通信 访问网络
package org.rongguang.testthread; import android.app.Activity; import android.os.Bundle; import andr ...
- centos 6 7更改主机名 命令添加ip
6:vi /etc/sysconfig/network 中hostname =主机名 vi /etc/hosts 添加127.0.0.1相应的主机名 7: hostnamectl set-hostn ...
- Compress a Folder/Directory via Perl5
Compress a Folder/Directory via Perl5 tested in Windows, Mac OS X, Ubuntu16.04 #!/usr/bin/perl #压缩指定 ...
- kvm虚拟机virt-manager启动报错
安装kvm,用virt-manager启动时报错如下: Traceback (most recent call last): File "/usr/share/virt-manager/v ...
- 14 个 grep 命令的例子 【转】
转自:https://linux.cn/article-5453-1.html 编译自:http://www.linuxtechi.com/linux-grep-command-with-14-dif ...
- Perl 和 Python 的比较 【转】
转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&id=4662991&uid=608135 作为万年Perl 党表示最近开 ...
- Hibernate解决n+1问题
观点:对于n+1问题的理解. 一般而言说n+1意思是,无论在一对多还是多对一当查询出n条数据之后,每条数据会关联的查询1次他的关联对象,这就叫做n+1. 但是我的理解是,本来所有信息可以一次性查询出来 ...
- [ERROR][org.springframework.web.context.ContextLoader][main] Context initialization failed org.sprin
做一个SSH为基础框架的webapp小DEMO,复制了一把以前可以跑的代码,竟发现无法初始化数据源,报错如下: [ERROR][org.springframework.web.context.Cont ...
- PHP常用的数组相关处理函数
[数组中常用的多种遍历方式] [for 和 foreach 略] [while() . list() .each() 组合循环遍历数组] each()函数 a. 需要一个数组作为参数 b. 返回来的也 ...
- PHP开发异步高性能的MySQL代理服务器
ySQL数据库对每个客户端连接都会分配一个线程,所以连接非常宝贵.开发一个异步的MySQL代理服务器,PHP应用服务器可以长连接到这台Server,既减轻MYSQL的连接压力,又使PHP保持长连接减少 ...