Sort the Array
/*
思路:
找到单调下降串的起始位置[l, r]
如果左边 0...l-1中的最大值 > l...r中的最小值 或者
r+1...n中的最小值 < l...r中的最大值 都是不能实现排序的!
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long LL;
int cur, nt;
int cnt;
int num[];
int main(){
int i, n;
int begin, end;
int flag;
int min1, max2;
while(scanf("%d", &n)!=EOF){
cnt=cur=;
flag=;
for(i=; i<=n; ++i){
scanf("%d", &nt);
num[i]=nt;
if(nt>cur)
flag=;
if(!flag && nt<cur){
++cnt;
flag=;
begin=i-;
end=i;
}
if(flag && nt<cur)
end=i;
cur=nt;
}
if(cnt==){
min1=0x3f3f3f3f;
if(end!=n)
min1=num[end+];
max2=-0x3f3f3f3f;
if(begin!=)
max2=num[begin-];
if(max2>num[end] || min1<num[begin])
printf("no\n");
else
printf("yes\n%d %d\n", begin, end);
}
else if(cnt==)
printf("yes\n1 1\n");
else printf("no\n");
}
return ;
}
/*
思路:
将两边单调递增的序列排除(将元素和元素下标一一映射起来,排序之后找到元素和下标映射不同的两个端点),然后中间的那部分就是要翻转的!
最后检查翻转部分的元素和下标是否对应! */
#include<iostream>
#include<algorithm>
#include<map>
#include<cstdio>
#define M 100005
using namespace std;
int n;
int a[M], b[M];
map<int, int>mp;
int main(){
int i;
while(cin>>n){
for(i=; i<n; ++i){
cin>>a[i];
b[i]=a[i];
}
sort(a, a+n);
for(i=; i<n; ++i)
mp[b[i]]=i;
for(i=; i<n; ++i)
a[i]=mp[a[i]];
int L=-, R=-;
for(i=; i<n; ++i)
if(a[i]!=i){
L=i;
break;
}
for(i=n-; i>=; --i)
if(a[i]!=i){
R=i;
break;
}
int ok=;
if(L==- || R==-)
cout<<"yes"<<endl<<"1 1"<<endl;
else{
reverse(a+L, a+R+); for(i=L; i<=R; ++i){
if(a[i]!=i){
ok=;
cout<<"no"<<endl;
break;
}
} if(ok)
cout<<"yes"<<endl<<L+<<" "<<R+<<endl;
}
}
return ;
}
Sort the Array的更多相关文章
- CF451B Sort the Array 水题
Codeforces Round #258 (Div. 2) Sort the Array B. Sort the Array time limit per test 1 second memory ...
- [CareerCup] 11.2 Sort Anagrams Array 异位词数组排序
11.2 Write a method to sort an array of strings so that all the anagrams are next to each other. 这道题 ...
- Codeforces Round #258 (Div. 2) . Sort the Array 贪心
B. Sort the Array 题目连接: http://codeforces.com/contest/451/problem/B Description Being a programmer, ...
- Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)
题目链接:http://codeforces.com/contest/451/problem/B --------------------------------------------------- ...
- Codeforces Round #258 (Div. 2)——B. Sort the Array
B. Sort the Array time limit per test 1 second memory limit per test 256 megabytes input standard in ...
- [LeetCode] 912. Sort an Array 数组排序
Given an array of integers nums, sort the array in ascending order. Example 1: Input: [5,2,3,1] Outp ...
- LeetCode 912. 排序数组(Sort an Array) 43
912. 排序数组 912. Sort an Array 题目描述 每日一算法2019/6/15Day 43LeetCode912. Sort an Array
- Minimum number of swaps required to sort an array
https://www.hackerrank.com/challenges/minimum-swaps-2/problem Minimum Swaps II You are given an unor ...
- 【leetcode】912. Sort an Array
题目如下: Given an array of integers nums, sort the array in ascending order. Example 1: Input: [5,2,3,1 ...
- 【LeetCode】912. Sort an Array 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 库函数排序 桶排序 红黑树排序 归并排序 快速排序 ...
随机推荐
- install skype4.3 in ubuntu15.04
Canonical Partners repository finally adds support for Ubuntu 15.04. Here’s how to enable the reposi ...
- Async/Await FAQ
From time to time, I receive questions from developers which highlight either a need for more inform ...
- 离线安装eclipse的svn插件
原文:http://blog.sina.com.cn/s/blog_8e037f440101ebmz.html 连接不到外网情况下,在eclipse中安装svn插件. 两种方法: 首先下载安装到ecl ...
- QC11客户端安装
win10使用hp qc11 步骤1:安装vcredist_x86,32位 步骤2:安装浏览器客户端 ALMExplorerAddIn,11版本 可能遇到的问题 1. 出现Initialization ...
- MediaWiki安装与配置(Ubuntu 10.4)
实验室准备发布一个网站,本来是准备外包给别人做的,后来自己调研了一下,发现也没有想象的复杂和困难,于是最近一周自己吭哧吭哧地把网站搭好了. 之所以使用Mediawiki,一是考虑到是以实验室发布,不想 ...
- 让Entity Framework启动不再效验__MigrationHistory表
Entity Framework中DbContext首次加载OnModelCreating会检查__MigrationHistory表,作为使用Code Frist编程模式,而实际先有数据库时,这种检 ...
- 树莓派(Raspberry Pi)搭建简单的lamp服务
树莓派(Raspberry Pi)搭建简单的lamp服务: 1. LAMP 的安装 sudo apt-get install apache2 mysql-server mysql-client php ...
- JavaScript工具库之Lodash
你还在为JavaScript中的数据转换.匹配.查找等烦恼吗?一堆看似简单的foreach,却冗长无趣,可仍还在不停的repeat it!也许你已经用上了Underscore.js,不错,你已经进步很 ...
- “你没有权限登录JIRA”的解决办法
为了更好的做部门的项目管理,部门老大让运维部署了一个jira系统.这两天让我试用并摸索如何用. jira默认的用户组有 jira-administrators.jira-developers.jira ...
- 说说设计模式~桥梁模式(Bridge)
返回目录 在软件系统中,某些类型由于自身的逻辑,它具有两个或多个维度的变化,那么如何应对这种“多维度的变化”?如何利用面向对象的技术来使得该类型能够轻松的沿着多个方向进行变化,而又不引入额外的复杂度? ...