您的位置:首页 > 其它

Codeforces Div. 2 #258-B. Sort the Array

2014-07-25 16:13 585 查看
B. Sort the Array

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

Being a programmer, you like arrays a lot. For your birthday, your friends have given you an array
a consisting of n
distinct integers.

Unfortunately, the size of a is too small. You want a bigger array! Your friends agree to give you a bigger array, but only if you are able to answer the following question correctly: is it possible to sort the array
a (in increasing order) by reversing
exactly one segment of a? See definitions of segment and reversing in the notes.

Input
The first line of the input contains an integer n (1 ≤ n ≤ 105) — the size of array
a.

The second line contains n distinct space-separated integers:
a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109).

Output
Print "yes" or "no" (without quotes), depending on the answer.

If your answer is "yes", then also print two space-separated integers denoting start and end (start must not be greater than end) indices of the segment to be reversed. If there are multiple ways of selecting these
indices, print any of them.

Sample test(s)

Input
3
3 2 1


Output
yes
1 3


Input
4
2 1 3 4


Output
yes
1 2


Input
4
3 1 2 4


Output
no


Input
2
1 2


Output
yes
1 1


Note
Sample 1. You can reverse the entire array to get [1, 2, 3], which is sorted.

Sample 3. No segment can be reversed such that the array will be sorted.

Definitions

A segment [l, r] of array
a is the sequence a[l], a[l + 1], ..., a[r].

If you have an array a of size
n and you reverse its segment [l, r], the array will become:

a[1], a[2], ..., a[l - 2], a[l - 1], a[r], a[r - 1], ..., a[l + 1], a[l], a[r + 1], a[r + 2], ..., a[n - 1], a[n].

//AC代码

/*
题意:给你一个长度为n的数列,问你只能颠倒这个数列一个子段的顺序使这个数列变成递增数列
此题可以先找出这个数列下降子序列然后颠倒再判断是否递增,如果有多个下降子序列直接输出no*/
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<map>
#include<cstdlib>
#include<cmath>
const int Max=100001;
using namespace std;
typedef struct Node
{
long long x;
long long num;
}node;
node s[Max],k[Max],t[Max];
bool cmp(node a,node b)
{
return a.x<b.x;
}
int main()
{
long long n,m,i,j,p,sum,st,ed,d1,d2,d3;
while(cin>>n)
{
for(i=1;i<=n;i++)
{
cin>>s[i].x;
s[i].num=i;
}
p=0;
for(i=2;i<n;i++)
{
d1=s[i-1].x;
d2=s[i].x;
d3=s[i+1].x;
if((d2>d1&&d2>d3)||(d2<d1&&d2<d3))
p+=1;
}
if(p>2)
{
//cout<<"000"<<endl;
cout<<"no"<<endl;
}
else if(p>=1)
{
if(p==2)
{
long long u,v;
for(i=2;i<n;i++)
{
d1=s[i-1].x;
d2=s[i].x;
d3=s[i+1].x;
if((d2>d1&&d2>d3))
{
u=i;
}
if((d2<d1&&d2<d3))
{
v=i;
}
}
if(u<v)
{
if(s[u-1].x>s[u+1].x)
{
cout<<"no"<<endl;
continue;
}
}
else if(u>v)
{
long long vm,um;
vm=1;
um=1;
for(i=2;i<=v;i++)
{
if(s[i].x<s[i-1].x)
vm+=1;
}
for(i=u;i<n;i++)
{
if(s[i].x>s[i+1].x)
um+=1;
}
//cout<<v<<" "<<u<<endl;
//cout<<vm<<" "<<um<<endl;
if(vm==v&&(um==(n-v)))
{
cout<<"no"<<endl;
continue;
}

}
}
sort(s+1,s+(n+1),cmp);
sum=0;
for(i=1;i<=n;i++)
{
k[i].x=s[i].num;
k[i].num=i;
t[i].x=k[i].x;
t[i].num=i;
}
sort(k+1,k+(n+1),cmp);
for(i=1;i<=n;i++)
{
if(t[i].x==k[i].num)
sum+=1;
}
if(sum!=n)
{
//cout<<"111"<<endl;
cout<<"no"<<endl;
}
else
{
for(i=1;i<=n;i++)
{
if(t[i].num!=k[i].num)
{
st=i;
break;
}
}
for(j=n;j>=1;j--)
{
if(t[j].num!=k[j].num)
{
ed=j;
break;
}
}
//cout<<"yyyy"<<endl;
cout<<"yes"<<endl;
cout<<st<<" "<<ed<<endl;
}
}
else if(p==0)
{
if(n==1)
{
cout<<"yes"<<endl;
cout<<1<<" "<<1<<endl;
}
else
{
if(s[1].x>s
.x)
{
cout<<"yes"<<endl;
cout<<1<<" "<<n<<endl;
}
else
{
cout<<"yes"<<endl;
cout<<1<<" "<<1<<endl;
}
}
}
}
return 0;
}
/*
3 3 2 14 2 1 3 44 3 1 2 42 1 21
100
6
1 10 70 40 20 100
7
1 20 30 70 100 110 112
8
100 90 70 65 55 43 21 0
4
3 4 1 2
6
0 6 8 2 4 9
5
2 1 0 4 3
*/


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces