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 ...
随机推荐
- 51Nod 1021 石子归并(动态规划)
#include <iostream> #include <algorithm> #include <string> #include <iostream&g ...
- django网页渲染
模板标签种类 {% for blog in blog_list %} <h2>{{ blog.title }}</h2> <p>{{ blog.timestamp ...
- HDU - 6058 Kanade's sum
Bryce1010模板 http://acm.hdu.edu.cn/showproblem.php?pid=6058 /* 思路是:找出每个x为第k大的区间个数有多少 用pos[i]保存当前x的位置, ...
- DP Codeforces Round #303 (Div. 2) C. Woodcutters
题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...
- H5页面快速搭建之高级字体应用实践
原文出处: 淘宝前端团队(FED)- 龙驭 背景 最近在开发一个 H5 活动页快速搭建平台,可以通过拖拽编辑图片,文字等元素组件,快速搭建出一个移动端的活动页面,基本交互和成品效果类似 PPT 软件. ...
- python函数基础(3)
第1章 编码补充 1.1 字符编码对照表 1.2 编码特性 1.4 encode/decode第2章 集合 2.1 特点 2.2 [重点]作用:去重 2.3 常用操作 2.3.1 删除 2.3.2 交 ...
- .net excel 导入 导出
哎,好好的代码今天说来个实验,结果用的是office15 气死人了,网上最高office14.dll 文章转自2012年 QQ群:13615607 MR.Young protected void Bt ...
- Android CursorAdapter的使用
CursorAdapter继承于BaseAdapter,为Cursor和ListView连接提供了桥梁. 首先看一下CursorAdapter的部分源码: /** * @see android.wid ...
- 激活eclipse自动提示功能
eclipse设置: Window->Preferences->Java->Editor->Content Assist
- ETH Dapp 体验报告
Dapp 体验报告 Dapp是分散式的应用程序.DApp运行在去中心化的网络上,也就是区块链网络中.网络中不存在中心化的节点可以完整的控制DApp. 必须依赖合约部署,没有一个中心化的服务器托管. 对 ...