题目描述
blablabla
样例
blablabla
算法1
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
#include<iostream>
#include<set>
using namespace std;
const int N = 1e5 + 10;
int a[N], tr[N], ans[N];
int n;
int lowbit(int x)
{
return x & -x;
}
void add(int x, int c)//可以不用了;
{
for (int i = x; i <= n; i += lowbit(i)) tr[i] += c;
}
int sum(int x)
{
int res = 0;
for (int i = x; i; i -= lowbit(i)) res += tr[i];
return res;
}
int main()
{
cin >> n;
for (int i = 2; i <= n; i ++) cin >> a[i];
for (int i = 1; i <= n; i ++) tr[i] = lowbit(i);//初始化;
for (int i = n; i; i --)
{
int k = a[i] + 1;
int l = 1, r = n;
while(l < r)
{
int mid = l + r >> 1;
if (sum(mid) >= k) r = mid;
else l = mid + 1;
}
ans[i] = l;
add(l, - 1);
}
for (int i = 1; i <= n; i ++) cout << ans[i] << endl;
return 0;
}
算法2
(暴力枚举) $O(n^2)$
blablabla
时间复杂度
参考文献
C++ 代码
blablabla
我想问为什么要这样初始化啊,我知道就和 add(i,1)这样,但是不知道为什么。。