题意:

给定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. Incorrect string value: '\xE8\x8B\x8F\xE6\x99\xA8...' for column 'user_name' at row 1

    前端插入数据的时候报如下错误: Incorrect string value: '\xE8\x8B\x8F\xE6\x99\xA8...' for column 'user_name' at row ...

  2. C++ typedef typename 作用

    C++ typedef typename 作用 C++的一些语法让人看着费解,其中就有: typedef typename std::vector<T>::size_type size_t ...

  3. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  4. java 反向工具类

    1.ReflectUtil.java package com.example.scansell; import android.util.Log; import java.lang.reflect.C ...

  5. C#实现为类和函数代码自动添加版权注释信息的方法

    这篇文章主要介绍了C#实现为类和函数代码自动添加版权注释信息的方法,主要涉及安装文件的修改及函数注释模板的修改,需要的朋友可以参考下   本文实例讲述了C#实现为类和函数代码自动添加版权注释信息的方法 ...

  6. ArcGIS Desktop新建postgresql版sde(10.4.1)的连接

    假设连接到的sde数据库是pg数据库,其他参数包括: ip:10.0.0.8 数据库:sde1 用户:sde 密码:sde 打开catalog,新建数据库连接 按如下输入数据库连接参数 红框1是数据库 ...

  7. Android开发中SharedPreferences的使用

    在Android开发中,在储存少量的数据时,个人感觉SharedPreferences是最好的选择,SharedPreferences是以键值对的方式进行储存,支持boolean,int,float, ...

  8. B树、B+树、红黑树、AVL树

    定义及概念 B树 二叉树的深度较大,在查找时会造成I/O读写频繁,查询效率低下,所以引入了多叉树的结构,也就是B树.阶为M的B树具有以下性质: 1.根节点在不为叶子节点的情况下儿子数为 2 ~ M2. ...

  9. 用户授权policy

    定义策略类 php artisan make:policy PostPolicy app/Policies/PostPolicy.php public function update(User $us ...

  10. Mac上安装Homebrew和wget

    实际上是使用Homebrew来安装wget 安装Homebrew Homebrew一般称为brew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件, 只需要一个命令, 非常 ...