Codeforces Round #258 (Div. 2) B. Sort the Array(简单题)
题目链接:http://codeforces.com/contest/451/problem/B
----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋:http://user.qzone.qq.com/593830943/main
----------------------------------------------------------------------------------------------------------------------------------------------------------
1 second
256 megabytes
standard input
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.
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).
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.
3
3 2 1
yes
1 3
4
2 1 3 4
yes
1 2
4
3 1 2 4
no
2
1 2
yes
1 1
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(简单题)的更多相关文章
- 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 ...
- Codeforces Round #258 (Div. 2) B. Sort the Array
题目链接:http://codeforces.com/contest/451/problem/B 思路:首先找下降段的个数,假设下降段是大于等于2的,那么就直接输出no,假设下降段的个数为1,那么就把 ...
- Codeforces Round #258 (Div. 2/B)/Codeforces451B_Sort the Array
解题报告 http://blog.csdn.net/juncoder/article/details/38102391 对于给定的数组,取对数组中的一段进行翻转,问翻转后是否是递增有序的. 思路: 仅 ...
- 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 ...
- Codeforces Round #258 (Div. 2) A. Game With Sticks 水题
A. Game With Sticks 题目连接: http://codeforces.com/contest/451/problem/A Description After winning gold ...
- 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 ...
- Codeforces Round #280 (Div. 2)E Vanya and Field(简单题)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 本场题目都比较简单,故只写了E题. E. Vanya and Field Vany ...
- Codeforces Round #258 (Div. 2)[ABCD]
Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意 ...
- Codeforces Round #258 (Div. 2) 小结
A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行 ...
随机推荐
- HDU 4281 (状态压缩+背包+MTSP)
Judges' response Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- iptables 防火墙
运行源地址为192.168.10.10-192.168.10.50 这个网段的机器访问本机的20-25还有80.443.6379端口进来的流量 iptables -A INPUT -p tcp -m ...
- java文件上传,自动判断文件类型
public enum FileType { /** * JEPG. */ JPEG("FFD8FF"), /** * PNG. */ PNG("89504E47&quo ...
- Java:Md5加密
文章来源:https://www.cnblogs.com/hello-tl/p/9139334.html import java.security.MessageDigest; public clas ...
- Django框架基础知识09-请求与响应
视图函数接受到的request到底是个什么对象呢? HttpRequest对象: 服务器接收到http协议的请求后,会根据报文创建HttpRequest对象视图函数的第一个参数是HttpRequest ...
- win10修改文件管理器默认打开我的电脑
- jquery validate基本
http://www.runoob.com/jquery/jquery-plugin-validate.html jquery validate 默认 在键盘按下并释放及提交后验证提交表单 例如: $ ...
- LeetCode(12)Integer to Roman
题目 Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...
- 数据结构实验4:C++实现循环队列
实验4 4.1 实验目的 熟练掌握队列的顺序存储结构和链式存储结构. 熟练掌握队列的有关算法设计,并在循环顺序队列和链队列上实现. 根据具体给定的需求,合理设计并实现相关结构和算法. 4.2 实验要求 ...
- vs2017编译boost 1.70.0
目前最新版本的boost库是1.70.0.现在在学习使用cinatra搭建c++的http服务器,需要用到boost库中的asio,下载了一下最新版本的boost库,捣鼓了半天. 1.下载 boost ...