acm集训训练赛B题【排序+模拟】

一、原题

Description

Being 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 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 Input

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

Hint

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

二 、题目源程序(一)

#include<bits/stdc++.h>
#define maxn 100005
typedef long long LL;
using namespace std;
int a[maxn];
int main()
{
    int n,x=1,y=1;
    cin>>n;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<n;i++){
        if(a[i]>a[i+1]){
            x=i;break;
        }
    }
    for(int i=n;i>=1;i--){
        if(a[i]<a[i-1]){
            y=i;
            break;
        }
    }
    for(int i=0;i<y-x;i++)
    {
        int t;
        t=a[x+i];
        a[x+i]=a[y-i];
        a[y-i]=t;
    }
    int flag=0;
    for(int i=1;i<n;i++){
        if(a[i]>a[i+1]){
            flag=1;
            break;
        }
    }
    if(flag) cout<<"no"<<endl;
    else{
        cout<<"yes"<<endl<<x<<" "<<y<<endl;
    }
    return 0;
}

题目源程序(二)

#include<bits/stdc++.h>
#define maxn 100005
typedef long long LL;
using namespace std;
int a[maxn];
int main()
{
    int n,x=1,y=1;
    cin>>n;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<n;i++){
        if(a[i]>a[i+1]){
            x=i;break;
        }
    }
    for(int i=n;i>=1;i--){
        if(a[i]<a[i-1]){
            y=i;
            break;
        }
    }
    reverse(a+x,a+y+1);
    int flag=0;
    for(int i=1;i<n;i++){
        if(a[i]>a[i+1]){
            flag=1;
            break;
        }
    }
    if(flag) cout<<"no"<<endl;
    else{
        cout<<"yes"<<endl<<x<<" "<<y<<endl;
    }
    return 0;
}

三、解题思路

题意分析:

给定一个序列,能否通过逆序它的一个子序列使得整个序列单调递增,如果能,输出需要逆序的下标,否则输出no

思路

1、从左到右遍历,第一次出现比前一个数小的下标;

2、然后从右往左遍历,第一次出现比后一个数小的下标;

3、将这段区间逆序,然后判断整个序列是否单调递增。

四、心得体会

(1)reverse函数的运用,包含于头文件<algorithm>中。

(2)这是一道比较简单的排序与模拟题。

acm集训训练赛B题【排序+模拟】

时间: 2024-10-26 07:40:25

acm集训训练赛B题【排序+模拟】的相关文章

acm集训训练赛A题【签到题】

一.题目 Description After winning gold and silver in IOI 2014, Akshat and Malvika want to have some fun. Now they are playing a game on a grid made of nhorizontal and m vertical sticks. An intersection point is any point on the grid which is formed by t

acm集训训练赛(二)D题【并查集】

一.题目 Description There is a town with N citizens. It is known that some pairs of people are friends. According to the famous saying that ?The friends of my friends are my friends, too? it follows that if A and B are friends and B and C are friends th

Sdut 2108 Alice and Bob(数学题)(山东省ACM第四届省赛D题)

题目地址:sdut 2608 Alice and Bob Alice and Bob Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Alice and Bob like playing games very much.Today, they introduce a new game. There is a polynomial like this: (a0*x^(2^0)+1) * (a1 * x^(2^1)+1)*....

最后一周训练赛第一题

A - Problem A Time Limit:2000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice SPOJ QUEST5 Description To get to the treasure, Jones must complete one more task. He comes across a table, where there are a number of wooden

[暑假集训]区域赛套题集

2014-07-03 [浙江第11届省赛]ZOJ 3785 What day is that day?  (打表找循环节) [暑假集训]区域赛套题集

2016暑假集训训练2 I题 Doing Homework again

傻逼贪心题. 先按照扣分从大到小排序,分数相同则按照截止日期从小到大排序.. 然后按顺序,从截止日期开始往前找没有占用掉的时间. 如果找不到了,则加到罚分里面. # include <stdio.h> # include <stdlib.h> # include <string.h> int h[1005][2], n, visit[1005]; int comp(const void * a, const void * b) { return *(((int*)b)+

2016 acm香港网络赛 C题. Classrooms(贪心)

原题网址:https://open.kattis.com/problems/classrooms Classrooms The new semester is about to begin, and finding classrooms for orientation activities is always a headache. There are k classrooms on campus and n proposed activities that need to be assigne

scu oj 4445 Right turn 2015年四川省赛J题(模拟题)

一般的模拟题.对于出现过的每一个x建立一个set 集合,对于y也如此.然后查找要走到哪个点即可,主要要对状态记录,判断是否无线循环,否者出现无线循环的情况,就tle了. #include<stdio.h> #include<string.h> #include<iostream> #include<string> #include<queue> #include<cmath> #include<map> #include&

2016暑假集训训练2 C题 食物链

带权并查集傻逼题 用拆点方法草过. 拆点代码: # include <stdio.h> # include <string.h> int set[150005], n, k, d; int find(int x) { int s, temp; for (s=x; set[s]>=0; s=set[s]) ; while (s != x) { temp = set[x]; set[x] = s; x = temp; } return s; } void union_set(in