[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 ...
随机推荐
- empty()、html("")和text("")
empty().html("")和text("")在删除匹配元素内内容时是一样的.jQuery源码中实现有所不同,但效果相同.可以测试一下源码: <!DO ...
- 新发现了一个编辑器HBuilder,感觉蛮好的,关键是国产软件。
http://www.dcloud.io/
- ImagXpress中如何修改Alpha通道方法汇总
ImagXpress支持处理Alpha通道信息来管理图像的透明度,Alpha通道支持PNG ,TARGA和TIFF文件,同时还支持BMP和ICO文件.如果说保存的图像样式不支持Alpha通道,就将会丢 ...
- 数据库订正脚本性能优化两则:去除不必要的查询和批量插入SQL
最近在做多数据库合并的脚本, 要将多个分数据库的表数据合并到一个主数据库中. 以下是我在编写数据订正脚本时犯过的错误, 记录以为鉴. 不必要的查询 请看以下语句: regiondb = db.Houy ...
- Android NDK 开发(一)--环境搭建【转】
转载请注明出处:http://blog.csdn.net/allen315410/article/details/41800955 Android下的NDK开发是Android开发中不可或缺的一部分, ...
- 为该目录以及子目录添加index.html
add index.html to a directory recursively using Perl5 使用的目录,是从Perl下载的perl5.18.2的文档 Look Here #!/usr/ ...
- Python 的命令行参数处理 optparse->argparse
optaprse自2.7版开始弃用:弃用optparse模块,不会进一步开发,将继续开发argparse模块作为替代. 但是用习惯了optparse,还是很好用的撒. optparse使用起来,相比旧 ...
- html5 canvas 笔记四(变形 Transformations)
绘制复杂图形必不可少的方法 save() 保存 canvas 状态 restore() 恢复 canvas 状态 Canvas 的状态就是当前画面应用的所有样式和变形的一个快照. Canvas 的状态 ...
- React Native微信分享 朋友圈分享 Android/iOS 通用
超详细React Native实现微信好友/朋友圈分享功能-Android/iOS双平台通用 2016/06/16 | React Native技术文章 | Sky丶清| 暂无评论 | 1 ...
- destoon短信接口修改方法
destoon是很优秀的B2B行业站程序.程序模块化开发契合度很高,二次开发起来也很顺畅.数据缓存,权限分配,SEO功能方面都不错. 但是在使用这套程序的时候,常常要用到发送短信的功能,而destoo ...