传送门

Description

Dynamic Programming, short for DP, is the favorite of iSea. It is a method for solving complex problems by breaking them down into simpler sub-problems. It is applicable to problems exhibiting the properties of overlapping sub-problems which are only slightly smaller and optimal substructure.
Ok, here is the problem. Given an array with N integers, find a continuous subsequence whose sum’s absolute value is the smallest. Very typical DP problem, right?

Input

The first line contains a single integer T, indicating the number of test cases.
Each test case includes an integer N. Then a line with N integers Ai follows.

Technical Specification
1. 1 <= T <= 100
2. 1 <= N <= 1 000
3. -100 000 <= Ai <= 100 000

Output

For each test case, output the case number first, then the smallest absolute value of sum.

Sample Input

2 2 1 -1 4 1 2 1 -2

Sample Output

Case 1: 0 Case 2: 1

思路

题意虽然是动态规划,但是不用动态规划也可以做,并且复杂度大大降低,存储序列的前缀和,那么我们可以知道绝对值最小的子段和就是前缀数组两两相减中绝对值最小者。因此,我们将序列的前缀数组排序,此时用桶排序,那么复杂度可以降低到O(N),快排的话复杂度降低到O(NlogN),然后用O(N)的复杂度扫一遍找出相邻差最小的值。注意,在前缀数组中,我们要手动加入一个前缀和为0的值,以此来保证结果的正确性。假设子段[1:3]的值为-1,子段[1:5]的值为1,如果不加入一个0,很可能得出的结果为2,当然了,加些预处理的话可以避免这个问题,但是还是手动加一个前缀和为0的值这样来得简单。也可以这么理解,子段和的绝对值最小,那就是离0最近的值。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1005;

int main()
{
    int T,Case = 0;
    scanf("%d",&T);
    while (T--)
    {
        int N,tmp,sum[maxn] = {0};
        scanf("%d",&N);
        for (int i = 1;i <= N;i++)
        {
            scanf("%d",&tmp);
            sum[i] = sum[i-1] + tmp;
        }
        sort(sum,sum+N+1);
        int res = sum[1]-sum[0];
        for (int i = 0;i < N;i++)
        {
            res = min(res,abs(sum[i+1]-sum[i]));
        }
        printf("Case %d: %d\n",++Case,res);
    }
    return 0;
}

  

HDU 4223 Dynamic Programming?(最小连续子序列和的绝对值O(NlogN))的更多相关文章

  1. hdu 4223 Dynamic Programming?

    Dynamic Programming? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  2. hdu 4223 Dynamic Programming? (dp)

    //连续的和的绝对值最小 # include <stdio.h> # include <string.h> # include <algorithm> # incl ...

  3. 连续子序列最大和的O(NlogN)算法

    对于一个数组,例如:int[] a = {4,-3,5,-2,-1,2,6,-2}找出一个连续子序列,对于任意的i和j,使得a[i]+a[i+1]+a[i+2]+.......+a[j]他的和是所有子 ...

  4. hdu分类 Dynamic Programming(这是一场漫长的旅途)

    下面是difficulty 1的题 1003   Max Sum 最长递增子序列.非常经典,最棒的解法是在线算法O(n)的复杂度. 贴的呢,是用dp做的代码. 先是一个高亮的dp递推式,然后找到最大处 ...

  5. hdu 5748(求解最长上升子序列的两种O(nlogn)姿势)

    Bellovin Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepte ...

  6. 【ToReadList】六种姿势拿下连续子序列最大和问题,附伪代码(以HDU 1003 1231为例)(转载)

    问题描述:       连续子序列最大和,其实就是求一个序列中连续的子序列中元素和最大的那个. 比如例如给定序列: { -2, 11, -4, 13, -5, -2 } 其最大连续子序列为{ 11, ...

  7. 动态规划(Dynamic Programming, DP)---- 最大连续子序列和

    动态规划(Dynamic Programming, DP)是一种用来解决一类最优化问题的算法思想,简单来使,动态规划是将一个复杂的问题分解成若干个子问题,或者说若干个阶段,下一个阶段通过上一个阶段的结 ...

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

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

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

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

随机推荐

  1. 2015-2016-2 《Java程序设计》教学进程

    2015-2016-2 <Java程序设计>教学进程 目录 考核方式 寒假准备 教学进程 第00周学习任务和要求 第01周学习任务和要求 第02周学习任务和要求 第03周学习任务和要求 第 ...

  2. SQL2005SP4补丁安装时错误: -2146233087 MSDTC 无法读取配置信息。。。错误代码1603的解决办法

    是在安装slq2005sp3和sp4补丁的时候碰到的问题. 起先是碰到的错误1603的问题,但网上搜索的1603的解决办法都试过了,google也用了,外文论坛也读了,依然没有能解决这个问题. 其实一 ...

  3. 拿到阿里,网易游戏,腾讯,smartx的offer的过程

    前言 从今年的3月14日阿里的电话面试开始,到现在4月16日在西安悦豪酒店进行的腾讯HR面到现在一个多月了,中间先后收到了阿里,网易游戏,腾讯和smartx的offer,今天早晨刚刚接到了腾讯HR的电 ...

  4. python动态网页爬取——四六级成绩批量爬取

    需求: 四六级成绩查询网站我所知道的有两个:学信网(http://www.chsi.com.cn/cet/)和99宿舍(http://cet.99sushe.com/),这两个网站采用的都是动态网页. ...

  5. EF增删改查操作

    增 using (StudentEntities ent = new StudentEntities()) { User aNewUser = new User() { Age = , Name = ...

  6. C语言输入输出整数

    scanf("%llu", &x); printf("%llu\n", x); scanf("%u", &x); print ...

  7. rabbitmq 相关方法

    //连接$conn_args = array( 'host'=>'127.0.0.1' , 'port'=> '5672', 'login'=>'guest' , 'password ...

  8. 微信公众平台消息接口开发之微信浏览器HTTP_USER_AGENT判断

    在微信公众平台的开发过程中,我们有时需要开发网页并判断是否是是来自微信浏览器访问,本文介绍如何做出这一判断. 一.$_SERVER数组 $_SERVER 是一个包含了诸如头信息(header).路径( ...

  9. Maven的内置变量

    Maven内置变量说明: ${basedir} 项目根目录(即pom.xml文件所在目录) ${project.build.directory} 构建目录,缺省为target目录 ${project. ...

  10. ubuntu搭建java开发环境

    最近因为要编译Android源码,但是报错因为Java版本低于1.7.x而不能进行编译,于是进行Java版本更改. 安装前软件环境: Ubuntu14.02,Java 1.6.0_29 目标软件环境: ...