正好训练赛来了一道最长递减序列问题,所以好好研究了一下最长递增序列问题。

B - Testing the CATCHER

Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u

Description

A military contractor for the Department of Defense has just completed a series of preliminary tests for a new defensive missile called the CATCHER which is capable of intercepting multiple incoming offensive missiles. The CATCHER is supposed to be a remarkable defensive missile. It can move forward, laterally, and downward at very fast speeds, and it can intercept an offensive missile without being damaged. But it does have one major flaw. Although it can be fired to reach any initial elevation, it has no power to move higher than the last missile that it has intercepted.

The tests which the contractor completed were computer simulations of battlefield and hostile attack conditions. Since they were only preliminary, the simulations tested only the CATCHER's vertical movement capability. In each simulation, the CATCHER was fired at a sequence of offensive missiles which were incoming at fixed time intervals. The only information available to the CATCHER for each incoming missile was its height at the point it could be intercepted and where it appeared in the sequence of missiles. Each incoming missile for a test run is represented in the sequence only once.

The result of each test is reported as the sequence of incoming missiles and the total number of those missiles that are intercepted by the CATCHER in that test.

The General Accounting Office wants to be sure that the simulation test results submitted by the military contractor are attainable, given the constraints of the CATCHER. You must write a program that takes input data representing the pattern of incoming missiles for several different tests and outputs the maximum numbers of missiles that the CATCHER can intercept for those tests. For any incoming missile in a test, the CATCHER is able to intercept it if and only if it satisfies one of these two conditions:

The incoming missile is the first missile to be intercepted in this test. 
-or- 
The missile was fired after the last missile that was intercepted and it is not higher than the last missile which was intercepted.

Input

The input data for any test consists of a sequence of one or more non-negative integers, all of which are less than or equal to 32,767, representing the heights of the incoming missiles (the test pattern). The last number in each sequence is -1, which signifies the end of data for that particular test and is not considered to represent a missile height. The end of data for the entire input is the number -1 as the first value in a test; it is not considered to be a separate test.

Output

Output for each test consists of a test number (Test #1, Test #2, etc.) and the maximum number of incoming missiles that the CATCHER could possibly intercept for the test. That maximum number appears after an identifying message. There must be at least one blank line between output for successive data sets.

Note: The number of missiles for any given test is not limited. If your solution is based on an inefficient algorithm, it may not execute in the allotted time.

Sample Input

  1. 389
  2. 207
  3. 155
  4. 300
  5. 299
  6. 170
  7. 158
  8. 65
  9. -1
  10. 23
  11. 34
  12. 21
  13. -1
  14. -1

Sample Output

  1. Test #1:
  2. maximum possible interceptions: 6
  3.  
  4. Test #2:
  5. maximum possible interceptions: 2
 

一种是动态规划方法

  1. #include <iostream>
  2. #include <cstdio>
  3. int f[];
  4. int p[];
  5. int main()
  6. {
  7. int count=;
  8. int cur=;
  9. while (scanf("%d",&p[cur++]))
  10. {
  11. if (p[cur-]==-)
  12. if (cur-==) break;
  13. else
  14. {
  15. int i,j,k;
  16. for (i=;i<cur-;i++)
  17. f[i]=;
  18. for (i=;i<cur-;i++)//类似于背包一样进行物品循环
  19. {
  20. for (j=i-;j>=;j--)//类似于循环背包容量。
  21. {
  22. if (p[i]<=p[j]) //此题求得是递减序列,故符号位<=
  23. if (f[i]<f[j]+) f[i]=f[j]+;
  24. }
  25. }
  26. int ans=;
  27. for (i=;i<cur-;i++)
  28. {
  29. if (ans<f[i]) ans=f[i];//此时的最大量不一定是坐标最大时,所以要这样来一下。
  30. }
  31. printf("Test #%d:\n",++count);
  32. printf(" maximum possible interceptions: %d\n\n",ans);
  33. cur=;
  34. }
  35.  
  36. }
  37. return ;
  38. }

还有一种方法是贪心加二分的方法。

贴了一下大神博客的内容

开一个栈,每次取栈顶元素top和读到的元素temp做比较,如果temp > top 则将temp入栈;如果temp < top则二分查找栈中的比temp大的第1个数,并用temp替换它。 最长序列长度即为栈的大小top。

这也是很好理解的,对于x和y,如果x < y且Stack[y] < Stack[x],用Stack[x]替换Stack[y],此时的最长序列长度没有改变但序列Q的''潜力''增大了。

举例:原序列为1,5,8,3,6,7

栈为1,5,8,此时读到3,用3替换5,得到1,3,8; 再读6,用6替换8,得到1,3,6;再读7,得到最终栈为1,3,6,7。最长递增子序列为长度4。

用该算法完成POJ2533的具体代码如下:

 
  1. #include <iostream>
  2. #define SIZE 1001
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int i, j, n, top, temp;
  9. int stack[SIZE];
  10. cin >> n;
  11.  
  12. top = 0;
  13. /* 第一个元素可能为0 */
  14. stack[0] = -1;
  15. for (i = 0; i < n; i++)
  16. {
  17. cin >> temp;
  18. /* 比栈顶元素大数就入栈 */
  19. if (temp > stack[top])
  20. {
  21. stack[++top] = temp;
  22. }
  23. else
  24. {
  25. int low = 1, high = top;
  26. int mid;
  27. /* 二分检索栈中比temp大的第一个数 */
  28. while(low <= high)
  29. {
  30. mid = (low + high) / 2;
  31. if (temp > stack[mid])
  32. {
  33. low = mid + 1;
  34. }
  35. else
  36. {
  37. high = mid - 1;
  38. }
  39. }
  40. /* 用temp替换 */
  41. stack[low] = temp;
  42. }
  43. }
  44.  
  45. /* 最长序列数就是栈的大小 */
  46. cout << top << endl;
  47.  
  48. //system("pause");
  49. return 0;
  50. }

关于这道题,我的代码是

  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. int stack[];
  5. int p[];
  6.  
  7. int main()
  8. {
  9. int count=;
  10. int cur=;
  11. while (scanf("%d",&p[cur++]))
  12. {
  13. if (p[cur-]==-)
  14. if (cur-==) break;
  15. else
  16. {
  17. int i,j,k;
  18. int top=;
  19. stack[top++]=p[];//模拟棧的操作。
  20. for (i=; i<cur-; i++)
  21. {
  22. if (p[i]<stack[top-])
  23. stack[top++]=p[i];
  24. else
  25. {
  26. int l=,r=top,mid;
  27. while (l<r)//二分的地方要注意,在递增序列里,要使得能搜到正好大于p[i]的那个点。。。若为递减序列,则要能正好搜到小于p[i]的那个点
  28. {
  29. mid=(l+r)/;
  30. if (stack[mid]<p[i]) r=mid;//因为是递减序列,所以为小于
  31. else
  32. l=mid+;
  33. }
  34. stack[l]=p[i];
  35. }
  36. }
  37. printf("Test #%d:\n",++count);
  38. printf(" maximum possible interceptions: %d\n\n",top);
  39. cur=;
  40. }
  41.  
  42. }
  43. return ;
  44. }

LIS(最长上升子序列)的 DP 与 (贪心+二分) 两种解法的更多相关文章

  1. 【noi 2.6_1759】LIS 最长上升子序列(DP,3种解法)

    题意我就不写了.解法有3种: 1.O(n^2).2重循环枚举 i 和 j,f[i]表示前 i 位必选 a[i] 的最长上升子序列长度,枚举a[j]为当前 LIS 中的前一个数. 1 #include& ...

  2. 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列

    出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...

  3. 动态规划模板1|LIS最长上升子序列

    LIS最长上升子序列 dp[i]保存的是当前到下标为止的最长上升子序列的长度. 模板代码: int dp[MAX_N], a[MAX_N], n; int ans = 0; // 保存最大值 for ...

  4. POJ 1887 Testingthe CATCHER (LIS:最长下降子序列)

    POJ 1887Testingthe CATCHER (LIS:最长下降子序列) http://poj.org/problem?id=3903 题意: 给你一个长度为n (n<=200000) ...

  5. POJ - 3903 Stock Exchange(LIS最长上升子序列问题)

    E - LIS Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u   Descripti ...

  6. hdu 5256 序列变换(LIS最长上升子序列)

    Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数. 请输出最少需要修改多 ...

  7. POJ 3903 Stock Exchange (E - LIS 最长上升子序列)

    POJ 3903    Stock Exchange  (E - LIS 最长上升子序列) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action ...

  8. POJ 1157 LITTLE SHOP OF FLOWERS (超级经典dp,两种解法)

    You want to arrange the window of your flower shop in a most pleasant way. You have F bunches of flo ...

  9. 洛谷 P1439 【模板】最长公共子序列(DP,LIS?)

    题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入输出格式 输入格式: 第一行是一个数n, 接下来两行,每行为n个数,为自然数1-n的一个排列. 输出格式: 一个数,即最长公共子 ...

随机推荐

  1. java 加法变乘法

    加法变乘法 我们都知道:1+2+3+ - + 49 = 1225 (1) 现在要求你把其中两个不相邻的加号变成乘号,使得结果为2015 比如: 1+2+3+...+10*11+12+...+27*28 ...

  2. 在 Scale Up 中使用 Health Check【转】

    对于多副本应用,当执行 Scale Up 操作时,新副本会作为 backend 被添加到 Service 的负载均衡中,与已有副本一起处理客户的请求.考虑到应用启动通常都需要一个准备阶段,比如加载缓存 ...

  3. GNS3 模拟icmp重定向

    网关实质上是一个网络通向其他网络的IP地址.比如有网络A和网络B,网络A的IP地址范围为“192.168.1.1~192. 168.1.254”,子网掩码为255.255.255.0:网络B的IP地址 ...

  4. impala invalidate metadata和impala-shell -r作用相同

    impala的invalidate metadata内部命令,是否和外部命令impala-shell -r的作用相同的? 这个问题的回答: 在invalidate metadata 和 impala- ...

  5. 从植发AI看智能手术机器人的国产化之路

    在工作和生活的双重压力下,很多80后乃至90后的青年都"光荣"地加入了"脱发一族".为了拯救发际线或是不变成"地中海",很多人从此走上了寻医 ...

  6. C语言版数据结构算法

    C语言版数据结构算法 C语言数据结构具体算法 https://pan.baidu.com/s/19oLoEVqV1I4UxW7D7SlwnQ C语言数据结构演示软件 https://pan.baidu ...

  7. SpringAOP源码跟踪及学习

    Spring 版本 4.3.2 在拿到 Bean 实例以后,会经历一系列的初始化工作,如:工厂回调.init 方法.后处理器在 Bean 初始化前后的处理等,在一般情况下(非 factory-meth ...

  8. python集成开发环境Anaconda的安装

    参考博文: anaconda在Linux下的安装 Linux下anaconda3的安装 Anaconda的安装.启用及停用的步骤 Python学习之Anaconda的使用及配置方法 Anaconda ...

  9. Vmware 和 VisualSVN-Server端口冲突

    安装 VisualSVN-Server 时,发现他和 Vmware   在端口  443 冲突: 先把本地自启动的 Vmware 全部停止,并改成手工启动服务: 这样可以节省资源,再安装 svn服务时 ...

  10. 吴裕雄--天生自然java开发常用类库学习笔记:Set接口

    import java.util.HashSet ; import java.util.Set ; public class HashSetDemo01{ public static void mai ...