My solution for SRM 306 DIV2 [250]

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;
  }
};

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭