首先看一下题目:

Introduction

Dynamic programming is a confusing name for a programming technique that dramatically reduces the runtime of algorithms: from exponential to polynomial. The basic idea is to try to avoid solving the same problem or subproblem twice. Here is a problem to demonstrate its power:

Given a sequence of as many as 10,000 integers (0 < integer < 100,000), what is the maximum decreasing subsequence? Note that the subsequence does not have to be consecutive.

原题说,有10000个整数,求其中最长递减子序列的长度,其中子序列是可以不连续的。例如,给定序列100, 5, 99, 98,其中最长递减子序列就是100,99,98。从这个例子可以看出,我们很有可能需要舍掉一些小到过分的元素,来保证后面的数字可以大一些,使得更后面的数字有下降的空间。

为了测试的方便,我们将这里的10000个整数暂时换成别的数目的整数。

以下是官方给出的最朴素的解法,我在上做了一些微小的修改。这种做法的实质就是把所有的递减序列找出来,然后找出长度最长的。

#include <iostream>
#include <cstdio> using namespace std;
const int maxn = ;
int n;
int sequence[maxn]; int check(int start, int nmatches, int smallest); int main() {
freopen("test.in", "r", stdin);
cin >> n;
for(int i = ; i < n; i++) {
cin >> sequence[i];
}
cout << check(, , );
return ;
} int check(int start, int nmatches, int smallest) {
cout << "Check!" << endl;
cout << start << " " << nmatches << " " << smallest << endl;
int better;
int best = nmatches;
for(int i = start; i < n; i++) {
if(sequence[i] < smallest) {
better = check(i, nmatches + , sequence[i]);
if(better > best) {
best = better;
}
}
}
return best;
}

其中,text.in的数据是我随机生成的,如下:

          

这里的check函数是使用了递归的,递归的终止条件是for循环运行结束,递归的状态转移是在添加了新的数字到递减序列后能达到的最大长度。

如果对于算法一下不能看懂的话,那么可以对照着运行结果看。

运行结果如下:

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

Check!

这里是说,对于递减子序列,我们需要把每一个在原序列中的可能成为当前子序列的末尾元素的元素进行测试。就比如最小子序列的第0号元素,因为任何一个元素都可以成为一个长度为1的递减子序列的,所以最小子序列的第0号元素可能是64,65,97,43,5,36,92,72,87,44中的任何一个。但是假设我们已经选定了第0号元素是64,那么第1号元素就有可能是43,5,36,44中的任意一个。

64 65 97 43 5 36 92 72 87 44

USACO Dynamic Programming (1)的更多相关文章

  1. 动态规划 Dynamic Programming

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

  2. Dynamic Programming

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

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

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

  4. hdu 4223 Dynamic Programming?

    Dynamic Programming? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  5. 算法导论学习-Dynamic Programming

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

  6. Dynamic Programming: From novice to advanced

    作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...

  7. HDU-4972 A simple dynamic programming problem

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

  8. [算法]动态规划(Dynamic programming)

    转载请注明原创:http://www.cnblogs.com/StartoverX/p/4603173.html Dynamic Programming的Programming指的不是程序而是一种表格 ...

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

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

随机推荐

  1. BM算法详解

    http://www-igm.univ-mlv.fr/~lecroq/string/node14.html http://www.cs.utexas.edu/users/moore/publicati ...

  2. Java 脚本化编程指南

    Java 脚本化编程指南 Java脚本化API为谁准备? 脚本语言的一些有用的特性是: 方便:大多数脚本语言都是动态类型的.您通常可以创建新的变量,而不声明变量类型,并且您可以重用变量来存储不同类型的 ...

  3. iOS 生成随机字符串 从指定字符串随机产生n个长度的新字符串

    随机字符串 - 生成指定长度的字符串 -(NSString *)randomStringWithLength:(NSInteger)len { NSString *letters = @"a ...

  4. 在Spring、Hibernate中使用Ehcache缓存(2)

    这里将介绍在Hibernate中使用查询缓存.一级缓存.二级缓存,整合Spring在HibernateTemplate中使用查询缓存.,这里是hibernate3,使用hibernate4类似,不过不 ...

  5. Linux下部署tomcat

    在Linux系统下,重启Tomcat使用命令操作的! 首先,进入Tomcat下的bin目录 cd /usr/local/tomcat/bin 使用Tomcat关闭命令 ./shutdown.sh 查看 ...

  6. Python Tkinter学习(1)——第一个Tkinter程序

    注:本文可转载,转载请注明出处:http://www.cnblogs.com/collectionne/p/6885066.html.格式修改未完成. Tkinter资料 Python Wiki, T ...

  7. 正则表达式入门案例C#

    ---恢复内容开始--- 在网上百度了好多关于正则表达式的,不过好多都是关于语法的,没有一个具体的案例,有点让人难以入门,毕竟我还是喜欢由具体到抽象的认识.所以我就在这先提供了一个入门小案例(学了了6 ...

  8. SQL Server AG集群启动不起来的临时自救大招

    SQL Server AG集群启动不起来的临时自救大招 背景 前晚一朋友遇到AG集群发生来回切换不稳定的情况,情急之下,朋友在命令行使用命令重启WSFC集群 结果重启WSFC集群之后,非但没有好转,导 ...

  9. python装饰器大详解

    1.作用域 在python中,作用域分为两种:全局作用域和局部作用域. 全局作用域是定义在文件级别的变量,函数名.而局部作用域,则是定义函数内部. 关于作用域,我要理解两点:a.在全局不能访问到局部定 ...

  10. 原生和jQuery的ajax用法

    jQuery的ajax方法: $.ajax({ url:'/comm/test1.php', type:'POST', //GET async:true, //或false,是否异步 data:{ n ...