LIS(最长上升子序列)的 DP 与 (贪心+二分) 两种解法
正好训练赛来了一道最长递减序列问题,所以好好研究了一下最长递增序列问题。
Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u
Description
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
Output
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
389
207
155
300
299
170
158
65
-1
23
34
21
-1
-1
Sample Output
Test #1:
maximum possible interceptions: 6 Test #2:
maximum possible interceptions: 2
一种是动态规划方法
#include <iostream>
#include <cstdio>
int f[];
int p[];
int main()
{
int count=;
int cur=;
while (scanf("%d",&p[cur++]))
{
if (p[cur-]==-)
if (cur-==) break;
else
{
int i,j,k;
for (i=;i<cur-;i++)
f[i]=;
for (i=;i<cur-;i++)//类似于背包一样进行物品循环
{
for (j=i-;j>=;j--)//类似于循环背包容量。
{
if (p[i]<=p[j]) //此题求得是递减序列,故符号位<=
if (f[i]<f[j]+) f[i]=f[j]+;
}
}
int ans=;
for (i=;i<cur-;i++)
{
if (ans<f[i]) ans=f[i];//此时的最大量不一定是坐标最大时,所以要这样来一下。
}
printf("Test #%d:\n",++count);
printf(" maximum possible interceptions: %d\n\n",ans);
cur=;
} }
return ;
}
还有一种方法是贪心加二分的方法。
贴了一下大神博客的内容
开一个栈,每次取栈顶元素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的具体代码如下:
#include <iostream>  | 
关于这道题,我的代码是
#include <iostream>
#include <cstdio>
using namespace std;
int stack[];
int p[]; int main()
{
int count=;
int cur=;
while (scanf("%d",&p[cur++]))
{
if (p[cur-]==-)
if (cur-==) break;
else
{
int i,j,k;
int top=;
stack[top++]=p[];//模拟棧的操作。
for (i=; i<cur-; i++)
{
if (p[i]<stack[top-])
stack[top++]=p[i];
else
{
int l=,r=top,mid;
while (l<r)//二分的地方要注意,在递增序列里,要使得能搜到正好大于p[i]的那个点。。。若为递减序列,则要能正好搜到小于p[i]的那个点
{
mid=(l+r)/;
if (stack[mid]<p[i]) r=mid;//因为是递减序列,所以为小于
else
l=mid+;
}
stack[l]=p[i];
}
}
printf("Test #%d:\n",++count);
printf(" maximum possible interceptions: %d\n\n",top);
cur=;
} }
return ;
}
LIS(最长上升子序列)的 DP 与 (贪心+二分) 两种解法的更多相关文章
- 【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& ...
 - 算法设计 - LCS 最长公共子序列&&最长公共子串 &&LIS 最长递增子序列
		
出处 http://segmentfault.com/blog/exploring/ 本章讲解:1. LCS(最长公共子序列)O(n^2)的时间复杂度,O(n^2)的空间复杂度:2. 与之类似但不同的 ...
 - 动态规划模板1|LIS最长上升子序列
		
LIS最长上升子序列 dp[i]保存的是当前到下标为止的最长上升子序列的长度. 模板代码: int dp[MAX_N], a[MAX_N], n; int ans = 0; // 保存最大值 for ...
 - POJ 1887 Testingthe CATCHER (LIS:最长下降子序列)
		
POJ 1887Testingthe CATCHER (LIS:最长下降子序列) http://poj.org/problem?id=3903 题意: 给你一个长度为n (n<=200000) ...
 - POJ - 3903 Stock Exchange(LIS最长上升子序列问题)
		
E - LIS Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descripti ...
 - hdu 5256 序列变换(LIS最长上升子序列)
		
Problem Description 我们有一个数列A1,A2...An,你现在要求修改数量最少的元素,使得这个数列严格递增.其中无论是修改前还是修改后,每个元素都必须是整数. 请输出最少需要修改多 ...
 - POJ 3903    Stock Exchange  (E - LIS 最长上升子序列)
		
POJ 3903 Stock Exchange (E - LIS 最长上升子序列) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action ...
 - 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 ...
 - 洛谷 P1439 【模板】最长公共子序列(DP,LIS?)
		
题目描述 给出1-n的两个排列P1和P2,求它们的最长公共子序列. 输入输出格式 输入格式: 第一行是一个数n, 接下来两行,每行为n个数,为自然数1-n的一个排列. 输出格式: 一个数,即最长公共子 ...
 
随机推荐
- git/github使用详解
			
介绍:gitHub是一个面向开源及私有软件项目的托管平台,因为只支持git 作为唯一的版本库格式进行托管,故名gitHub. 2018年6月4日,微软宣布,通过75亿美元的股票交易收购代码托管平台Gi ...
 - 深度解析标点符号在Report写作中的应用
			
准确的标点符号和大写字母可以帮助Tutor准确理解report的意思.标点符号的某些方面,例如使用逗号,可以是一种个人风格,在引号中正确的标点符号是至关重要的.在前面的一些文章当中我们也给大家说了re ...
 - MyBatis-Insert、Delete、Update的注意事项
			
MyBatis-Insert.Delete.Update的注意事项 插入/更新乱码的解决 出现插入乱码,首先要考虑数据库的编码集是不是UTF-8 如果数据库的编码无误,查看MyBatis的全局配置文件 ...
 - HTML学习第五天
			
HTML学习第五天 今天学HTML的实体.背景.布局 HTML布局的标签基本被淘汰frameset就被淘汰了,只有iframe依然存活,但是iframe可以被CSS给代替.下面就是一个练习的程序 &l ...
 - pip install .whl文件时is not a supported wheel on this platform.解决方法
			
首先,在python中输入import pip和print(pip.pep425tags.get_supported()),从而获取pip支持的文件名和版本. somnus@somnus-HP-Pa ...
 - 《高性能MySQL》之EXPLAIN
			
使用explain关键字获取sql执行性能 语法如下: explain select * from table explain 中的列expain出来的信息有10列,分别是id,select_type ...
 - 003.Delphi插件之QPlugins,菜单插件加强
			
相比上一篇的菜单插件,这个在创建和销毁时候,做了增强,同时做了2个菜单对应的窗口 unit MenuSvc; interface uses windows, classes, SysUtils, Gr ...
 - ErrorCode=-2147217900 表已存在.
			
ErrorCode=-2147217900 表已存在. 在导出excel时遇到这个问题. 原因是dataTable的TableName中有减号 "-"
 - CSS样式表——格式与选择器
			
1.分类 1)内联(写在标签内部) style="样式" 控制精确,代码重用性差 2)内嵌(在<head></head>中) <style type= ...
 - 【pwnable.kr】 codemap
			
pwnable新的一题. download: http://pwnable.kr/bin/codemap.exe ssh codemap@pwnable.kr -p2222 (pw:guest) 这道 ...