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. Scala本地安装

    一.下载 https://www.scala-lang.org/download/ 这里我选择Scala2.10.4版本 二.安装 安装比较简单  和jdk类似 点击一路安装: 选择自己的路径 完成 ...

  2. iOS开发笔记1

    1.在堆上模拟函数调用栈 背景: 在看算法书时候, 很多地方提到要谨防递归的栈溢出问题. 分析: 递归调用时候, 有可能出现非常深的函数调用. 对于每次的函数调用, 都需要将函数体内的局部变量保存在栈 ...

  3. Simple Vedio Intercom System

    I. Deployment  / Architecture Block Diagram II. Resources Used sip proxy server + sip user agent 1.  ...

  4. console.log 不起作用

    devtool console.log 突然不起作用了

  5. WiFi密码新攻击破解方法,黑客攻破只需10秒

    近日,中国知名黑客安全组织东方联盟研究人员透露了一种新的WiFi黑客技术,使黑客更容易破解大多数现代路由器的WiFi密码,并且攻破只需要10秒,速度非常快. 方法是利用由流行的密码破解工具Hashca ...

  6. RAC搭建---自己做

    一.本地磁盘是指你本身加上去的磁盘,只能本机使用的.共享磁盘是指可以多台机器同时读取写入.你做RAC就要用到共享存储: 二.ORC分区一般1G*3  数据分区5G*3  ,FRA分区一般5G*3  这 ...

  7. LeetCode--053--最大子序和(java)

    给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和. 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 ...

  8. SQLite和MySQL数据库的差别与应用

    简单来说,SQLITE功能简约.小型化,追求最大磁盘效率:MYSQL功能全面,综合化.追求最大并发效率.假设仅仅是单机上用的,数据量不是非常大.须要方便移植或者须要频繁读/写磁盘文件的话.就用SQLi ...

  9. Telegraf根据配置文件启动(Influxdb的数据收集)

    1.创建一个telegraf.config文件 telegraf -sample-config -input-filter cpu:disk:diskio:net:system:mem -output ...

  10. dede标签大全

    想必很多人对后台不熟悉,并且觉得很难.其实不难,只是你们没有找到合适的方法学习而已!只有找到一个合适的学习方法,不管做什么事情,我想都很容易.学习讲究的是效率,而效率又是由思路决定的.就拿网页制作来说 ...