题目链接:http://codeforces.com/contest/451/problem/B

----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋http://user.qzone.qq.com/593830943/main

----------------------------------------------------------------------------------------------------------------------------------------------------------

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

题意:

寻找把数字串的某一段字串反转后是否能使数字串变为升序;并输出需反转的字串的起始和结束点;

思路:

把数字串排序后寻找和原数字串不同的子段是否是呈反转关系的。

代码例如以下:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
int a[100017];
struct NUM
{
int x,id;
}b[100017];
bool cmp(NUM p, NUM q)
{
return p.x < q.x;
}
int main()
{
int n, i, j;
while(~scanf("%d",&n))
{
for(i = 1; i <= n; i++)
{
scanf("%d",&a[i]);
b[i].id = i;
b[i].x = a[i];
}
sort(b+1,b+n+1,cmp);
for(i = 1; i <= n; i++)//寻找不同段的開始点
{
if(b[i].id != i)
break;
}
if(i == n+1)
{
printf("yes\n1 1\n");
continue;
}
for(j = n; j >= 1; j--)//寻找不同段的末尾点
{
if(b[j].id != j)
break;
}
int tt = i;
int t = j;
for(; i <= j; i++)//反向比較不同段
{
if(b[i].x != a[t--])
break;
}
if(i == j+1)
{
printf("yes\n%d %d\n",tt,j);
}
else
printf("no\n");
}
return 0;
}

Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)的更多相关文章

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

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

    题目链接:http://codeforces.com/contest/451/problem/B 思路:首先找下降段的个数,假设下降段是大于等于2的,那么就直接输出no,假设下降段的个数为1,那么就把 ...

  3. Codeforces Round #258 (Div. 2/B)/Codeforces451B_Sort the Array

    解题报告 http://blog.csdn.net/juncoder/article/details/38102391 对于给定的数组,取对数组中的一段进行翻转,问翻转后是否是递增有序的. 思路: 仅 ...

  4. Codeforces Round #258 (Div. 2) D. Count Good Substrings 水题

    D. Count Good Substrings 题目连接: http://codeforces.com/contest/451/problem/D Description We call a str ...

  5. Codeforces Round #258 (Div. 2) A. Game With Sticks 水题

    A. Game With Sticks 题目连接: http://codeforces.com/contest/451/problem/A Description After winning gold ...

  6. Codeforces Round #331 (Div. 2) B. Wilbur and Array 水题

    B. Wilbur and Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/596/p ...

  7. Codeforces Round #280 (Div. 2)E Vanya and Field(简单题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 本场题目都比较简单,故只写了E题. E. Vanya and Field Vany ...

  8. Codeforces Round #258 (Div. 2)[ABCD]

    Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...

  9. Codeforces Round #258 (Div. 2) 小结

    A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行 ...

随机推荐

  1. Python3简明教程(二)—— 变量和数据类型

    关键字和标识符 下列的标识符是Python3的关键字,并且不能用于通常的标识符.关键字必须严格按照下面的拼写: False def if raise None del import return Tr ...

  2. 通过JS加载XML文件,跨浏览器兼容

    引言 通过JS加载XML文件,跨多种浏览器兼容. 在Chrome中,没有load方法,需要特殊处理! 解决方案 部分代码 try //Internet Explorer { xmlDoc=new Ac ...

  3. JAVA自定义栈

    public class Stack{ int[] data; int maxSize; int top; public Stack(int maxSize) { this.maxSize=maxSi ...

  4. python之range (范围)

    例题: for i in range(10): print(i,end='') # 输出结果 0123456789 # s = range(1,10) # 面试大坑 python2 和 python3 ...

  5. 如何实时查看mysql当前连接数?

    1.查看当前所有连接的详细资料: ./mysqladmin -uadmin -p -h10.140.1.1 processlist2.只查看当前连接数(Threads就是连接数.): ./mysqla ...

  6. 前端基础之CSS_1

    摘要 CSS(层叠样式表)的三种设置方法 基本选择器 组合选择器 属性选择器 分组与嵌套 伪类选择器 伪元素选择器 选择器的优先级 一些样式的设置(字体.文本.背景.边框) display属性设置 0 ...

  7. 免费开源《OdooERP系统部署架构指南》试读:第一章 Odoo架构概述

    文/开源智造联合创始人老杨 本文来自<OdooERP系统部署架构指南>的试读章节.书籍尚未出版,请勿转载.欢迎您反馈阅读意见. 从web浏览器到PostgreSQL,多层与其他层交互以处理 ...

  8. Android设置透明状态

    xml中: android:background="@android:color/transparent" 半透明: android:background="#e0000 ...

  9. .NET Core 文件的上传与下载

    1.前言 文件导入导出是简单且常用的功能,以下示例实现了文件上传与下载的几种方法,如有不妥的地方,欢迎指正.小白一枚,在往目标前进. 2.1.文件上传    --    纯文件 <form ac ...

  10. ExtJS前端框架EXT弹出窗口事件

    https://blog.csdn.net/alsyuan/article/details/73240841 Ext.MessageBox.alert()Ext.MessageBox.alert()提 ...