https://www.luogu.org/problemnew/show/P1020

终于搞明白了。根据某定理,最少需要的防御系统的数量就是最长上升子序列的数量。

呵呵手写二分果然功能很多,想清楚自己要找的是什么就可以了。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll; const int INF=0x3f3f3f3f; int n;
int a[100005];
int dp[50005]; int f1(int *dp,int a){
int l=1,r=n;
while(1){
int m=(l+r)>>1;
if(m==l){
if(dp[l]<a)
return l;
else
return r;
}
if(dp[m]<a){
r=m;
}
else{
l=m+1;
}
}
return -1;
} int f2(int *dp,int a){
int l=1,r=n;
while(1){
int m=(l+r)>>1;
if(m==l){
if(dp[l]>=a)
return l;
else
return r;
}
if(dp[m]>=a){
r=m;
}
else{
l=m+1;
}
}
return -1;
} int main(){
n=0;
int t;
while(~scanf("%d",&t)){
n++;
a[n]=t;
} memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++){
int id=f1(dp,a[i]);
//cout<<"id="<<id<<endl;
dp[id]=a[i];
/*for(int k=1;k<=n;k++){
printf("dp[%d]=%d\n",k,dp[k]);
}*/
}
int ans=n;
for(int i=1;i<=n;i++){
if(dp[i]==0){
ans=i-1;
break;
}
}
printf("%d\n",ans); memset(dp,INF,sizeof(dp));
for(int i=1;i<=n;i++){
int id=f2(dp,a[i]);
//cout<<"id="<<id<<endl;
dp[id]=a[i];
/*for(int k=1;k<=n;k++){
printf("dp[%d]=%d\n",k,dp[k]);
}*/
} ans=n;
for(int i=1;i<=n;i++){
if(dp[i]==INF){
ans=i-1;
break;
}
}
printf("%d\n",ans);
}

洛谷 - P1020 - 导弹拦截 - 最长上升子序列的更多相关文章

  1. codevs1044 拦截导弹==洛谷 P1020 导弹拦截

    P1020 导弹拦截 题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天 ...

  2. 洛谷 P1020 导弹拦截(dp+最长上升子序列变形)

    传送门:Problem 1020 https://www.cnblogs.com/violet-acmer/p/9852294.html 讲解此题前,先谈谈何为最长上升子序列,以及求法: 一.相关概念 ...

  3. 【动态规划】【二分】【最长不下降子序列】洛谷 P1020 导弹拦截

    最长不下降子序列的nlogn算法 见 http://www.cnblogs.com/mengxm-lincf/archive/2011/07/12/2104745.html 这题是最长不上升子序列,倒 ...

  4. 洛谷 P1020导弹拦截题解

    洛谷链接:https://www.luogu.org/problem/P1020 题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到 ...

  5. 洛谷P1020 导弹拦截【单调栈】

    题目:https://www.luogu.org/problemnew/show/P1020 题意: 给定一些导弹的高度. 一个导弹系统只能拦截高度不增的一系列导弹,问如果只有一个系统最多能拦截多少导 ...

  6. codevs——T1044 拦截导弹 || 洛谷——P1020 导弹拦截

    http://codevs.cn/problem/1044/ || https://www.luogu.org/problem/show?pid=1020#sub 时间限制: 1 s  空间限制: 1 ...

  7. 洛谷P1020导弹拦截——LIS

    题目:https://www.luogu.org/problemnew/show/P1020 主要是第二问,使用了dilworth定理:一个序列中最长不上升子序列的最大覆盖=最长上升子序列长度. di ...

  8. 洛谷P1020 导弹拦截 题解 LIS扩展题 Dilworth定理

    题目链接:https://www.luogu.com.cn/problem/P1020 题目大意: 给你一串数,求: 这串数的最长不上升子序列的长度: 最少划分成多少个子序列是的这些子序列都是不上升子 ...

  9. 洛谷 [P1020] 导弹拦截 (N*logN)

    首先此一眼就能看出来是一个非常基础的最长不下降子序列(LIS),其朴素的 N^2做法很简单,但如何将其优化成为N*logN? 我们不妨换一个思路,维护一个f数组,f[x]表示长度为x的LIS的最大的最 ...

随机推荐

  1. nodejs-runoob

    是否安装成功nodejs - node -v 是否安装成功npm - npm -v

  2. windows快捷键大全(转载)

    常见用法: F1 显示当前程序或者windows的帮助内容. F2 当你选中一个文件的话,这意味着“重命名” F3 当你在桌面上的时候是打开“查找:所有文件” 对话框 F10或ALT 激活当前程序的菜 ...

  3. C# 调用类库里的事件

    首先在类库中定义事件: //定义委托 public delegate void DelWeiTuo(string name); //定义事件 public event DelWeiTuo EventW ...

  4. 九度OJ 1138:进制转换 (进制转换)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2388 解决:935 题目描述: 将一个长度最多为30位数字的十进制非负整数转换为二进制数输出. 输入: 多组数据,每行为一个长度不超过30 ...

  5. ZeroMQ Distributed Messaging

    ZeroMQ \zero-em-queue\, \ØMQ\: Ø  Connect your code in any language, on any platform. Ø  Carries mes ...

  6. Problem binding to [bigdata-server-01:9000] java.net.BindException: Cannot assign requested address;

    If the port is "0", then the OS is looking for any free port -so the port-in-use and port- ...

  7. 可以执行全文搜索的原因 Elasticsearch full-text search Kibana RESTful API with JSON over HTTP elasticsearch_action es 模糊查询

    https://www.elastic.co/guide/en/elasticsearch/guide/current/getting-started.html Elasticsearch is a ...

  8. Hadoop实战-使用Eclipse开发Hadoop API程序(四)

    一.准备运行所需Jar包 1)avro-1.7.4.jar 2)commons-cli-1.2.jar 3)commons-codec-1.4.jar 4)commons-collections-3. ...

  9. Git检出远程库的分支等相关操作

    来到公司,询问同事后发现系统已经上传到Git远程仓库: 我这里先把远程仓库clone下来: $ git clone http://git.eas****tect.git 发现目录下只有一个READY. ...

  10. 有关 java 不定参数

    不定参数实际为数组参数的一种写法而已,本质上与数组参数完全相同 //1.数组参数函数 public static int sum(int[] values) { } //2.不定参数函数 不定参数只能 ...