HDU 4223 Dynamic Programming?(最小连续子序列和的绝对值O(NlogN))
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))的更多相关文章
- hdu 4223 Dynamic Programming?
Dynamic Programming? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- hdu 4223 Dynamic Programming? (dp)
//连续的和的绝对值最小 # include <stdio.h> # include <string.h> # include <algorithm> # incl ...
- 连续子序列最大和的O(NlogN)算法
对于一个数组,例如:int[] a = {4,-3,5,-2,-1,2,6,-2}找出一个连续子序列,对于任意的i和j,使得a[i]+a[i+1]+a[i+2]+.......+a[j]他的和是所有子 ...
- hdu分类 Dynamic Programming(这是一场漫长的旅途)
下面是difficulty 1的题 1003 Max Sum 最长递增子序列.非常经典,最棒的解法是在线算法O(n)的复杂度. 贴的呢,是用dp做的代码. 先是一个高亮的dp递推式,然后找到最大处 ...
- hdu 5748(求解最长上升子序列的两种O(nlogn)姿势)
Bellovin Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepte ...
- 【ToReadList】六种姿势拿下连续子序列最大和问题,附伪代码(以HDU 1003 1231为例)(转载)
问题描述: 连续子序列最大和,其实就是求一个序列中连续的子序列中元素和最大的那个. 比如例如给定序列: { -2, 11, -4, 13, -5, -2 } 其最大连续子序列为{ 11, ...
- 动态规划(Dynamic Programming, DP)---- 最大连续子序列和
动态规划(Dynamic Programming, DP)是一种用来解决一类最优化问题的算法思想,简单来使,动态规划是将一个复杂的问题分解成若干个子问题,或者说若干个阶段,下一个阶段通过上一个阶段的结 ...
- DP专题训练之HDU 1231 最大连续子序列
Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj },其中 1 <= i <= j < ...
- HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)
C - 最大连续子序列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
随机推荐
- 关于mvc5+EF里面的db.Entry(model).State = EntityState.Modified报错问题
最近在使用mvc5+EF的的时候用到了这句话 db.Entry(model).State = EntityState.Modified 看上去很简单的修改数据,但是一直报错,说是key已经存在,不能修 ...
- 抛开react,如何理解virtual dom和immutability
去年以来,React的出现为前端框架设计和编程模式吹来了一阵春风.很多概念,无论是原本已有的.还是由React首先提出的,都因为React的流行而倍受关注,成为大家研究和学习的热点.本篇分享主要就聚焦 ...
- 你是否还在质疑EF的性能
1. 写在前面的话 一直没有写博客的习惯,感觉太浪费时间,没有那么多精力,其实仔细一想,写博客是一种习惯,也是一种心境,同时也是对自己所掌握的知识结构的一个梳理过程,对自己知识体系的一个巩固,同时也是 ...
- 又发现个.net framework的坑
请找出这两个方法的区别: http://msdn.microsoft.com/en-us/library/ms584187(v=vs.110).aspx
- js的offsetWidth,offsetHeight,offsetLeft,offsetTop
js的offsetWidth,offsetHeight,offsetLeft,offsetTop
- Quartz.NET 入门
概述 Quartz.NET是一个开源的作业调度框架,非常适合在平时的工作中,定时轮询数据库同步,定时邮件通知,定时处理数据等. Quartz.NET允许开发人员根据时间间隔(或天)来调度作业.它实现了 ...
- C# 多线程防止卡死
软件界面的响应特性是判断一款软件的非常重要的方面.一般来说,不管你软件功能做得有多么奇妙,如果软件有一点点死机的感觉都会让用户感到很讨厌,甚至怀疑你软件里是否藏有更大的问题. 要提高界面的响应特性,最 ...
- c#模拟表单POST数据,并获取跳转之后的页面
直接看代码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Sy ...
- 单链表C/C++实现
#include <iostream> using namespace std; const int N = 10; typedef int ELEMTYPE; typedef struc ...
- jsp内置对象作业3-application用户注册
1,注册页面 zhuCe.jsp <%@ page language="java" contentType="text/html; charset=UTF-8&qu ...