Almost Sorted Array
http://acm.hdu.edu.cn/contests/contest_showproblem.php?cid=646&pid=1006
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
const int maxn=1e5+;
int ac[maxn],flag,n;
int Increase()
{
int ans,index;
ans=index=;
for(int i=; i<=n; i++)
{
if(ac[i]<ac[i-])
{
ans++;
index=i;
}
}
if(ans==)
flag=;
if(ans==)
{
if(index==n || index== || ac[index-] <= ac[index+] || ac[index-] <= ac[index] )
flag=;///如果这个不满足元素的左边和右边还是非递减的,那么可以去掉该元素,或者该元素前的第二个元素和该元素是非递减的,可以去掉该元素之前的第一个元素
}
if(flag==)
return ;
return ;
}
int Decrease()
{
int ans,index;
ans=index=;
for(int i=; i<=n; i++)
{
if(ac[i]>ac[i-])
{
ans++;
index=i;
}
}
if(ans==)
flag=;
if(ans==)
{
if(index== || index==n || ac[index-]>=ac[index+] || ac[index-]>=ac[index])
flag=;
}
if(flag)
return ;
return ;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
flag=;
scanf("%d",&n);
for(int i=; i<=n; i++)
scanf("%d",&ac[i]);
if(Decrease())//先判断是否是递增序列;找到不满足条件的元素下标,并统计个数,个数<=1时才满足条件
printf("YES\n");
else if(Increase())//如果不是递增在判断其是否是递减序列
printf("YES\n");
else
printf("NO\n");
}
return ;
}
ps : 4 4 1 2 3
4 4 2 7 3
4 4 7 2 3
5 1 2 6 4 5
4 1 1 0 1 1
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?
1≤T≤2000
2≤n≤105
1≤ai≤105
There are at most 20 test cases with n>1000.
3
2 1 7
3
3 2 1
5
3 1 4 1 5
YES
NO
Almost Sorted Array的更多相关文章
- Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- [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 ...
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
- [LeetCode] Merge Sorted Array 混合插入有序数组
Given two sorted integer arrays A and B, merge B into A as one sorted array. Note:You may assume tha ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [LeetCode] Remove Duplicates from Sorted Array II 有序数组中去除重复项之二
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice? For exampl ...
- [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 migh ...
- [LeetCode] Remove Duplicates from Sorted Array 有序数组中去除重复项
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- Remove Duplicates from Sorted Array II
题目简述 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ...
随机推荐
- winrt组件库(包括翻书组件)
http://www.mindscapehq.com/products/metroelements/controls/book-control-for-winrt 点击“down free trial ...
- codeforces 425B Sereja and Table (枚举、位图)
输入n*m的01矩阵.以及k. n,m<=100,k<=10 问修改至多k个,使得矩阵内的各连通块(连着的0或1构成连通块)都是矩形,且不含另外的数字(边界为0(1)的矩形内不含1(0)) ...
- 类型转换辅助工具类TypeCaseHelper
package org.sakaiproject.util; import java.math.BigDecimal; import java.sql.Date; import java.sql.Ti ...
- 【leetcode】Find Minimum in Rotated Sorted Array I & II (middle)
1. 无重复 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 ...
- spring bean中scope="prototype“的作用
今天写代码时,遇到个问题,问题大概如下:在写一个新增模块,当各文本框等输入值后,提交存入数据库,跳到其它页面,当再次进入该新增页面时,上次输入的数据还存在. 经过检查发现是,spring配置文件中,配 ...
- C# webBrowser控件使用
C# webBrowser控件使用心得 最近用到WebBrowser控件,遇到很多问题,也学习了不少新的东西.下面是我在C#下写的关于WebBrowser控件使用的代码. 1.WebBrowser常用 ...
- 使用xib需要记得的小问题
1. 图片 加载 图片上的label 不显示, 最后是因为xib 里位置动了 图片跑到最上层盖住了labe 2. 加载xib 有时候会崩 或加载不出来 先查看xib 是否有多余控件 3. 查看关联 ...
- Android Support兼容包详解
原文:http://www.open-open.com/lib/view/open1427852683115.html
- XAML语言介绍
<Window x:Class="WpfApplication1.Window1" xmlns="http://schemas.microsoft.com/winf ...
- SVN 升级后出现You need to upgrade the working copy first.
今天将svn更新后,出现 svn: The working copy at 'E:\591woospace\kst_fashion_alipay_v1.2.0\src\com\kstapp\wansh ...