dp(最长升序列:二分查找时间优化nlogn)
https://vjudge.net/contest/313050#problem/A
题意:给出一序列,可以选择剔除一个数,问该序列是否为非递增或非递减。
思路:找最长非降子序列或最长非增子序列的个数是否与原数列的个数最多 少1个 , 即为yes。。
#include <iostream>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <set>
using namespace std;
int v[509] , w[509] , dp[100009] , a[100009] , dpp[100009], b[100009];
int len1 , len2 ; int halfsearch(int *dp , int r , int x) // 返回数组dp中第一个比a[i]大的数的下标+1
{
int l = 1 , mid ;
while(r >= l)
{
mid = (l + r) >> 1 ;
if(dp[mid] <= x)
{ l = mid + 1 ;
}
else
{
r = mid - 1 ;
}
}
return l ;
} int main()
{
int n ;
scanf("%d" , &n);
while(n--)
{
int m ;
scanf("%d" , &m);
for(int i = 1 ; i <= m ; i++)
{
scanf("%d" , &a[i]);
b[m - i + 1] = a[i]; // 这里巧妙的将求最长非增子序列转为求最长非减子序列。
}
dp[1] = a[1] ;
len1 = 1 ;
for(int i = 2 ; i <= m ; i++)
{
if(a[i] >= dp[len1]) // 如果小于该数组的最大一个就插入后面
dp[++len1] = a[i];
else//将第一个比a[i]大的数替换成a[i];
{
dp[halfsearch(dp , len1 , a[i])] = a[i];
}
}
if(len1 >= m - 1)
printf("YES\n");
else
{
dpp[1] = b[1] ;
len2 = 1 ;
for(int i = 2 ; i <= m ; i++)
{
if(b[i] >= dpp[len2])
dpp[++len2] = b[i];
else
{
dpp[halfsearch(dpp , len2 , b[i])] = b[i];
}
} if(len2 >= m - 1)
printf("YES\n");
else
printf("NO\n"); } } return 0;
}
因为是不增不减,所有相等符合条件,lower_bound找到第一个大于等于某数,upper——bound找到第一个大于某数
所以这里只能用upper_bound。
//#include <bits/stdc++.h>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
#define mod 1000000007
#define PI acos(-1)
using namespace std;
typedef long long ll ;
int b[100009];
int a[100009]; int c[100009];
int main()
{
int t;
scanf("%d" , &t);
while(t--)
{
int n ;
scanf("%d" , &n);
int flag1 = 0 , flag2 = 0 ;
for(int i = 1 ; i <= n ; i++)
{
scanf("%d" , &a[i]);
b[n - i + 1] = a[i];
}
int l = 0 ;
c[l] = a[1];
for(int i = 2 ; i <= n ; i++)
{
if(c[l] <= a[i])
{
c[++l] = a[i];
}
else
{
int index = upper_bound(c , c+l+1 , a[i]) - c;
c[index] = a[i];
}
}
if(l + 1 >= n - 1)
{
flag1 = 1 ;
} l = 0 ;
c[l] = b[1];
for(int i = 2 ; i <= n ; i++)
{
if(c[l] <= b[i])
{
c[++l] = b[i];
}
else
{
int index1 = upper_bound(c , c + l + 1 , b[i]) - c;
c[index1] = b[i];
}
}
if(l + 1 >= n - 1)
{
flag2 = 1 ;
}
if(flag1 || flag2)
{
cout << "YES" << endl;
}
else
{
cout << "NO" << endl ;
}
} return 0;
}
dp(最长升序列:二分查找时间优化nlogn)的更多相关文章
- dp(最长升序列)
http://poj.org/problem?id=2533 题意:给你n(1-1000)个数,求这n个数的最长升序列. 题解:dp[i]表示以第i个数结尾的最长升序列. #include & ...
- P1091 合唱队形 DP 最长升序列维护
题目描述 NN位同学站成一排,音乐老师要请其中的(N-KN−K)位同学出列,使得剩下的KK位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2,…,K1,2,…,K,他 ...
- 最长升序列 DP
class Solution: def lengthOfLIS(self,nums): if not nums:return 0 #边界处理 dp = [1 for _ in range(len(nu ...
- P1020 导弹拦截 dp 树状数组维护最长升序列
题目描述 某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能高于前一发的高度.某天,雷达捕捉到敌国的导弹 ...
- (LIS)最长上升序列(DP+二分优化)
求一个数列的最长上升序列 动态规划法:O(n^2) //DP int LIS(int a[], int n) { int DP[n]; int Cnt=-1; memset(DP, 0, sizeof ...
- 洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP
洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP 题目描述 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他. 玩具上有一个数列,数列中某些项的值可能会 ...
- XHXJ's LIS HDU - 4352 最长递增序列&数位dp
代码+题解: 1 //题意: 2 //输出在区间[li,ri]中有多少个数是满足这个要求的:这个数的最长递增序列长度等于k 3 //注意是最长序列,可不是子串.子序列是不用紧挨着的 4 // 5 // ...
- DP——最长上升子序列(LIS)
DP——最长上升子序列(LIS) 基本定义: 一个序列中最长的单调递增的子序列,字符子序列指的是字符串中不一定连续但先后顺序一致的n个字符,即可以去掉字符串中的部分字符,但不可改变其前后顺序. LIS ...
- 【模板】最长上升子序列(LIS)及其优化 & 洛谷 AT2827 LIS
最长上升子序列 传送门 题意 对于给定的一个n个数的序列,找到它的一个最长的子序列,并且保证这个子序列是由低到高排序的. 例如,1 6 2 5 4 6 8的最长上升子序列为1 2 4 6 8. 基本思 ...
随机推荐
- 解决 vue-cli构建项目自动打开浏览器问题
1.打开项目下的config/index.js 2.找到module.exports的 dev下的 autoOpenBrowser ,将 false 改成 true 3.控制台输入:npm run d ...
- 【记录】form-data与x-www-form-urlencoded的区别
1)application/x-www-form-urlencoded 这应该是最常见的 POST 提交数据的方式了.浏览器的原生 <form> 表单,如果不设置 enctype 属性,那 ...
- Linux系统启动过程浅析
经过老师的讲解以及查阅资料后,现对Linux系统启动做以浅析,仅是个人理解. 主要的步骤有以下几步: 第一步:Power On.用户按下电源开关的那一瞬间,叫Power On阶段 .在这个阶段,BIO ...
- 07.SUSE Linux 系统本地yum源配置
SUSE Linux 系统 1.新建本地源存储目录root@suse:mkdir /mnt/SUSE_LOCAL_SOURCE 2.创建zypper本地源root@suse:zypper ar fil ...
- 14DBCP连接池
实际开发中“获得连接”或“释放资源”是非常消耗系统资源的两个过程,为了解决此类性能问题,通常情况我们采用连接池技术,来共享连接Connection.这样我们就不需要每次都创建连接.释放连接了,这些操作 ...
- Linux shell中自动完成登录
在写shell脚本时,需要登录到不同的服务器上执行相关命令,在未建立信任之前如何批量操作. 1.ssh 首次登录服务器时会提示RSA key fingerprint输入yes/no,可以通过下面的方法 ...
- Uboot命令U_BOOT_CMD分析
其中U_BOOT_CMD命令格式如下: U_BOOT_CMD(name,maxargs,repeatable,command,"usage","help") 各 ...
- 给mongodb设置密码
内容来自:https://segmentfault.com/a/1190000011554055 mongodb安装后是无需密码 Mongodb安装后自身是没有密码的,用户连接只需填写id地址,端口号 ...
- 在vue项目中,解决如何在element表格中循环出图片列!
效果图: 1,vue项目环境 2,引入element-ui组件 3,制作表格 此处省去制作循环表格数据那步,想看的可以找回我的博客:element中的表格处理:循环出表格数据 今天想在表格出循环出一列 ...
- MySQL基础篇——安装、管理
MySQL 安装 所有平台的 MySQL 下载地址为https://dev.mysql.com/downloads/mysql/ .挑选你需要的 MySQL Community Server 版本及对 ...