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

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

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
  1. 3
  2. 3 2 1
output
  1. yes
  2. 1 3
input
  1. 4
  2. 2 1 3 4
output
  1. yes
  2. 1 2
input
  1. 4
  2. 3 1 2 4
output
  1. no
input
  1. 2
  2. 1 2
output
  1. yes
  2. 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].

题意:

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

思路:

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

代码例如以下:

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5. int a[100017];
  6. struct NUM
  7. {
  8. int x,id;
  9. }b[100017];
  10. bool cmp(NUM p, NUM q)
  11. {
  12. return p.x < q.x;
  13. }
  14. int main()
  15. {
  16. int n, i, j;
  17. while(~scanf("%d",&n))
  18. {
  19. for(i = 1; i <= n; i++)
  20. {
  21. scanf("%d",&a[i]);
  22. b[i].id = i;
  23. b[i].x = a[i];
  24. }
  25. sort(b+1,b+n+1,cmp);
  26. for(i = 1; i <= n; i++)//寻找不同段的開始点
  27. {
  28. if(b[i].id != i)
  29. break;
  30. }
  31. if(i == n+1)
  32. {
  33. printf("yes\n1 1\n");
  34. continue;
  35. }
  36. for(j = n; j >= 1; j--)//寻找不同段的末尾点
  37. {
  38. if(b[j].id != j)
  39. break;
  40. }
  41. int tt = i;
  42. int t = j;
  43. for(; i <= j; i++)//反向比較不同段
  44. {
  45. if(b[i].x != a[t--])
  46. break;
  47. }
  48. if(i == j+1)
  49. {
  50. printf("yes\n%d %d\n",tt,j);
  51. }
  52. else
  53. printf("no\n");
  54. }
  55. return 0;
  56. }

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. OSI七层模型和TCP/IP五层模型详解

    OSI是一个开放性的通信系统互连参考模型,他是一个定义得非常好的协议规范.OSI模型有7层结构,每层都可以有几个子层. OSI的7层从上到下分别是 7 应用层 6 表示层 5 会话层 4 传输层 3 ...

  2. rfcn讲解博客

    http://www.cnblogs.com/lillylin/p/6277094.html ROI pooling操作的输入(对于C+1个类)是k^2*(C+1)*W' *H'(W'和H'是ROI的 ...

  3. 1.入手树莓派之linux环境搭建

    最近刚刚买了一款 树莓派3代B型 raspberrypi 板载蓝牙和WIFI 英国版本,没玩过,觉得很好奇,生怕记性不好哈,把自己玩的过程记录一下,以备不时之需: 需要材料: 1) 树莓派: 2)sd ...

  4. windows10用WMware安装Linux虚拟机详细步骤

    windows10用WMware安装Linux虚拟机详细步骤   一.安装环境 windows10操作系统物理机VMware Workstation 软件(可以在网上下载)CentOS6.9镜像文件( ...

  5. git命令使用(一)

    作为程序员怎么能不了解git命令呢,但是由于本人不常用到git命令,现在的软件上也都一体化了,能够简化命令,直接运行都可以了,完全能够去实现git上的命令,导致输入git命令完全不会,git命令能够让 ...

  6. Android流行界面结构——Fragment通过ViewPager(带指示器)嵌套Fragment结构的创建方法详解

    原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6287213.html 当前Android流行界面结构的一种——Fragment通过ViewPage ...

  7. URAL1966 Cipher Message 3

    题目描述 题解: 能看出来的是,每一组数只能改最后一位,所以前$7$位动不了. 所以$KMP$跑一跑. 重点在于最后一位怎么搞. 如果$KMP$跑完了还没找到合适的位置,直接$puts("N ...

  8. centeros 6 远程升级ssl ssh 的shell脚本

    变量说明 SSL_N=openssl-1.0.2p #ssl 版本SSH_N=openssh-7.9p1 #ssh 版本ZLIB_N=zlib-1.2.11 # zlib 版本 脚本分为两个,因为升级 ...

  9. mysql5.7配置

    my3306.cnf [client] port = 3306   #端口socket = /data/mysql3306/mysql3306.sock   #mysql以socket方式运行的soc ...

  10. Beego:原生方式使用MySQL

    示例: package controllers import ( "database/sql" "fmt" "github.com/astaxie/b ...