• 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
    1 1
    ");
    			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
    %d %d
    ",tt,j);
    		}
    		else
    			printf("no
    ");
    	}
    	return 0;
    }


  • 相关阅读:
    Azkaban 简介(一)
    大数据平台搭建(Ambari +HDP)
    大数据平台比较-CDH、HDP、CDP
    Kylin 操作使用(六)
    Kylin 安装部署(五)
    Kylin 核心概念(四)
    数据流图
    android:sharedUserId
    Android的uid与UserHandle
    C++ 多态
  • 原文地址:https://www.cnblogs.com/cynchanpin/p/6860970.html
Copyright © 2020-2023  润新知