【HDOJ5532】Almost Sorted Array(签到)
题意:给定一个n个数的数列,问删掉一个数之后剩余部分是否可以单调不增或单调不减
n<=1e5,a[i]<=1e5
思路:预处理一下前后缀是否合法
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
#define N 210000
#define M 1100
#define MOD 2147493647
#define eps 1e-8
#define pi acos(-1) int a[N],b[N],c[N],k; int main()
{
//freopen("F.in","r",stdin);
//freopen("F.out","w",stdout);
int cas;
scanf("%d",&cas);
for(int v=;v<=cas;v++)
{
k=;
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=c[i]=;
}
b[]=;
for(int i=;i<=n;i++)
if(b[i-]==&&a[i]>=a[i-]) b[i]=;
else c[i]=;
c[n]=;
for(int i=n-;i>=;i--)
if(c[i+]==&&a[i]<=a[i+]) c[i]=;
else c[i]=;
int flag=;
//for(int i=1;i<=n;i++) printf("%d ",b[i]);
// printf("\n");
//for(int i=1;i<=n;i++) printf("%d ",c[i]);
// printf("\n");
for(int i=;i<=n;i++)
{
if(i>&&b[i-]==) continue;
if(i<n&&c[i+]==) continue;
if(i>&&i<n&&a[i-]>a[i+]) continue;
flag=; k=i;
break;
}
// printf("%d\n",k);
for(int i=;i<=n;i++) b[i]=c[i]=;
b[]=;
for(int i=;i<=n;i++)
if(b[i-]==&&a[i]<=a[i-]) b[i]=;
else break;
c[n]=;
for(int i=n-;i>=;i--)
if(c[i+]==&&a[i]>=a[i+]) c[i]=;
else break;
for(int i=;i<=n;i++)
{
if(i>&&b[i-]==) continue;
if(i<n&&c[i+]==) continue;
if(i>&&i<n&&a[i-]<a[i+]) continue;
flag=; k=i;
break;
}
// printf("%d\n",k);
if(flag) printf("YES\n");
else printf("NO\n");
}
return ;
}
【HDOJ5532】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 ...
随机推荐
- cf550D. Regular Bridge(构造)
题意 给出一个$k$,构造一个无向图,使得每个点的度数为$k$,且存在一个桥 Sol 神仙题 一篇写的非常好的博客:http://www.cnblogs.com/mangoyang/p/9302269 ...
- 【PHP】常用的PHP正则表达式收集整理
匹配中文字符的正则表达式: [\u4e00-\u9fa5]评注:匹配中文还真是个头疼的事,有了这个表达式就好办了 匹配双字节字符(包括汉字在内):[^\x00-\xff]评注:可以用来计算字符串的长度 ...
- Lake Counting(dfs)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- 迭代器Iterator与语法糖for-each
一.为什么需要迭代器 设计模式迭代器 迭代器作用于集合,是用来遍历集合元素的对象.迭代器迭代器不是Java独有的,大部分高级语言都提供了迭代器来遍历集合.实际上,迭代器是一种设计模式: 迭代器模式提供 ...
- 2612. [FHZOI 2017]被窃的项链
2612. [FHZOI 2017]被窃的项链 ★★★ 输入文件:stolen_necklace.in 输出文件:stolen_necklace.out 简单对比时间限制:1 s 内存 ...
- Nodejs-内置核心模块&npm包管理工具
1.核心模块的意义 如果只是在服务器运行JavaScript代码,其实意义不大(浏览器就可以解决)因为无法实现功能(读写文件,访问网络) Node的用处在于本身还提供了一系列的功能模块,用于与操作系统 ...
- leetcode 【 Subsets II 】python 实现
题目: Given a collection of integers that might contain duplicates, S, return all possible subsets. No ...
- MFC深入浅出读书笔记第二部分2
第七章 MFC骨干程序 所谓骨干程序就是指有AppWizard生成的MFC程序.如下图的层次关系是程序中常用的几个类,一定要熟记于心. 1 Document/View应用程序 CDocument存放 ...
- Android 使用intent传递返回值:startActivityForResult()与onActivityResult()与setResult()参数分析,activity带参数的返回
在一个父Activity通过intent跳转至多个不同子Activity上去,当子模块的代码执行完毕后再次返回父页面,将子activity中得到的数据显示在主界面/完成的数据交给父Activity处理 ...
- Leetcode 655.输出二叉树
输出二叉树 在一个 m*n 的二维字符串数组中输出二叉树,并遵守以下规则: 行数 m 应当等于给定二叉树的高度. 列数 n 应当总是奇数. 根节点的值(以字符串格式给出)应当放在可放置的第一行正中间. ...