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优化一下。。。
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define pi (4*atan(1.0))
  5. #define eps 1e-14
  6. const int N=2e5+,M=4e6+,inf=1e9+,mod=1e9+;
  7. const ll INF=1e18+;
  8. int a[N];
  9. int pre[N];
  10. int main()
  11. {
  12. int T,cas=;
  13. scanf("%d",&T);
  14. while(T--)
  15. {
  16. int n;
  17. scanf("%d",&n);
  18. for(int i=;i<=n;i++)
  19. scanf("%d",&a[i]);
  20. for(int i=;i<=n;i++)
  21. pre[i]=pre[i-]+a[i];
  22. int ans=inf;
  23. for(int i=;i<=n;i++)
  24. for(int t=i;t<=n;t++)
  25. {
  26. ans=min(ans,abs(pre[t]-pre[i-]));
  27. }
  28. printf("Case %d: %d\n",cas++,ans);
  29. }
  30. return ;
  31. }
 

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. linux设备驱动归纳总结(四):5.多处理器下的竞态和并发【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-67673.html linux设备驱动归纳总结(四):5.多处理器下的竞态和并发 xxxxxxxxxx ...

  2. linux设备驱动归纳总结(一)内核的相关基础概念【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-59413.html linux设备驱动归纳总结(一):内核的相关基础概念 xxxxxxxxxxxxxx ...

  3. 160914、ionic指令简单布局

    1) 添加引用类库(ionic样式和ionic js文件) 2) 标题栏,页脚栏,内容区 3) Js引入ionic类库,添加页面操作方法和对象 4) 数据初始化 5) Html页面绑定方法和对象 &l ...

  4. makefile 中 $@ $^ %< 使用【转】

    转自:http://blog.csdn.net/kesaihao862/article/details/7332528 这篇文章介绍在LINUX下进行C语言编程所需要的基础知识.在这篇文章当中,我们将 ...

  5. 坑爹的VS2012

    2.2.2.如果卸载 Visual Studio 2010 Service Pack 1,则必须先重新安装 Visual Studio 2010,然后才能再次安装 SP1 如果卸载 Visual St ...

  6. 一道面试题比较synchronized和读写锁

    一.科普定义 这篇博文的两个主角“synchronized”和“读写锁” 1)synchronized 这个同步关键字相信大家都用得比较多,在上一篇“多个线程之间共享数据的方式”中也详细列举他的应用, ...

  7. 26、Oracle(二)

    1)掌握多行函数与分组操作 2)掌握多表查询与子查询 3)理解集合查询与掌握Oracle分页语法 4)理解创建.修改和删除表和表约束 --------------------------------- ...

  8. SQL数据类型大全 《转自网络》

    数据类型是数据的一种属性,表示数据所表示信息的类型.任何一种计算机语言都定义了自己的数据类型.当然,不同的程序语言都具有不同的特点,所定义的数据类型的种类和名称都或多或少有些不同.SQLServer ...

  9. c# 遍历子控件,比如Form下的group,或者panel

    方法很好用.目的是遍历所有容器的子控件... 方法1private void GetControl(Control.ControlCollection ctc, ref int checkNull) ...

  10. (翻译)理解Java当中的回调机制

    原文地址:http://cleancodedevelopment-qualityseal.blogspot.com/2012/10/understanding-callbacks-with-java. ...