题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5532

题意:问一个含有n个数的序列,删除一个数后是否有序(递增或递减都可以)

我们只要求一下最长上升子序列或者最长下降子序列是否大于等于n-1即可;

时间复杂度n*log(n);

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
#include<set>
using namespace std;
#define met(a, b) memset(a, b, sizeof(a))
#define N 100005
#define INF 0x3f3f3f3f
typedef long long LL; int n, a[N], dp[N]; int solve1()
{
for(int i=; i<=n; i++)
dp[i] = INF;
int ans = ;
for(int i=; i<=n; i++)
{
int pos = upper_bound(dp, dp+n, a[i]) - dp;
dp[pos] = min(dp[pos], a[i]);
ans = max(ans, pos);
}
return ans+;
}
int solve2()
{
for(int i=; i<=n; i++)
dp[i] = INF;
int ans = ;
for(int i=n; i>=; i--)
{
int pos = upper_bound(dp, dp+n, a[i]) - dp;
dp[pos] = min(dp[pos], a[i]);
ans = max(ans, pos);
}
return ans+;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
scanf("%d", &n);
for(int i=; i<=n; i++)
scanf("%d", &a[i]);
int ans1 = solve1();
int ans2 = solve2();
if(ans1 >= n- || ans2 >= n-)
puts("YES");
else
puts("NO");
}
return ;
}

Almost Sorted Array---hdu5532(简单dp)的更多相关文章

  1. 简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现。

    简单的算法题, Find Minimum in Rotated Sorted Array 的Python实现. 题目: Suppose a sorted array is rotated at som ...

  2. HDU-5532//2015ACM/ICPC亚洲区长春站-重现赛-F - Almost Sorted Array/,哈哈,水一把区域赛的题~~

    F - Almost Sorted Array Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  3. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

  4. [LeetCode] Find Minimum in Rotated Sorted Array 寻找旋转有序数组的最小值

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  5. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  6. [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

  7. [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  8. 【leetcode】Remove Duplicates from Sorted Array

    题目描述: Given a sorted array, remove the duplicates in place such that each element appear only once a ...

  9. Java for LeetCode 026 Remove Duplicates from Sorted Array

    Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...

随机推荐

  1. BZOJ3673 : 可持久化并查集

    题目没有强制在线! 考虑离线做法. 把操作建立成一棵树的结构,然后按照欧拉序遍历,每次转移要么是一次合并操作,要么是一次撤销合并操作,可以看成是分离操作. 用LCT维护集合,合并就是加边,分离就是删边 ...

  2. (转)Linq学习笔记

    写在前面 最近在看Linq,在博客园看到这篇文章,写的通俗易懂,转来和大家一起做个分享.原文地址http://www.cnblogs.com/goscan/archive/2011/05/05/Lin ...

  3. Centos6.4 yum安装MariaDB5.5

    vi /etc/yum.repos.d/MariaDB.repo 加入下面内容 [mariabd]name=MariaDBbaseurl=http://yum.mariadb.org/5.5.34/c ...

  4. 对于String对象,可以使用"="赋值,也可以使用"new"关键字赋值,两种方式有什么区别?

    当你看见这个标题的时候,你可能会下意识的去想一下,这两种方式到底有什么样的区别呢? 且看下面的demo,自然便区分开了 /** * */ package com.b510.test; /** * Pr ...

  5. JSONP - 跨域AJAX

    基础概念 在进入本文正题之前,我们需要先了解一些基础概念(如果你已经对这些基础有所了解,可跳过此段落). 同源策略和跨域概念 同源策略(Same-orgin policy)限制了一个源(orgin)中 ...

  6. Html - Bootstrap Panel面板

    http://v3.bootcss.com/components/#panels Bootstrap Panel面板 <div class="panel panel-default&q ...

  7. [转]OBOUT ASP.NET HTML Editor - Insert HTML

    本文转自:http://www.obout.com/editor_new/sample_InsertHTML.aspx Example demonstrates how to access HTML ...

  8. IM客户端Socks 5代理协议应用

    之前编写的一个基于openfire服务器的即时通讯软件,因为部署环境需要,需要增加代理登录通信的实现.整理了一下相关代理的知识分享一下. 一个基于TCP协议的客户端希望与一个只能通过特定网络节点才可以 ...

  9. 【转载】jQuery插件开发精品教程,让你的jQuery提升一个台阶

    要说jQuery 最成功的地方,我认为是它的可扩展性吸引了众多开发者为其开发插件,从而建立起了一个生态系统.这好比大公司们争相做平台一样,得平台者得天下.苹果,微软,谷歌等巨头,都有各自的平台及生态圈 ...

  10. 《Java核心技术卷二》笔记(二)文件操作和内存映射文件

    文件操作 上一篇已经总结了流操作,其中也包括文件的读写.文件系统除了读写以为还有很多其他的操作,如复制.移动.删除.目录浏览.属性读写等.在Java7之前,一直使用File类用于文件的操作.Java7 ...