CodeForces 451B Sort the Array

Description

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

33 2 1

output

yes1 3

input

42 1 3 4

output

yes1 2

input

43 1 2 4

output

no

input

21 2

output

yes1 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].

题意:给你一个长度不超过10^5的数组,数组中的每个元素互不相同。

问是否能翻转数组的某个区间,使得数组形成一个有序的升序序列。

两次循环,第一次从左到右找出比前一个数小的数字,记录下标

第二次从右到左找出比后一个数大的数字,记录下标

反转两个下标直接的数字

检验得到的数组序列是否为递增

 1 #include<stdio.h>
 2 #include<algorithm>
 3 #define maxn 100005
 4
 5 using namespace std;
 6 int a[maxn];
 7 int main()
 8 {
 9     int n, i, j;
10     while(scanf("%d", &n) != EOF){
11         int x = 1, y = 1;
12         for(i = 1; i <= n; i++)
13             scanf("%d",&a[i]);
14
15         for(i = 1; i < n; i++)
16         if(a[i] > a[i+1]){
17             x = i; break;
18         }
19         for(i = n; i >= 1; i--)
20         if(a[i] < a[i-1]){
21             y = i; break;
22         }
23
24         reverse(a+x, a+y+1);
25         int flag=0;
26         for(i = 1; i < n; i++)
27             if(a[i] > a[i+1]){
28                 flag = 1; break;
29             }
30         if(flag)
31             printf("no\n");
32         else
33             printf("yes\n%d %d\n", x, y);
34     }
35     return 0;
36 }

CodeForces 451B Sort the Array

时间: 2025-01-02 03:40:46

CodeForces 451B Sort the Array的相关文章

【模拟】【codeforces】451B Sort the Array

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=52107 给出一个数列,问截取哪一部分连续子列进行翻转可以使整个数列升序排列, 如果有这样的字串输出yes和截取位置,没有输出no 就是先记录原始位置进行排序,之后一旦发现一个元素排序后更改了位置那它就被翻转过. 完全逆序排列应该是排序后原先的位置依次减1,这样得到被翻转的前后位置. 之后继续扫描,如果还有位置不对的,说明翻转一段不能完成,输出no 1 #include<bi

Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)

题目链接:http://codeforces.com/contest/451/problem/B ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943/ma

Codeforces Round #258 (Div. 2) B. Sort the Array

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,

Codeforces 442C Artem and Array(stack+贪心)

题目连接:Codeforces 442C Artem and Array 题目大意:给出一个数组,每次删除一个数,删除一个数的得分为两边数的最小值,如果左右有一边不存在则算作0分.问最大得分是多少. 解题思路:首先将连续的a,b,c,a > b && c > b的情况将c掉,获得min(a,b)分,这样处理后数组变成一个递増再递减的序列,除了最大和第二大的取不到,其他数字均可以得分. 样例:4 10 2 2 8 #include <cstdio> #include

CodeForces 451B

#include <iostream> #include <algorithm> using namespace std; #define max 100010 int a[max],b[max]; bool cmp(int a,int b){ return a<b; } int main(){ int n; while(cin>>n){ for(int i=0;i<n;i++){ cin>>a[i]; b[i]=a[i]; } sort(a,a

Sort the Array

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,

[2016-04-09][codeforces][660][A][ Co-prime Array]

时间:2016-04-09 22:50:56 星期六 题目编号:[2016-04-09][codeforces][660][A][ Co-prime Array] 题目大意:给定一个数列,问至少需要插入多少个1 1091 109中的任一数字,才能使得相邻两个数字是互质的,输出最少次数和最后的数列 分析:直接扫一遍,相邻元素不互质的,中间插个1, #include<cstdio> #include<vector> using namespace std; const int maxn

Educational Codeforces Round 21 D. Array Division

题目链接:Educational Codeforces Round 21 D. Array Division 题意: 给你n个数,现在你可以改变1<=个数的位置,然后问你是否存在有一个k,使得sum(a[i])(1<=i<=k)==sum(a[j])(k+1<=j<=n) 题解: 分析: 如果需要将一个数移动,无非就是将这个数从第一部分移到第二部分,或者从第二部分移到第一部分. 所以,我们只需要开两个map来记录一下两部分有哪些数. 当两部分的差值/2等于其中一部分的一个数时

(周日赛)Sort the Array

题意:一段数字,逆置其中两个使其递增 DescriptionBeing a programmer, you like arrays a lot. For your birthday, your friends have given you an array a consisting of ndistinct integers. Unfortunately, the size of a is too small. You want a bigger array! Your friends agre