Dynamic Programming?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Problem 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
 
Author
iSea@WHU
 
Source
思路:暴力,我以为会T,还想用treap优化一下。。。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pi (4*atan(1.0))
#define eps 1e-14
const int N=2e5+,M=4e6+,inf=1e9+,mod=1e9+;
const ll INF=1e18+;
int a[N];
int pre[N];
int main()
{
int T,cas=;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
for(int i=;i<=n;i++)
pre[i]=pre[i-]+a[i];
int ans=inf;
for(int i=;i<=n;i++)
for(int t=i;t<=n;t++)
{
ans=min(ans,abs(pre[t]-pre[i-]));
}
printf("Case %d: %d\n",cas++,ans);
}
return ;
}
 

hdu 4223 Dynamic Programming?的更多相关文章

  1. HDU 4223 Dynamic Programming?(最小连续子序列和的绝对值O(NlogN))

    传送门 Description Dynamic Programming, short for DP, is the favorite of iSea. It is a method for solvi ...

  2. hdu 4223 Dynamic Programming? (dp)

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

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

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

  4. hdu 4972 A simple dynamic programming problem(高效)

    pid=4972" target="_blank" style="">题目链接:hdu 4972 A simple dynamic progra ...

  5. HDU-4972 A simple dynamic programming problem

    http://acm.hdu.edu.cn/showproblem.php?pid=4972 ++和+1还是有区别的,不可大意. A simple dynamic programming proble ...

  6. 动态规划 Dynamic Programming

    March 26, 2013 作者:Hawstein 出处:http://hawstein.com/posts/dp-novice-to-advanced.html 声明:本文采用以下协议进行授权: ...

  7. Dynamic Programming

    We began our study of algorithmic techniques with greedy algorithms, which in some sense form the mo ...

  8. hdu 4223

    暴力: Problem : ( Dynamic Programming? ) Judge Status : Accepted RunId : Language : C++ Author : yudun ...

  9. 算法导论学习-Dynamic Programming

    转载自:http://blog.csdn.net/speedme/article/details/24231197 1. 什么是动态规划 ------------------------------- ...

随机推荐

  1. FormData对象

    FF4中增加了一个很有意思的对象,FormData.通常我们提交(使用submit button)时,会把form中的所有表格元素的name与value组成一个queryString,提交到后台.这用 ...

  2. C# 控制连接超时

    首先连接超时分为三种,TCP Connection to SQL Server -> SqlConnection.Open -> SqlCommand.Execute先说第二种超时,sql ...

  3. Maven3.x 插件开发入门

    Maven工具有很多插件,各种各样的插件,让我们开发调试过程中非常方便,但是终究是有你想要的但是现目前插件不能满足的(可能性非常非常低),这个时候就需要使用其他的替代工具,或者是自己来开发一个Mave ...

  4. 浏览器的不兼容,归纳几点html编码要素

    1.文字本身的大小不兼容.同样是font-size:14px的宋体文字,在不同浏览器下占的空间是不一样的,ie下实际占高16px,下留白3px,ff下实际占高17px,上留白1px,下留白3px,op ...

  5. MySQL连接字符串总结

    一.MySQL Connector/ODBC 2.50 (MyODBC 2.50)连接方式 1.本地数据库连接 Driver={MySQL};Server=localhost;Option=16834 ...

  6. js原型 prototype

    js中只有构造函数(所有函数)拥有prototype属性对象

  7. python:Xml

    <data> <country name="Liechtenstein"> <rank updated="yes">2< ...

  8. STL MAP及字典树在关键字统计中的性能分析

    转载请注明出处:http://blog.csdn.net/mxway/article/details/21321541 在搜索引擎在通常会对关键字出现的次数进行统计,这篇文章分析下使用C++ STL中 ...

  9. YTU 3006: 迷宫问题(栈与队列)

    3006: 迷宫问题(栈与队列) 时间限制: 1 Sec  内存限制: 128 MB 提交: 3  解决: 1 题目描述 编写一个求解迷宫问题的程序,要求输出迷宫的所有路径,并求最短路径长度及最短路径 ...

  10. JSTL.带标签体的标签,方法和例子

    1. 实现 forEach 标签: 两个属性: items(集合类型, Collection), var(String 类型) doTag: 遍历 items 对应的集合 把正在遍历的对象放入到 pa ...