SRM 670 div2 B BearSlowlySorts

原创 2015年11月19日 22:11:48
   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

题意:给你一个序列,每次操作最多对n-1个元素排序,问最少几次操作能使序列变成升序。

分四种情况

情况一:本来就是升序的

直接返回0

情况二:最大的在最后或者最小的在最前面

这种情况只要操作一次即可。

返回1

情况三:最小的在最后,最大的在最前面

把最大值或者最小值通过一次操作,使其到中间位置。

这就变成情况二了

返回3

情况四:其他

首先把不是在两端的最大值或者最小值通过一次操作,使其到最前面或者最后。

然后就是第二种情况了

返回二

做题时太浮躁了搞了半天才A

#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 DIV 2 Cdgame 250-point

Problem StatementTwo players are playing a cooperative game. At the beginning of the game each play...

SRM 670 div2 A

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

Topcoder SRM 648 Div.2 - A(水), B(枚举),C(DP/构造)

A. KitayutaMart2 水题~求一下公式即可,2^(n+1) = T/k+1. CODE : #line 7 "KitayutaMart2.cpp" #include ...

TC SRM 388 div 2 problem 3

  • 2012年04月16日 22:16
  • 2KB
  • 下载

SRM 590 DIV1 B题 XorCards ---- 高斯消元法

很久就开通博客了  无奈人太懒  从来没写过  最近开始学linux  开始慢慢写linux日志了  就顺带着也写点ACM的题解吧 题意:给你n个数a0,a1...a(n-1)(n 例如:  {1...
  • qw4990
  • qw4990
  • 2013年09月10日 00:59
  • 925

topcoder SRM495 div1 level2

Problem Statement   There are N boxes numbered 0 through N-1. Every box except for one conta...

Topcoder srm 653 div.2 1000 - SingingEasy(区间DP)

题意: 给出一个序列,请你找出两个没有交且并为全集的子序列,使得每个子序列相邻两位之间的数字差的绝对值之和最小。 思路: DP....(dp弱渣, 折腾了好久请教了人才会>, dp[i][j] 表示A...

SRM537-div1-2-KingXMagicSpells

zz: http://www.strongczq.com/2012/03/srm537-div1-2-kingxmagicspells.html 题目原文:http://community.t...

TopCoder 250 points 28-SRM 157 DIV 2 185.60/250 74.24%

普通的二分查找。 Problem Statement   A popular guessing game is "Guess the number", where one pe...

SRM 556 DIV2 解题报告

250:简单的字符串处理题目,因为最多只有50个字符,所以可以用O(n^2)的算法,对从每个位置开始的字符进行扫描,每次扫到了重复的字符即退出此次扫描,并比较长度是否最大即可。 500:看起...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:SRM 670 div2 B BearSlowlySorts
举报原因:
原因补充:

(最多只允许输入30个字)