HDU 1257 最少拦截系统(最长上升子序列)
题意:
给定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 最少拦截系统(最长上升子序列)的更多相关文章
- HDU 1257 最少拦截系统 最长递增子序列
HDU 1257 最少拦截系统 最长递增子序列 题意 这个题的意思是说给你\(n\)个数,让你找到他最长的并且递增的子序列\((LIS)\).这里和最长公共子序列一样\((LCS)\)一样,子序列只要 ...
- hdu 1257 最少拦截系统 求连续递减子序列个数 (理解二分)
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 1257 最少拦截系统(贪心 or LIS)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 1257最少拦截系统[动态规划]
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1257 最 ...
- POJ - 2533 Longest Ordered Subsequence与HDU - 1257 最少拦截系统 DP+贪心(最长上升子序列及最少序列个数)(LIS)
Longest Ordered Subsequence A numeric sequence of ai is ordered if a1 < a2 < ... < aN. Let ...
- HDU 1257 最少拦截系统【最长上升子序列】
解题思路:可以转化为求最长上升子序列来做,还是可以用an与按升序排列后的an求LCS来做,为防止超时,用滚动数组优化一下就可以了. 最少拦截系统 Time Limit: 2000/1000 MS (J ...
- HDU 1257 最少拦截系统(Dilworth定理+LIS)
最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- hdu 1257 最少拦截系统【贪心 || DP——LIS】
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1257 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU 1257——最少拦截系统——————【LIS变型题】
最少拦截系统 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
- HDU 1257 最少拦截系统 (DP || 贪心)
最少拦截系统 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Statu ...
随机推荐
- Centos 7.x 配置Gitlab
GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务. 1. 安装并配置必要的依赖关系 如果你想使用 Postfix 发送邮件,请在安装过程中根 ...
- nginx使用autoindex
有时候一个nginx服务就是为了用来下载文件的,网上很多下载服务都是这样的 这个很简单 在http段加上以下参数,重启nginx就行. autoindex on; autoindex_exact_si ...
- 洛谷 P3455 [POI2007]ZAP-Queries || 洛谷P2522,bzoj2301
https://www.luogu.org/problemnew/show/P3455 就是https://www.cnblogs.com/hehe54321/p/9315244.html里面的方法2 ...
- Sereja and Brackets(括号匹配)
Description Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length ...
- 微信小程序消息推送,前端操作
<form bindsubmit="getFormId" report-submit="true"> <button form-type=&q ...
- JavaScript入门2
5.document对象:Document对象是window对象的一个对象属性,代表浏览器窗口中装载的整个HTML文档.文档中的每个HTML元素对应着JavaScript对象. 因为document代 ...
- python正则表达式多次提取数据(一个规则提取多组数据)
import re ttt='"FileName":"陈雪凝 - <em>绿色<\/em>","AlbumID":& ...
- 498 Diagonal Traverse 对角线遍历
详见:https://leetcode.com/problems/diagonal-traverse/description/ C++: class Solution { public: vector ...
- 将php数组传递到js—json_encode(),json_decode()
json_decode(),对一个json字符串进行解码,json_encode()是生成一个json字符串 上面的解释很清楚了,关于php里数组赋值的问题,列举如下: <?php //对象 c ...
- js删除最后一个字符
在最近做一个系统,使用socket来完成后台操作,C#来完成前端操作.但是在定的协议里面,一定要用某个符号来表示传的数据结束.后台进行交互时,获取到的数据必须进行删除最后一个字符的操作. 比如我们协议 ...