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
Output
Input
Output
Input
Output
Input
Output
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代码
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 Round #258 (Div. 2) B. Sort the Array (模拟)
- Codeforces Round #258 (Div. 2/B)/Codeforces451B_Sort the Array
- codeforces 251 div2 C. Devu and Partitioning of the Array 模拟
- Codeforces Round #258 (Div. 2)——B. Sort the Array(STL-reverse)
- Codeforces Round #258 (Div. 2/B)/Codeforces451B_Sort the Array
- [Codeforces 451B] Sort the Array (实现)
- Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)
- Codeforces Round #258 (Div. 2) . Sort the Array 贪心
- Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)
- Codeforces Div. 2 #258-C. Predict Outcome of the Game
- Codeforces 451(#258 (Div. 2) ) 解题报告
- codeforces --- Round #250 (Div. 2) B. The Child and Set
- B - Sort the Array
- Codeforces Round #251 (Div. 2) C Devu and Partitioning of the Array
- Ruby: how to sort array of string parsing the content
- B题 Sort the Array
- Codeforces Round #312 (Div. 2) B. Amr and The Large Array
- CodeForces 506 Div.1 A. Mr. Kitayuta, the Treasure Hunter
- Codeforces Div. 2 #259-B. Little Pony and Sort by Shift
- Codeforces Round #258 (Div. 2) C. Predict Outcome of the Game