POJ 1887 Testingthe CATCHER (LIS:最长下降子序列)
POJ 1887Testingthe CATCHER (LIS:最长下降子序列)
http://poj.org/problem?id=3903
题意:
给你一个长度为n (n<=200000) 的数字序列, 要你求该序列中的最长(严格)下降子序列的长度.
分析:
读取全部输入, 将原始数组逆向,
然后求最长严格上升子序列就可以.
因为n的规模达到20W, 所以仅仅能用O(nlogn)的算法求.
令g[i]==x表示当前遍历到的长度为i的全部最长上升子序列中的最小序列末尾值为x.(假设到眼下为止,
根本不存在长i的上升序列,
那么x==INF无穷大)
如果当前遍历到了第j个值即a[j], 那么先找到g[n]数组的值a[j]的下确界(即第一个>=a[j]值的g[k]的k值).
那么此时表明存在长度为k-1的最长上升子序列且该序列末尾的位置<j且该序列末尾值<a[j].
那么我们能够令g[k]=a[j] 且 dp[i]=k (dp含义如解法1).
(上面一段花时间细致理解)
终于我们能够找出下标最大的i使得: g[i]<INF 中i下标最大. 这个i就是LIS的长.
AC代码: O(n*logn)复杂度
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=200000+5;
const int INF=1e8; int n;
int a[maxn];
int g[maxn]; int main()
{
int kase=0;
while(scanf("%d",&a[1])==1 && a[1]!=-1)
{
if(kase>0) printf("\n");
n=2;
while(scanf("%d",&a[n])==1 && a[n]!=-1)
{
n++;
}
n--; reverse(a+1,a+n+1);
for(int i=1;i<=n;i++)
g[i]=INF; int ans=0;
for(int i=1;i<=n;i++)
{
int k=lower_bound(g+1,g+n+1,a[i])-g;
g[k]=a[i];
ans=max(ans,k);
}
printf("Test #%d:\n maximum possible interceptions: %d\n",++kase,ans);
}
return 0;
}
POJ 1887 Testingthe CATCHER (LIS:最长下降子序列)的更多相关文章
- poj 1887 Testing the CATCHER_最长上升子序列
题意:题目太长没看,直接看输入输出猜出是最长下降子序列 用了以前的代码直接a了,做法类似贪心,把最小的顺序数存在数组里面,每次二分更新数组得出最长上升子序列 #include<iostream& ...
- POJ3903Stock Exchange&&POJ1631Bridging signals最长上升子序列 &&POJ1887Testing the CATCHER(最长下降子序列)(LIS模版题)
题目链接:http://poj.org/problem?id=3903 题目链接:http://poj.org/problem?id=1631 题目链接:http://poj.org/problem? ...
- POJ 1631 Bridging signals (LIS:最长上升子序列)
题意:给你一个长为n(n<=40000)的整数序列, 要你求出该序列的最长上升子序列LIS. 思路:要求(nlogn)解法 令g[i]==x表示当前遍历到的长度为i的所有最长上升子序列中的最小序 ...
- POJ 3903 Stock Exchange (E - LIS 最长上升子序列)
POJ 3903 Stock Exchange (E - LIS 最长上升子序列) 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action ...
- POJ-1887 Testing the CATCHER(dp,最长下降子序列)
Testing the CATCHER Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16515 Accepted: 6082 ...
- 低价购买 (动态规划,变种最长下降子序列(LIS))
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
- POJ - 3903 Stock Exchange(LIS最长上升子序列问题)
E - LIS Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descripti ...
- 【最长下降子序列的长度和个数】 poj 1952
转自http://blog.csdn.net/zhang360896270/article/details/6701589 这题要求最长下降子序列的长度和个数,我们可以增加数组maxlen[size] ...
- POJ 1836 Alignment(DP max(最长上升子序列 + 最长下降子序列))
Alignment Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 14486 Accepted: 4695 Descri ...
随机推荐
- 洛谷1345 [Usaco5.4]奶牛的电信
题目描述 农夫约翰的奶牛们喜欢通过电邮保持联系,于是她们建立了一个奶牛电脑网络,以便互相交流.这些机器用如下的方式发送电邮:如果存在一个由c台电脑组成的序列a1,a2,...,a(c),且a1与a2相 ...
- (50)与magento集成
我对接的是 odoo8 和 magento1.9.x 准备工作: l 服务器 装上mangento 组件 : $ pip install magento 装上 requests 组件:$ pip ...
- 紫书 例题 11-1 UVa 12219 (表达式树)
这道题看了刘汝佳的代码真的是天秀, 很值得学习. 具体看代码 #include<cstdio> #include<iostream> #include<cctype> ...
- 【codeforces 29B】Traffic Lights
[题目链接]:http://codeforces.com/problemset/problem/29/B [题意] 一辆车; 让从A开到B; 然后速度是v; (只有在信号灯前面才能停下来..否则其他时 ...
- centos6.5安装配置nginx+php+mysql+httpsqs+ttserver+redis
一.准备 1.nginx, http://nginx.org/download/nginx-1.5.12.tar.gz 2.pcre, ftp://ftp.csx.cam.ac.uk/pub/soft ...
- C++primer书店程序
#include <iostream> #include <string> #include <cassert> #include <algorithm> ...
- vijos - P1732能量採集 (状态转移)
P1732能量採集 Accepted 标签:NOI2010[显示标签] 背景 描写叙述 栋栋有一块长方形的地.他在地上种了一种能量植物,这样的植物能够採集太阳光的能量. 在这些植物採集能量后,栋栋再使 ...
- codeforces #313(div 2)
B. Gerald is into Art time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- less09 判断语句
less //.mixin (@a) when (lightness(@a) >= 50%) { //255/2=127.5 // background-color: black; //} // ...
- Pascal Script
MsgBox http://www.jrsoftware.org/ishelp/index.php?topic=isxfunc_msgbox ExpandConstant http://www.jrs ...