# SRM 670 div2 B BearSlowlySorts

 Problem Statement

Problem Statement for BearSlowlySorts

### Problem Statement

Limak is a little polar bear. He has a int[] w containing a sequence of N distinct numbers. He wants to sort this sequence into ascending order.

Limak knows some fast sorting algorithms but in the real world such knowledge sometimes isn't enough. In order to sort the sequencew Limak must physically move the numbers into their correct places. Such a thing can be hard for a little bear.

In a single move Limak can take all elements he can reach and sort them into ascending order. The problem is that Limak's arms are too short. Regardless of where he stands, he can only reach N-1 consecutive elements of w. Hence, in each move he can either sort all elements except for the last one, or all elements except for the first one.

Limak can make the moves in any order he likes. Compute and return the smallest number of moves necessary to sort the given sequencew.

### Definition

 Class: BearSlowlySorts Method: minMoves Parameters: int[] Returns: int Method signature: int minMoves(int[] w) (be sure your method is public)

### Constraints

- w will contain between 3 and 50 elements, inclusive.
- Each element in w will be between 1 and 1000, inclusive.
- w will contain distinct elements.

### Examples

0)

 {2,6,8,5}
Returns: 1
 Limak can sort this sequence in a single move. All he needs to do is to sort all elements except for the first one. In this single move Limak will pick up the elements {6,8,5} and reorder them into {5,6,8}. This will change the given w into {2,5,6,8}, and that is a sorted sequence.
1)

 {4,3,1,6,2,5}
Returns: 2
 One of the shortest ways is to sort first N-1 numbers first to get {1,2,3,4,6,5} and then to sort the last N-1 numbers.
2)

 {93,155,178,205,213,242,299,307,455,470,514,549,581,617,677}
Returns: 0
 This sequence is already sorted, no moves are necessary.
3)

 {50,20,30,40,10}
Returns: 3
4)

 {234,462,715,596,906,231,278,223,767,925,9,526,369,319,241,354,317,880,5,696}
Returns: 2

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2010, TopCoder, Inc. All rights reserved.

This problem was used for:
Single Round Match 673 Round 1 - Division II, Level Two

#include
#include
#include
#include
#include
using namespace std;

class BearSlowlySorts
{
public:
int minMoves(vector w)
{
int ans = 0;
int tmp = w[0];
int len = w.size()-1;
int MAX = w[len];
int i;
int temp = w[0];
for(i = 0; i < w.size(); i++)
{
if(temp > w[i]) {ans = 1;break;}
temp = w[i];
}
if(!ans) return 0;
sort(w.begin(), w.end());
if(w[0] == tmp || MAX == w[len]) return 1;
if(tmp == w[len] && MAX == w[0]) return 3;
return 2;
}
};

• 本文已收录于以下专栏：

## SRM 670 div2 A

Problem Statement   Two players are playing a cooperative game. At the beginning of the...
• ZZ_AC
• 2015年10月11日 19:38
• 222

## SRM 670 div2 A BearSong

Problem Statement    Problem Statement for BearSong ...
• ZZ_AC
• 2015年11月19日 23:14
• 348

## SRM 649 div2 1000

• slowlight93
• 2015年02月12日 18:31
• 500

## TC SRM 670 Div2 1050 Treestrat

Problem StatementTwo players are playing a game with some tokens on a tree.Player A has one or more ...
• Liao_Jingyi
• 2015年10月14日 18:35
• 250

## Topcoder SRM 683 Div2 B

• huayunhualuo
• 2016年03月03日 15:20
• 396

## 【SRM 717 div2 B】LexmaxReplace

Problem StatementAlice has a string s of lowercase letters. The string is written on a wall. Alice ...
• harlow_cheng
• 2017年07月01日 11:18
• 90

## Topcoder SRM 636 Div2 B

• huayunhualuo
• 2016年06月02日 21:23
• 206

## TopCoder SRM 637 Div2 B

• huayunhualuo
• 2016年06月07日 19:35
• 203

## cf448B Suffix Structures

B. Suffix Structures time limit per test 1 second memory limit per test 256 megabytes ...
• zhb1997
• 2014年07月18日 00:59
• 251

## TopCoder SRM 670 Div2 Problem 1050 - Treestrat (floyd)

RT
• u014247806
• 2015年10月11日 10:00
• 511

举报原因： 您举报文章：SRM 670 div2 B BearSlowlySorts 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)