Half ----- special sort

There are N elements numbered 1.2 ... N, the relationship between the size of each element is determined by the relationship with inversion symmetry, but not transitive.
Note: where two elements of equal size do not exist.
That is, the relationship between the size of the element is the N * N points in any configuration of a directed edge (N-1) / 2 directed Article FIG.
However, this is an interactive questions, that these relationships can not be a one-time, you must pass no more than 10,000 questions to obtain information, each question can only understand the relationship between a certain two elements.
Now you put these N elements are aligned so that each element is less than the right side and its adjacent elements.
You can get the size relationship between the two elements through our pre-bool function compare.
For example, the number of two elements a and b, the element if the element a is less than b, the Compare (a, b) returns true, otherwise returns false.
After the N elements sorted, put their output in the form of an array of numbers, if the answer is not unique, output any one can be.
Data range
1≤N≤10001≤N≤1000
input sample
[[0, 1, 0], [0, 0, 0], [1, 1, 0]]

Sample output
[3, 1, 2]

// Forward declaration of compare API.
// bool compare(int a, int b);
// return bool means whether a is less than b.
class Solution {
public:
    vector<int> specialSort(int N) {
         vector <int> res(1, 1);
   for (int i = 2; i <= N; i ++){
    int l = 0, r = res.size() - 1;
    while (l < r){
     int mid = l + r + 1 >> 1;
     if (compare(res[mid], i))   l = mid;
     else   r = mid - 1;
    }
   res.push_back(i);
   for (int j = res.size() - 2; j > r; j --)   swap(res[j], res[j + 1]);
   if (compare(i, res[r]))    swap(res[r], res[r + 1]);
   }
   return res;   
    }
};
Published 106 original articles · won praise 67 · views 5442

Guess you like

Origin blog.csdn.net/qq_45772483/article/details/104725534