HDU 1257 最少拦截系统 最长递增子序列

题意

这个题的意思是说给你\(n\)个数,让你找到他最长的并且递增的子序列\((LIS)\)。这里和最长公共子序列一样\((LCS)\)一样,子序列只要满足前后关系即可,不需要相邻。

解题思路

解法一:这个可以用动态规划来实现,\(dp[i]\)代表前\(i\)个数列中以第\(i\)个数为结尾的\(LIS\)的长度。递推关系如下:

\[dp[i] = \begin{aligned} & max(dp[k])+1 & \text{k=1,2...(i-1)} \end{aligned}
\]

复杂度为\(n^2\)。

解法二:这里一个\(dp[i]\),但是代表是长度为\(i\)的子序列中最后一个元素是多少,这里这个元素要尽量小。因为在同等长度下,最后一个结尾的数值越小”越好“。复杂度为\(nlogn\)。效率很高了。

这个递推关系有点复杂,详情看代码实现或者搜索\(LIS\)会有相关的博客讲解。

代码实现

//解法一的形式
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e3+7;
int dp[maxn][2];
int n;
int main() {
while(scanf("%d",&n)!=EOF) {
for(int i=1; i<=n; i++) {
scanf("%d", &dp[i][0]);
dp[i][1]=1;
}
int ans=1;
for(int i=2; i<=n; i++) {
for(int j=1; j<i; j++) {
if(dp[j][0] < dp[i][0] && dp[j][1]+1 > dp[i][1])
dp[i][1]=dp[j][1]+1;
}
ans=max(ans, dp[i][1]);
}
printf("%d\n",ans);
}
return 0;
}
//解法二
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1e3+7;
int dp[maxn], num[maxn];
int n;
int main() {
while(scanf("%d", &n)!=EOF) {
for(int i=1; i<=n; i++) {
scanf("%d",&num[i]);
dp[i]=inf;
}
int j, ans=0;
for(int i=1; i<=n; i++) {
j=lower_bound(dp+1, dp+n+1, num[i])-dp;
ans=max(ans, j);
dp[j]=num[i];
}
printf("%d\n", ans);
} return 0;
}

HDU 1257 最少拦截系统 最长递增子序列的更多相关文章

  1. hdu 1257 最少拦截系统 求连续递减子序列个数 (理解二分)

    最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  2. HDU 1257 最少拦截系统(贪心 or LIS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)   ...

  3. HDU 1257最少拦截系统[动态规划]

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1257                                                 最 ...

  4. POJ - 2533 Longest Ordered Subsequence与HDU - 1257 最少拦截系统 DP+贪心(最长上升子序列及最少序列个数)(LIS)

    Longest Ordered Subsequence A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let ...

  5. HDU 1257 最少拦截系统【最长上升子序列】

    解题思路:可以转化为求最长上升子序列来做,还是可以用an与按升序排列后的an求LCS来做,为防止超时,用滚动数组优化一下就可以了. 最少拦截系统 Time Limit: 2000/1000 MS (J ...

  6. hdu 1257 最少拦截系统【贪心 || DP——LIS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  7. HDU 1257——最少拦截系统——————【LIS变型题】

    最少拦截系统 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  8. HDU 1257 最少拦截系统(Dilworth定理+LIS)

    最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

  9. HDU 1257 最少拦截系统 (DP || 贪心)

    最少拦截系统 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Statu ...

随机推荐

  1. 哈密尔顿环x

    欧拉回路是指不重复地走过所有路径的回路,而哈密尔顿环是指不重复地走过所有的点,并且最后还能回到起点的回路.  代码如下: #include<iostream> #include<cs ...

  2. Acwing:137. 雪花雪花雪花(Hash表)

    有N片雪花,每片雪花由六个角组成,每个角都有长度. 第i片雪花六个角的长度从某个角开始顺时针依次记为ai,1,ai,2,…,ai,6ai,1,ai,2,…,ai,6. 因为雪花的形状是封闭的环形,所以 ...

  3. 本机向window服务器传送数据

    我们需要在远程服务器中安装 FileZilla_Server,在本机安装 FileZilla 客户端 FileZilla 服务器下载地址:https://pan.baidu.com/s/1o7Aen2 ...

  4. @Transient使用心得

    使用注解@Transient使表中没有此字段 注意,实体类中要使用org.springframework.data.annotation.Transient 在写实体类时发现有加@Transient注 ...

  5. SpringMVC参数传递 HttpServletRequest,HttpServletResponse和HttpSession

    SpringMVC参数传递 HttpServletRequest,HttpServletResponse和HttpSession 2017-11-27 16:44:51 douunderstand 阅 ...

  6. spark 笔记 6: RDD

    了解RDD之前,必读UCB的论文,个人认为这是最好的资料,没有之一. http://www.cs.berkeley.edu/~matei/papers/2012/nsdi_spark.pdf A Re ...

  7. leetcode-easy-math-412 Fizz Buzz

    mycode  99.06% class Solution(object): def fizzBuzz(self, n): """ :type n: int :rtype ...

  8. leetcode-easy-string-14 Longest Common Prefix

    mycode   91.59% class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if not str ...

  9. 统计学_Wilcoxon signed-rank test(python脚本)

    python机器学习-乳腺癌细胞挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...

  10. Django中csrf token验证原理

    我多年没维护的博客园,有一篇初学Django时的笔记,记录了关于django-csrftoekn使用笔记,当时几乎是照抄官网的使用示例,后来工作全是用的flask.博客园也没有维护.直到我的博客收到了 ...