SRM 397 DIV2 [500]

TopCoder SRM 专栏收录该内容
11 篇文章 0 订阅

 

#include < iostream >
#include
< queue >
#include
< vector >
#include
< set >
#include
< algorithm >
using   namespace  std;

vector
< int >  swap(vector < int >  a, int  beg, int  len)
{
    
int mid=len/2,end=beg+len-1;;
    
for (int i=0;i!=mid;++i){
        
int temp=a[beg+i];
        a[beg
+i]=a[end-i];
        a[end
-i]=temp;
    }

    
return a;
}


class  SortingGame {
public:
    
int fewestMoves(vector<int> a,int num)
    
{
        
int len=a.size();
           vector
<int> des(a);
        sort(des.begin(),des.end());
        
if (des==a) return 0;
        
set<vector<int> > vis;
        vis.insert(a);
        queue
<vector<int> > qData;
        qData.push(a);
        queue
<int> qTime;
        qTime.push(
0);
        
while (!qData.empty()){
            
for (int i=0;i!=len-num+1;++i){
                vector
<int> temp(swap(qData.front(),i,num) );
                
if (temp==des)     return ++qTime.front();
                
if (!vis.count(temp)){
                    vis.insert(temp);
                    qTime.push(qTime.front()
+1);
                    qData.push(temp);
                }

            }

            qData.pop();
            qTime.pop();
        }

        
return -1;
    }

}
;

Problem Statement

     In The Sorting Game, you are given a sequence containing a permutation of the integers between 1 and n, inclusive. In one move, you can take any k consecutive elements of the sequence and reverse their order. The goal of the game is to sort the sequence in ascending order. You are given a vector <int> board describing the initial sequence. Return the fewest number of moves necessary to finish the game successfully, or -1 if it's impossible.

Definition

    
Class: SortingGame
Method: fewestMoves
Parameters: vector <int>, int
Returns: int
Method signature: int fewestMoves(vector <int> board, int k)
(be sure your method is public)
    
 

Constraints

- board will contain between 2 and 8 elements, inclusive.
- Each integer between 1 and the size of board, inclusive, will appear in board exactly once.
- k will be between 2 and the size of board, inclusive.

Examples

0)  
    
{1,2,3}
3
Returns: 0
The sequence is already sorted, so we don't need any moves.
1)  
    
{3,2,1}
3
Returns: 1
We can reverse the whole sequence with one move here.
2)  
    
{5,4,3,2,1}
2
Returns: 10
This one is more complex.
3)  
    
{3,2,4,1,5}
4
Returns: -1
 
4)  
    
{7,2,1,6,8,4,3,5}
4
Returns: 7
 

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)2003, TopCoder, Inc. All rights reserved.

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值