Files
python/TangDou/AcWing_TiGao/T6/ErFen/113.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

24 lines
897 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<int> specialSort(int N) {
vector<int> a; // 结果数组
// a数组中入的是序号,没有值,本题我们在代码中不关注值,值是在我们黑盒的compare中体现的
a.push_back(1); // 第一个元素序号入数组
for (int k = 2; k <= N; k++) { // 枚举2~N个元素,它要放在哪里?
int l = 0, r = k - 1; // 右边界位置,左闭右开
while (l < r) {
int mid = (l + r) >> 1;
// 如果元素a小于元素b,则 compare(a,b) 返回 true
if (compare(k, a[mid])) // a[mid]是指序号
r = mid;
else
l = mid + 1;
}
a.insert(a.begin() + l, k);
}
return a;
}
};