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

题目链接: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(简单题),布布扣,bubuko.com

时间: 2024-10-25 15:39:16

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

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 Round #258 (Div. 2/B)/Codeforces451B_Sort the Array

解题报告 http://blog.csdn.net/juncoder/article/details/38102391 对于给定的数组,取对数组中的一段进行翻转,问翻转后是否是递增有序的. 思路: 仅仅要找到最初递减的区域,记录区域内最大和最小的值,和区间位置. 然后把最大值与区间的下一个元素对照,最小值与区间上一个元素对照. 这样还不够,可能会出现两个或两个以上的递减区间,这样的情况直接pass,由于仅仅能翻转一次. #include <iostream> #include <cstd

Codeforces Round #258 (Div. 2)

A - Game With Sticks 题目的意思: n个水平条,m个竖直条,组成网格,每次删除交点所在的行和列,两个人轮流删除,直到最后没有交点为止,最后不能再删除的人将输掉 解题思路: 每次删除交点所在的行和列,则剩下n-1行和m-1列,直到行或列被删完为止,最多删除的次数为min(n,m),删除min(n,m)后剩余的都是行或者列(注意行与行,列与列之间不可能有交点).只需要判断min(n,m)的奇偶性. #include <iostream> #include <vector&

Codeforces Round #258 (Div. 2) 小结

A. Game With Sticks (451A) 水题一道,事实上无论你选取哪一个交叉点,结果都是行数列数都减一,那如今就是谁先减到行.列有一个为0,那么谁就赢了.因为Akshat先选,因此假设行列中最小的一个为奇数,那么Akshat赢,否则Malvika赢. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace

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

Codeforces Round #258 (Div. 2)[ABCD] ACM 题目地址:Codeforces Round #258 (Div. 2) A - Game With Sticks 题意: Akshat and Malvika两人玩一个游戏,横竖n,m根木棒排成#型,每次取走一个交点,交点相关的横竖两条木棒要去掉,Akshat先手,给出n,m问谁赢. 分析: 水题,很明显不管拿掉哪个点剩下的都是(n-1,m-1),最后状态是(0,x)或(x,0),也就是拿了min(n,m)-1次,

Codeforces Round #258 (Div. 2)-(A,B,C,D,E)

http://blog.csdn.net/rowanhaoa/article/details/38116713 A:Game With Sticks 水题...每次操作,都会拿走一个横行,一个竖行. 所以一共会操作min(横行,竖行)次. #include<stdio.h> #include<iostream> #include<stdlib.h> #include<string.h> #include<algorithm> #include&l

Codeforces Round #258 (Div. 2) A. Game With Sticks(数学题)

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

Codeforces Round #258 (Div. 2/A)/Codeforces451A_Game With Sticks

解题报告 http://blog.csdn.net/juncoder/article/details/38102263 n和m跟木棍相交,问一人取一交点(必须是交点,且取完后去掉交点的两根木棍),最后谁赢 思路: 取最大正方形,以对角线上的交点个数判断输赢. #include <iostream> #include <cstdio> using namespace std; int main() { int m,n; while(cin>>n>>m) { i

Codeforces Round #258 (Div. 2/C)/Codeforces451C_Predict Outcome of the Game(枚举)

解题报告 http://blog.csdn.net/juncoder/article/details/38102391 题意: n场比赛其中k场是没看过的,对于这k场比赛,a,b,c三队赢的场次的关系是a队与b队的绝对值差d1,b队和c队绝对值差d2,求是否能使三支球队的赢的场次相同. 思路: |B-A|=d1 |C-B|=d2 A+B+C=k 这样就有4种情况,分别是: B>A&&C<B B>A&&C>B B<A&&C<