题目链接:点我

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

   比如说 删除后的序列是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. 机器学习进阶-图像金字塔与轮廓检测-轮廓检测 1.cv2.cvtColor(图像颜色转换) 2.cv2.findContours(找出图像的轮廓) 3.cv2.drawContours(画出图像轮廓) 4.cv2.contourArea(轮廓面积) 5.cv2.arcLength(轮廓周长) 6.cv2.aprroxPloyDP(获得轮廓近似) 7.cv2.boudingrect(外接圆)..

    1. cv2.cvtcolor(img, cv2.COLOR_BGR2GRAY) # 将彩色图转换为灰度图 参数说明: img表示输入的图片, cv2.COLOR_BGR2GRAY表示颜色的变换形式 ...

  2. 机器学习入门-数值特征-数据四分位特征 1.quantile(用于求给定分数位的数值) 2.plt.axvline(用于画出竖线) 3.pd.pcut(对特征进行分位数切分,生成新的特征)

    函数说明: 1.  .quantile(cut_list) 对DataFrame类型直接使用,用于求出给定列表中分数的数值,这里用来求出4分位出的数值 2.  plt.axvline()  # 用于画 ...

  3. electron安装到第一个实例

    1.node.js下载,然后安装.下载地址:链接:http://pan.baidu.com/s/1o7TONhS 密码:fosa 2.cmd下输入:npm install electron-prebu ...

  4. redis异常和注意点

    目录: 1. 修改配置不起作用 2.Connection reset by peer: socket write error 3. redis-cli 查看中文乱码 1. 修改配置不起效果 我们修改了 ...

  5. 使用dig或nslookup指定dns服务器查询域名解析

    一般来说linux下查询域名解析有两种选择,nslookup或者dig,而在使用上我觉得dig更加方便顺手.如果是在linux下的话,只要装上dnsutils这个包就可以使用dig命令, 安装bind ...

  6. Java IO流学习总结五:转换流-InputStreamReader、OutputStreamWriter

    类的继承关系 Reader |__ BufferedReader.StringReader.InputStreamReader |__ FileReader Writer |__ BufferedWr ...

  7. Unity 平台依赖编译

    位置:unity文档-Manual-Scripting-Platform dependent compilation Property: Function: UNITY_EDITOR #define ...

  8. suse 关于使用 /etc/init.d/boot.local的问题

    最近看了一个问题,有同事在 suse环境下的/etc/init.d/boot.local 中,增加了一行脚本. 该脚本的简单大意如下: #!/bin/bash ] do ] then echo &qu ...

  9. Hibernate学习笔记3.3(Hibernate组建映射2)

    多对多 相当于一个老师可以教多个学生,一个学生也可以有多个老师 数据表中都是再设计一个表寸相关的id 1.多对多单向 1annotation Student.java package com.bjsx ...

  10. Hosts

    Hosts "C:\Windows\System32\drivers\etc\hosts" [最新]2018 hosts 持续更新[更新于:2018-11-13] 本页面WordP ...