-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathQueue Reconstruction by Height.cpp
41 lines (40 loc) · 1.3 KB
/
Queue Reconstruction by Height.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Runtime: 24 ms (Top 88.43%) | Memory: 12.10 MB (Top 82.86%)
class Solution {
public:
vector<int> BIT;
int n;
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
n = people.size();
BIT = vector<int>(n+1, 0); //BIT[i+1] recorded the res[i] information because BIT[0] is not used.
for(int i = 2; i <= n; i++) update(i, 1); // BIT[1] is the 0th empty position, so we didn't add 1
sort(people.begin(), people.end(), cmp);
vector<vector<int>> res(n, vector<int>());
for(int i = 0; i < n; i++){
int l=0, r=n;
while(l<r){
int mid=l+(r-l)/2;
if(getsum(mid+1)<people[i][1]) l=mid+1; // we need get the index mid empty information, but actually it's stored in BIT[mid+1]
else r=mid;
}
res[l]=people[i];
update(l+1, -1);
}
return res;
}
void update(int x, int v){
for(int i = x; i <= n; i+=(i&-i)){
BIT[i]+=v;
}
}
int getsum(int x){
int sum=0;
for(int i = x; i > 0; i-=(i&-i)){
sum += BIT[i];
}
return sum;
}
static bool cmp(vector<int>& p1, vector<int>& p2){
if(p1[0]!=p2[0]) return p1[0]<p2[0];
else return p1[1]>p2[1];
}
};