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. Add baidu map in your website (wordpress)

    手动挡 访问应用(AK)Key http://lbsyun.baidu.com/apiconsole/key Basic Map Generator http://api.map.baidu.com/ ...

  2. 设置 textarea 默认滑动到底部

    javascript: var textarea = document.getElementById('textarea_id'); textarea.scrollTop = textarea.scr ...

  3. CI 配置验证规则

    //判断表单域,提交表单显示对应的错误信息      $this->load->library('form_validation');      $config = array(      ...

  4. sharepint 数据视图 添加超链接

    1. 数值域清除数值,输入文本 详细进度 2. 添加连接 到 哪个页面 3. 将inteid拖过来 4. 连接到项目显示表单 5. 直接改下面的连接地址 <a href="http:/ ...

  5. JAVA字段的初始化规律

    JAVA字段的初始化规律 1.类的构造方法 (1)“构造方法”,也称为“构造函数”,当创建一个对象时,它的构造方法会被自动调用.构造方法与类名相同,没有返回值. (2)如果类没有定义构造函数,Java ...

  6. js笔记----(运动)淡入淡出

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  7. Javascript事件委托

      事件委托利用事件冒泡,只指定一个事件处理程序,就可以管理某一类型的所有事件未使用事件委托之前: <!DOCTYPE html> <html> <head> &l ...

  8. 调试器不能连接到STM32的问题与解决办法

    很多人都碰到过调试器不能连接到STM32的问题,不管是IAR的J-Link还是Keil的ULink,或者是ST的ST-Link.出现这个问题时,调试软件会提示不能建立与Cortex-M3的连接,或提示 ...

  9. winform中treeview中节点选中的技巧

    我想实现譬如选择某子节点的时候,父节点会自动选中,如果选择父节点,子节点会全部选中,如果子节点全部不选,父节点也要不选. 贴代码 private void tvwMenu_AfterCheck(obj ...

  10. lua中特殊用法

    th> a=torch.zeros(,) [.0001s] th> a [torch.DoubleTensor of size 1x5] [.0001s] th> a[{,floor ...