其他分享
首页 > 其他分享> > acwing 113

acwing 113

作者:互联网

// 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> ans;
        ans.push_back(1);
        for(int idx = 2; idx <= n; idx++){
            int l = 0, r = ans.size();
            while(l < r){
                int mid = (l + r) >> 1;
                if(compare(idx, ans[mid])) r = mid; else l = mid + 1;
            }
            ans.push_back(idx);
            for(int j = ans.size()-2; j >= r; j--) swap(ans[j], ans[j + 1]);
        }
        return ans;
    }
};

标签:compare,idx,int,mid,back,113,ans,acwing
来源: https://blog.csdn.net/ssd778/article/details/122281740