[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 ...
随机推荐
- 获取CPUID等
unit CommonUnit; interface uses Windows, SysUtils, DateUtils; Const CPUVendorIDs: .. ] of string = ( ...
- ubuntu修改文件权限记录
查看文件权限的命令: 在终端输入: ls -l xxx.xxx (xxx.xxx是文件名) 那么就会出现相类似的信息,主要都是这些: -rw-rw-r-- 一共有10位数 其中: 最前面那个 - 代表 ...
- iOS 学习笔记 十三 (2015.04.15)采用第三方库,实现ios录音转为amr
1.第三方开源库地址 https://github.com/guange2015/ios-amr 2.参考博客地址 http://blog.csdn.net/windsoul85/article/de ...
- flex 添加右键链接
private var myMenu:ContextMenu; private function setViewerVersion():void { var menuItem:ContextMenuI ...
- GDCPC2016 省赛随笔
这是第一次参加省赛,第二次参加正式的组队赛. 昨晚很早就睡,早上挺早就睡不着醒了,内心既紧张又激动(虽然赛前和队友说尽力就好,但是还是很怕打铁啊).然后吃过早餐跟随学校大部队来到了中大.发现有好多学校 ...
- SQLServer学习笔记<>日期和时间数据的处理(cast转化格式、日期截取、日期的加减)和 case表达式
日期和时间数据的处理. (1)字符串日期 ‘20080301’,这一串为字符串日期,但必须保证为四位的年份,两位的月份,两位的日期.例如,查询订单表日期大于‘20080301’.可以这样写: 1 se ...
- [HTML]HTML5实现可编辑表格
HTML5实现的简单的可编辑表格 [HTML]代码 <!DOCTYPE html > <html > <head> <meta charset="u ...
- ACM题目————二叉树的遍历
一.二叉树的后序遍历: 题目描述 给定一颗二叉树,要求输出二叉树的深度以及后序遍历二叉树得到的序列.本题假设二叉树的结点数不超过1000 输入 输 入数据分为多组,第一行是测试数据的组数n,下面的n行 ...
- ACM题目————Aggressive cows
Description Farmer John has built a new long barn, with N (2 <= N <= 100,000) stalls. The stal ...
- c#之线程池优先级
using System; using System.Threading; namespace ConsoleApplication1 { class Program { static void Ma ...