题目链接:点我

题意:给定一个序列,询问是否能删除一个数让它成为非递减或者非递增的序列。

   比如说 删除后的序列是1 3 3 5 或者5 3 3 1 或者1 3 5 或者5 3 1 都可以。只要满足删掉某个数,构成非递减或者非递增,就输出YES,如果不能就输出NO

正解(LIS求最长上升子序列):

正着来一遍,反着来一遍 注意要用upper_bound即可:

代码:

#include<bits/stdc++.h>
using namespace std;
int Maxlen(int a[],int n){
int b[];
memset(b,,sizeof(b));
int len = ;
for(int i = ; i < n; i ++)
{
if(len == ||a[i] >= b[len - ])
{
b[len] = a[i];
len ++;
}
else
{
int p = upper_bound(b,b + len,a[i]) - b;
b[p] = a[i];
}
}
return len;
}
int main(){
int t,n;
for(scanf("%d",&t);t--;){
scanf("%d",&n);
int a[],c[];
for(int i = ; i < n ; i++){
scanf("%d",&a[i]);
c[n-i-] = a[i];
}
int len = Maxlen(a,n);
int len1 = Maxlen(c,n);
if(len >= n- || len1 >= n-)puts("YES");
else puts("NO");
}
}

如果想瞎搞也行。。。

因为删除一个嘛,先证明删除一个能不能是非递减的(非递增的把序列倒过来搞一次就行了)

首先,对一个序列前后两个数做差

比如说序列

3 1 4 1 5 做差后(即1-3,4-1,1-4,5-1)是 -2,3,-3,4。发现有2个负数,那就不行。

如果序列是 3 1 1 5。 做差后是-2,0,4。发现有一个负数,是在头部,可以删掉

如果序列是5 6 3 ,7 7,做差后是 1,-3,4,0。发现有一个负数,而且可以跟左右两边的某个数相加变成正数,那么这个3就可以删掉。

如果序列是1 2 3 4,做差后是1,1,1,1 没有负数,本身就可以是非递减。

能看得出来:

做差后的序列:如果有2个及以上负数,那它肯定不可能是非递减。

        如果有一个负数,它在头或者尾,或者在中间而且可以跟左右两边任意一个数相加是正数,即可以是非递减

        如果没有负数,已经是非递减

时间复杂度是O(N),额外需要O(N)的空间存做差后的数组

非递增的话就把数组倒一下再来一次就行了。

代码(很乱):

#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <bitset> using namespace std;
#define LL long long
#define pb push_back
#define mk make_pair
#define pill pair<int, int>
#define mst(a, b) memset(a, b, sizeof a)
#define REP(i, x, n) for(int i = x; i <= n; ++i)
int main(){
int t,n;
for(scanf("%d",&t);t--;){
scanf("%d",&n);
int a[],b[],c[];
for(int i = ; i < n ; i++){
scanf("%d",&a[i]);
c[n-i-] = a[i];
}
int f1 = ,ard = -;
int s1 = ,s2 = ;
for(int i = ; i < n ; i++){
b[i] = a[i] - a[i-];
if(b[i] < ){
f1++;
ard = i;
}
if(f1 == ){
break;
}
}
if(f1 == ){
s1 = ;
}
if(f1 == ){
if(ard == || ard == n-){
s1 = ;
}
else if(b[ard] + b[ard-] >= || b[ard] + b[ard+] >= ){
s1 = ;
}
}
f1 = ;
ard = -;
//for(int i = 0 ; i < n ; i++) printf("%d ",c[i]);
for(int i = ; i < n ; i++){
b[i] = c[i] - c[i-];
if(b[i] < ){
f1++;
ard = i;
}
if(f1 == ){
break;
}
}
if(f1 == ){
s2 = ;
}
if(f1 == ){
if(ard == || ard == n-){
s2 = ;
}
else if(b[ard] + b[ard-] >= || b[ard] + b[ard+] >= ){
s2 = ;
}
}
s1||s2?puts("YES"):puts("NO");//s1,s2分别代表在非递减和非递增可不可以满足条件
}
}

HDU5532 Almost Sorted Array(最长上升子序列 or 瞎搞个做差的数组)的更多相关文章

  1. HDU-5532 Almost Sorted Array (LIS)

    题目大意:给一个n个数的序列,问这个序列删掉一个数后是否有序. 题目分析:找最长上升子序列和最长下降子序列,只要有一个的长度不小于n-1即可. 代码如下: # include<iostream& ...

  2. 算法 - 求一个数组的最长递减子序列(C++)

    //************************************************************************************************** ...

  3. Remove Duplicates From Sorted Array

    Remove Duplicates from Sorted Array LeetCode OJ Given a sorted array, remove the duplicates in place ...

  4. [LeetCode]题解(python):033-Search in Rotated Sorted Array

    题目来源 https://leetcode.com/problems/search-in-rotated-sorted-array/ Suppose a sorted array is rotated ...

  5. hunnu 11313 无重复元素序列的最长公共子序列转化成最长递增子序列 求法及证明

    题目:http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11313 湖师大的比赛,见我的另一篇水题题解,这里要说的 ...

  6. ACMDP之最长公共子序列长度—HDU1159

    Common Subsequence Problem Description A subsequence of a given sequence is the given sequence with ...

  7. 最长公共子序列(LCS)问题 Longest Common Subsequence 与最长公告字串 longest common substr

    问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y=“y0,y1,…,yk ...

  8. 最长公共子序列(LCS)、最长递增子序列(LIS)、最长递增公共子序列(LICS)

    最长公共子序列(LCS) [问题] 求两字符序列的最长公共字符子序列 问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字 ...

  9. 求解最长递增子序列(LIS) | 动态规划(DP)+ 二分法

    1.题目描述     给定数组arr,返回arr的最长递增子序列. 2.举例     arr={2,1,5,3,6,4,8,9,7},返回的最长递增子序列为{1,3,4,8,9}. 3.解答      ...

随机推荐

  1. java中的byte

    8 bit (位) = 1 Byte (字节) java中的byte就是Byte 1024 Byte = 1KB 1024 KB = 1MB 1:“字节”是byte,“位”是bit : 2: 1 by ...

  2. png 2 icon

    http://www.easyicon.net/covert/ 这个网页可以转换png图片为icon格式

  3. python之 pendulum讲解

    一,下载地址:https://pypi.python.org/pypi/pendulum 二,pendulum的一大优势是内嵌式取代Python的datetime类,可以轻易地将它整合进已有代码,并且 ...

  4. webpack 引用vconsole

    1.npm install -vconsole --save-dev 2.npm install -vconsele-webpack-plugin --save-dev 3.webpack.base. ...

  5. ubuntu 安装oracle客户端

    from: http://webikon.com/cases/installing-oracle-sql-plus-client-on-ubuntu Installing Oracle SQL*Plu ...

  6. 4.ClassLink - 一种新型的VPC 经典网络的连接方式

    阿里云CLassLink文档地址:https://help.aliyun.com/document_detail/65412.html?spm=a2c4g.11186623.2.9.41a25a07F ...

  7. How to Pronounce the Word ARE

    How to Pronounce the Word ARE Share Tweet Share Tagged With: ARE Reduction Study the ARE reduction. ...

  8. 一秒去除Win7快捷方式箭头

    我相信有无数的小盆友跟我一样很讨厌Win7快捷方式图标上的箭头,实在太丑陋了,尤其是带有强迫症滴.现在介绍去除箭头的方式. 1. 打开编辑器,将以下代码粘贴进去,然后保存为.bat后缀的文件,然后双击 ...

  9. MyBatis动态SQL中trim标签的使用

    My Batis 官方文档 对 动态SQL中使用trim标签的场景及效果介绍比较少. 事实上trim标签有点类似于replace效果. trim 属性 prefix:前缀覆盖并增加其内容 suffix ...

  10. python 装饰器的缺点以及解决方法

    1.python装饰器的缺点 装饰器可以允许我们在不改变函数或犯方法的调用方式的情况下,添加额外的功能; 如下所示,我们要在中的方法之前增加装饰器check_is_admin,用来判断执行类的方法的用 ...