HDU5532 Almost Sorted Array

Almost Sorted Array

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 9982    Accepted Submission(s): 2329

Problem Description

We are all familiar with sorting algorithms: quick sort, merge sort, heap sort, insertion sort, selection sort, bubble sort, etc. But sometimes it is an overkill to use these algorithms for an almost sorted array.
We say an array is sorted if its elements are in non-decreasing order or non-increasing order. We say an array is almost sorted if we can remove exactly one element from it, and the remaining array is sorted. Now you are given an array a1,a2,…,an, is it almost sorted?

Input

The first line contains an integer T indicating the total number of test cases. Each test case starts with an integer n in one line, then one line with n integers a1,a2,…,an.
1≤T≤2000
2≤n≤105
1≤ai≤105
There are at most 20 test cases with n>1000.

Output

For each test case, please output "`YES`" if it is almost sorted. Otherwise, output "`NO`" (both without quotes).

Sample Input


3
3
2 1 7
3
3 2 1
5
3 1 4 1 5

Sample Output


YES
YES
NO

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = 1e5 + 5;
int b[maxn];
int n;
int Search(int num, int low, int high)
{
    int mid;
    while (low <= high)
    {
        mid = (low + high) / 2;
        if (num >= b[mid])
            low = mid + 1;
        else
            high = mid - 1;
    }
    return low;
}
int fin(int *a)
{
    int len, pos;
    b[1] = a[1];
    len = 1;
    for (int i = 2; i <= n; i++)
    {
        if (a[i] >= b[len])
        {
            len = len + 1;
            b[len] = a[i];
        }
        else
        {
            pos = Search(a[i], 1, len);
            b[pos] = a[i];
        }
    }
    if (len >= n - 1)
        return true;
    else
        return false;
}
int t[maxn], tt[maxn];
int main()
{
    int cas;
    scanf_s("%d", &cas);
    while (cas--)
    {
        scanf_s("%d", &n);
        for (int i = 1; i <= n; i++)
            scanf_s("%d", t + i);
        for (int i = 1; i <= n; i++)
            tt[n + 1 - i] = t[i];
        bool flag = fin(t);
        flag |= fin(tt);
        if (flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
}

原文地址:https://www.cnblogs.com/jianqiao123/p/11241115.html

时间: 2024-08-08 06:04:53

HDU5532 Almost Sorted Array的相关文章

HDU-5532 Almost Sorted Array (LIS)

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

HDU5532 Almost Sorted Array(最长上升子序列 or 瞎搞个做差的数组)

题目链接:点我 题意:给定一个序列,询问是否能删除一个数让它成为非递减或者非递增的序列. 比如说 删除后的序列是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[],i

Remove Duplicates from Sorted Array II

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3. It doesn'

LeetCode --- 88. Merge Sorted Array

题目链接:Merge Sorted Array Given two sorted integer arrays A and B, merge B into A as one sorted array. Note: You may assume that A has enough space (size that is greater or equal to m + n) to hold additional elements from B. The number of elements init

[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 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. 这道题是要求找出一个数组中的最小值,但是这个数组是在有序数组的基础上循环右移了K次. 提示可以用二分

[leetcode]Search in Rotated Sorted Array II

问题描述: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. "Search in Rotated Sorted Array&q

LeetCode: Search in Rotated Sorted Array

LeetCode: Search 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 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, othe

Leetcode | Remove Duplicates from Sorted Array I &amp;&amp; II

Remove Duplicates from Sorted Array I Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memor

LeetCode OJ - Convert Sorted Array/List to Binary Search Tree

虚函数使用的时机 为什么虚函数不总是适用? 1. 虚函数有事会带来很大的消耗: 2. 虚函数不总是提供所需的行为: 3. 当我们不考虑继承当前类时,不必使用虚函数. 必须使用虚函数的情况: 1. 当你想删除一个表面上指向基类对象,实际却是指向派生类对象的指针,就需要虚析构函数. LeetCode OJ - Convert Sorted Array/List to Binary Search Tree,布布扣,bubuko.com LeetCode OJ - Convert Sorted Arra