关闭

My solution for SRM 306 DIV2 [250]

829人阅读 评论(0) 收藏 举报
Problem Statement: http://www.topcoder.com/stat?c=problem_statement&pm=6413&rd=9986

My solution:

#include <iostream>
#include <vector>

using namespace std;

class SortMachine  {
 
public:
  SortMachine () {}
 
public:
  int countMoves(vector <int> a) {
    int countMove = 0;

    while(1) {
      int move = -1;
      int minInversion = 1001;
     
      for (unsigned int i = 0; i < a.size() - 1; ++i) {
        for (unsigned int j = i + 1; j < a.size(); ++j) {
          if (a[i] > a[j] && a[i] < minInversion) {
            move = i;
            minInversion = a[i];
            break;
          }
        }
      }
     
      if (move == -1) {
        break;
      }
     
      int temp = a[move];     
      for (unsigned int i = move; i < a.size() - 1; ++i) {
        a[i] = a[i + 1];
      }
      a[a.size() - 1] = temp;

      ++countMove;
    }
   
    return countMove;
  }
};

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:21437次
    • 积分:339
    • 等级:
    • 排名:千里之外
    • 原创:12篇
    • 转载:1篇
    • 译文:0篇
    • 评论:2条
    最新评论