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)的更多相关文章

  1. dp(最长升序列)

    http://poj.org/problem?id=2533   题意:给你n(1-1000)个数,求这n个数的最长升序列.   题解:dp[i]表示以第i个数结尾的最长升序列. #include & ...

  2. P1091 合唱队形 DP 最长升序列维护

    题目描述 NN位同学站成一排,音乐老师要请其中的(N-KN−K)位同学出列,使得剩下的KK位同学排成合唱队形. 合唱队形是指这样的一种队形:设K位同学从左到右依次编号为1,2,…,K1,2,…,K,他 ...

  3. 最长升序列 DP

    class Solution: def lengthOfLIS(self,nums): if not nums:return 0 #边界处理 dp = [1 for _ in range(len(nu ...

  4. P1020 导弹拦截 dp 树状数组维护最长升序列

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

  5. (LIS)最长上升序列(DP+二分优化)

    求一个数列的最长上升序列 动态规划法:O(n^2) //DP int LIS(int a[], int n) { int DP[n]; int Cnt=-1; memset(DP, 0, sizeof ...

  6. 洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP

    洛谷 P4093 [HEOI2016/TJOI2016]序列 CDQ分治优化DP 题目描述 佳媛姐姐过生日的时候,她的小伙伴从某宝上买了一个有趣的玩具送给他. 玩具上有一个数列,数列中某些项的值可能会 ...

  7. XHXJ's LIS HDU - 4352 最长递增序列&数位dp

    代码+题解: 1 //题意: 2 //输出在区间[li,ri]中有多少个数是满足这个要求的:这个数的最长递增序列长度等于k 3 //注意是最长序列,可不是子串.子序列是不用紧挨着的 4 // 5 // ...

  8. DP——最长上升子序列(LIS)

    DP——最长上升子序列(LIS) 基本定义: 一个序列中最长的单调递增的子序列,字符子序列指的是字符串中不一定连续但先后顺序一致的n个字符,即可以去掉字符串中的部分字符,但不可改变其前后顺序. LIS ...

  9. 【模板】最长上升子序列(LIS)及其优化 & 洛谷 AT2827 LIS

    最长上升子序列 传送门 题意 对于给定的一个n个数的序列,找到它的一个最长的子序列,并且保证这个子序列是由低到高排序的. 例如,1 6 2 5 4 6 8的最长上升子序列为1 2 4 6 8. 基本思 ...

随机推荐

  1. 案例 element 表单名两端对齐

    >>> .el-form-item label:after { content: ""; display: inline-block; width: 100%; ...

  2. R语言封装函数

    R语言封装函数 原帖见豆瓣:https://www.douban.com/note/279077707/ 一个完整的R函数,需要包括函数名称,函数声明,函数参数以及函数体几部分. 1. 函数名称,即要 ...

  3. 我是如何用python给Thunar写GUI插件的 (pygtk+glade)

    更新:zip乱码的问题可以通过安装patch之后的p7zip-natspec和unzip-natspec解决(archlinuxcn源),而仍使用Engrampa做前端.此文重点在pygtk... 问 ...

  4. ffmpeg windows下编译安装

    安装msys2 更新源使下载速度更快 进入msys64/etc/pacman.d/目录中,分别在三个文件中增加mirrorlist.mingw32Server = http://mirrors.ust ...

  5. 【串线篇】spring boot配置文件加载位置

    springboot 启动会扫描以下位置的application.properties或者application.yml文件作为Spring boot的默认配置文件 (1)–file:./config/ ...

  6. 详解zabbix中文版安装部署

    一.zabbix简介(摘自百度百科) zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案. zabbix能监视各种网络参数,保证服务器系统的安全运营:并提供柔软 ...

  7. Java面向对象(二) 接口、多态和泛型

    一.接口 二.多态 多态是同一个行为具有多个不同表现形式或形态的能力. 2.1 类型转换 转换方式 隐式 向上转型 对于基本数据类型,存储容量低的可自动向存储容量高的类型转换 对于引用变量,子类可被转 ...

  8. 对props的研究

    Vue.component('my-component', { props: { // 基础的类型检查 (`null` 匹配任何类型) propA: Number, // 多个可能的类型 propB: ...

  9. proxyTable-后端代理-跨域请求数据

    config >>> index.js  配置 proxyTable: { '/api': { target:'https://api.jisuapi.com', // 你请求的第三 ...

  10. Java 设计模式之 Command 设计模式

    首先我们先来看 UML 图: 参考资料: java设计模式-Command(命令)模式 - - ITeye技术网站http://men4661273.iteye.com/blog/1633775 JA ...