Almost Sorted Array HDU - 5532
Almost Sorted Array HDU - 5532
题意:
如果一个数组的元素“按非递减或非递增顺序排列”,那么称这个数组有序。现在,LYD给了你一个数组a,他让你从中选择并移除仅仅1个元素,请问剩下的元素构成的数组有可能是有序的吗?
题解:
其实就是让你求一个数组的最长上升或者下降下序列,这个序列是否大于等于n-1,这样多余的数的数量才会小于等于1,直接用nlogn的方法求LIS
或者直接模拟写法,我们同时判断一个数列的上升和下降情况,上升时,下降的情况不能出现多于一次,下降情况 时,上升的情况不能多于一次。
两个方法详细看代码
代码:
nlogn求LIS
#include<bits/stdc++.h>
#define debug(a,b) printf("%s = %d\n",a,b);
typedef long long ll;
using namespace std;inline int read(){int s=0,w=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();//s=(s<<3)+(s<<1)+(ch^48);return s*w;
}
const int maxn=1e5+9;
int dp[maxn];
int val[maxn];
int main()
{int t;cin>>t;while(t--){memset(dp,0,sizeof(dp));int n;int cnt1=0,cnt2=0;n=read();int temp;for(int i=1;i<=n;i++)val[i]=read();for(int i=1;i<=n;i++){//求单调上升序列 if(val[i]>=dp[cnt1])dp[++cnt1]=val[i];else {temp=upper_bound(dp+1,dp+1+cnt1,val[i])-dp;dp[temp]=val[i];}}memset(dp,0,sizeof(dp));for(int i=n;i>=1;i--){//求单调递减序列 if(val[i]>=dp[cnt2])dp[++cnt2]=val[i];else {temp=upper_bound(dp+1,dp+1+cnt2,val[i])-dp;dp[temp]=val[i];}}if(cnt1>=n-1||cnt2>=n-1)printf("YES\n");else printf("NO\n");}return 0;
}
模拟做法
#include<stdio.h>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn=100005;
int n,sor[maxn];
bool judge1()//第一次非递减
{int sum=0;sor[0]=-inf;sor[n+1]=inf;for(int i=2;i<=n;i++){if(sor[i]<sor[i-1]){if(sum==1)return 0;sum++;if(sor[i+1]>=sor[i-1]||sor[i]>=sor[i-2])continue;elsereturn 0;}}return 1;
}
bool judge2()//第二次非递增
{int sum=0;sor[0]=inf;sor[n+1]=-inf;for(int i=2;i<=n;i++){if(sor[i]>sor[i-1]){if(sum==1)return 0;sum++;if(sor[i+1]<=sor[i-1]||sor[i]<=sor[i-2])continue;elsereturn 0;}}return 1;
}
int main()
{int t;scanf("%d",&t);while(t--){scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d",&sor[i]);if(judge1()||judge2())printf("YES\n");elseprintf("NO\n");}return 0;
}
Almost Sorted Array HDU - 5532相关推荐
- 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 ...
- Remove Duplicates from Sorted Array II -- LeetCode
原题链接: http://oj.leetcode.com/problems/remove-duplicates-from-sorted-array-ii/ 这道题跟Remove Duplicates ...
- LeetCode: 108. 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】Search in Rotated Sorted Array II(middle)☆
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- leetcode-26. Remove Duplicates from Sorted Array
今天发现一个好东西--leetcode的course ,虽然没有付费的内容会比较少,不过也很不错了. 第一篇的string讲的是两点法(Two-pointer technique),也就是数据结构课本 ...
- [leetcode]Search in Rotated Sorted Array @ Python
原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array/ 题意: Suppose a sorted array is ...
- (LeetCode 153)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 之 Merge Sorted Array(排序)
[问题描写叙述] Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array ...
- 【LeetCode】154. Find Minimum in Rotated Sorted Array II (3 solutions)
Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...
最新文章
- 美国科学院学报:如何在竞争激烈的环境下维持稳定的群体
- c语言中有关指针的选择题,关于C语言指针的几道选择题能选几道就几道吧万分感谢1. 若已定义 爱问知识人...
- django博客项目8:文章详情页
- 技术的本质 2月23日摘抄
- tensorflow 数据格式
- 跟我一起学.NetCore之依赖注入
- codeforces gym-101741 Elevator 动态规划、单调队列
- C语言实战:(1) 整数的二进制表示
- 第二届构建之法论坛预告(草案)
- c语言long a什么意思啊,英文long的中文是什么意思
- Core Animation基础
- 面向过程和面向对象的本质理解
- 小程序分类左右内容联动
- SLF4J错误:NoSuchMethodError解决方案
- Allegro 使用技巧
- 中国地区2012年第二季度 网络安全威胁报告
- 按英语体育计算机创建透视表,2016年职称计算机考试模拟练习题
- Javascript-放大镜实现原理分析
- 安装Ubuntu18.10并安装华硕AC57无线网卡驱动
- react监听回车事件