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 ...
随机推荐
- CODE VS1008选数
#include<cstdlib> #include<cstdio> #include<iostream> #include<cmath> #inclu ...
- C#string类;math类;datetime类
String类: .Length字符的长度 .Trim()去掉开头以及结尾的空格 .TrimStart()去掉字符串开头的空格 .TrimEnd()去掉字符串后面的空格 .ToUpper()全 ...
- struts2拦截器+监听器 .
一.拦截器是怎么实现: 实际上它是用Java中的动态代理来实现的 二.拦截器在Struts2中的应用 对于Struts2框架而言,正是大量的内置拦截器完成了大部分操作.像params拦截器将http请 ...
- Hibernate基本CRUD
1 hibernate 框架介绍 冬眠:开发的代码基本不变. 1.1回顾jdbc Java提供的标准的数据访问的API 作用:完成应用程序java程序中的数据和db中的数据进行交换. 工作过程: A ...
- linker command failed with exit code 1 (use -v to see invocation)
library not found for -|AFNetworking 错误内容如图所示: 解决方法:1. 如果没有安装pod,则安装pod,并导入项目AFNetworking参考:http://w ...
- Mybatis 学习笔记1
---恢复内容开始--- 什么是 MyBatis ? MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获 ...
- August 26th 2016 Week 35th Friday
It always seems impossible until it's done. 在事情未完成之前,一切都看似不可能. When I was young, once I had to lift ...
- July 1st, Week 27th Friday, 2016
It does not do to dwell on dreams, and forget to live. 不要生活在梦里,不要沉醉于空想而疏忽了生活. Stand straightly, and ...
- 添加thrust的库后出错
在添加thrust库中的host_vector.h等头文件时 C:\NVIDIA\cudatoolkit\include\thrust\detail\config中的debug.h一直出问题,因此注释 ...
- DO语句与SELECT语句,HANDLER语句
DO语句,只执行语句不返回结果:SELECT 既执行语句也返回结果. HANDLER的效率会更好. HANDLER 语句比SELECT 语句更快: 1,HANDLER只需OPEN一次,能重用,不须每次 ...