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. Javascript Window Location

    window.location 对象在编写时可不使用 window 这个前缀. URL : 统一资源定位符 (Uniform Resource Locator) 说明: 完整的URL示例:scheme ...

  2. iOS完整App资源收集

    前言 iOS开发学习者都希望得到实战训练,但是很多资料都是只有一小部分代码,并不能形成完成的App,笔者在此处收集了很多开源的完整的App,都有源代码哦! 本篇文章持续更新中,请持续关注.本篇所收集的 ...

  3. SQLServer学习笔记<> 表连接查询----交叉连接、内连接、左连接、右连接

    (1)交叉连接(cross join)即我们所说的笛卡尔积.查询出满足两张表所有的记录数,A(3条记录),B(9条记录),A*B(27条记录). 比如:雇员表(HR.employees)和货运公司(S ...

  4. ACM题目————已知前序和中序求后序

    #include <iostream> #include <cstring> #include <cstdio> using namespace std; ], z ...

  5. 两句话概括“sql外键”

    外键的使用就是: 1.外键表可以删除,外键表删完了 才能删主键表2.添加的时候不能添加在主键没有的内容

  6. 结对2.0--复利计算WEB升级版

    结对2.0--复利计算WEB升级版 复利计算再升级------------------------------------------------------------ 客户在大家的引导下,有了更多 ...

  7. javaWEB中的HttpServlet(企业开发使用)

    HttpServlet: 1). 是一个 Servlet, 继承自 GenericServlet. 针对于 HTTP 协议所定制. 2). 在 service() 方法中直接把 ServletReuq ...

  8. HDU(1175),连连看,BFS

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1175 越学越不会,BFS还是很高级的. 连连看 Time Limit: 20000/100 ...

  9. ..在lua中运用

    ..连接两个字符串 th> a="hello" th> b="world" th> print(a..b) helloworld th> ...

  10. Mysql-学习笔记(==》建表修改一)

    -- 建立表CREATE TABLE 表名( )ENGINE=存储引擎(MYISAM INNODB) AUTO_INCREMENT=100 DEFAULT CHARSET=utf8; CREATE T ...