题意:

给定n个数, 然后要求看看有多少对不上升子序列。

分析:

求出最长上升子序列, 那么整个序列中LIS外的数都会在前面找到一个比自己大的数, 所以不上升子序列最多有最长上升子序列个数个。

关于求LIS, 下列有两种DP算法

O(n²)

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#define rep(i,a,b) for(int i = a; i < b; i++)
#define _rep(i,a,b) for(int i = a; i <= b; i++)
using namespace std;
int a[ + ], dp[ + ], n;
int main()
{
/* 求出最长上升子序列, 那么整个序列中LIS外的数都会在前面找到一个比自己大的数*/ while(cin >> n){
rep(i,,n) cin >> a[i]; int ans = -; rep(i,,n){
dp[i] = ;
rep(j,,i){
if(a[j] < a[i]) dp[i] = max(dp[i] , dp[j] + );
ans = max(ans, dp[i]);
}
}
printf("%d\n", ans); }
return ;
}

二分思想, 设置一个栈, 扫描一遍序列, 每次将大于栈顶元素的入栈, 小于栈顶元素的在栈中找到一个刚好大于等于该元素的替换掉, 复杂度O(nlogn)

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#include<algorithm>
#include<cmath>
#define rep(i,a,b) for(int i = a; i < b; i++)
#define _rep(i,a,b) for(int i = a; i <= b; i++)
using namespace std;
int a[ + ], dp[ + ], n;
int main()
{
/* 求出最长上升子序列, 那么整个序列中LIS外的数都会在前面找到一个比自己大的数*/ while(cin >> n){
rep(i,,n) {
cin >> a[i];
dp[i] = 1e9;
} for(int i = ; i < n; i++){
*lower_bound(dp, dp+n, a[i]) = a[i]; //每次找到一个>= a[i]的替换掉
}
printf("%d\n", lower_bound(dp,dp+n,1e9) - dp); //找到第一个INF的地址减去首地址就是最大子序列的长度; }

HDU 1257 最少拦截系统(最长上升子序列)的更多相关文章

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

    HDU 1257 最少拦截系统 最长递增子序列 题意 这个题的意思是说给你\(n\)个数,让你找到他最长的并且递增的子序列\((LIS)\).这里和最长公共子序列一样\((LCS)\)一样,子序列只要 ...

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 莫比乌斯函数 && HDU-1695

    莫比乌斯函数定义: $$\mu(d)=\begin{cases}1 &\text{d = 1}\\(-1)^r &\text{$d=p_1p_2...p_r,其中p_i为不同的素数$} ...

  2. Objective-C和 C++ 混编的要点(转)

    Using C++ With Objective-C苹果的Objective-C编译器允许用户在同一个源文件里自由地混合使用C++和Objective-C,混编后的语言叫Objective-C++.有 ...

  3. HBase备份恢复练习

    一.冷备 1.创建测试表并插入测试数据 [root@weekend05 ~]# hbase shell hbase(main):005:0> create 'scores','grade','c ...

  4. js修改物理返回键功能

    preventBack: function(theurl){ var pushState = window.history.pushState; //点击物理返回键时,退出到跳转定义首页 if(pus ...

  5. [转]如何使用MFC和类型库创建自动化项目

    本文转自:http://www.cnblogs.com/zhoug2020/archive/2012/04/01/2429064.html 摘要 本文详细介绍了如何自动化像Microsoft Offi ...

  6. 【C#】枚举

    枚举 public static class CommonEnums { public enum people { /// <summary> ///男人 /// </summary ...

  7. CF989C A Mist of Florescence

    思路: 有趣的构造题. 实现: #include <bits/stdc++.h> using namespace std; ][]; void fillin(int x, int y, c ...

  8. iOS infoq资料架构设计漫谈

    http://www.infoq.com/cn/ios/?utm_source=infoq&utm_medium=header_graybar&utm_campaign=topic_c ...

  9. Bundle的用法

    一.API文档说明 1.介绍 用于不同Activity之间的数据传递 1.重要方法 clear():清除此Bundle映射中的所有保存的数据. clone():克隆当前Bundle containsK ...

  10. Oracle体系结构总览

    第一篇 Oracle架构总览 先让我们来看一张图   这张就是Oracle 9i的架构全图.看上去,很繁杂.是的,是这样的.现在让我们来梳理一下: 一.数据库.表空间.数据文件 1.数据库 数据库是数 ...