Lost Cows
Description
N (2 <= N <= 8,000) cows have unique brands in the range 1…N. In a spectacular display of poor judgment, they visited the neighborhood ‘watering hole’ and drank a few too many beers before dinner. When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands.
Regrettably, FJ does not have a way to sort them. Furthermore, he’s not very good at observing problems. Instead of writing down each cow’s brand, he determined a rather silly statistic: For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow.
Given this data, tell FJ the exact ordering of the cows.
Input
-
Line 1: A single integer, N
-
Lines 2…N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow. Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on.
Output
- Lines 1…N: Each of the N lines of output tells the brand of a cow in line. Line #1 of the output tells the brand of the first cow in line; line 2 tells the brand of the second cow; and so on.
Sample Input
5
1
2
1
0
Sample Output
2
4
5
3
1
方法一
平平无奇的一个二分+树状数组,时间复杂度 O ( n l o g 2 n ) O(nlog^2n) O(nlog2n)。
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
int n,a[maxn],c[maxn];
void add(int x,int v){
for(;x<maxn;x+=(x&-x)) c[x]+=v;
}
int ask(int x){
int ret=0;
for(;x;x-=(x&-x)) ret+=c[x];
return ret;
}
int main(){
scanf("%d",&n);
for(int i=2;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) add(i,1);
for(int i=n;i>=1;i--){
int l=1,r=n;
while(l<r){
int mid=(l+r)/2;
int k=ask(mid);
if(ask(mid)>=a[i]+1) r=mid;
else l=mid+1;
}
a[i]=l,add(l,-1);
}
for(int i=1;i<=n;i++) printf("%d\n",a[i]);
}
方法二
倍增+树状数组。倍增利用了树状数组本身的性质,它们的的组合更加契合。所以复杂度降为
O
(
n
l
o
g
n
)
O(nlogn)
O(nlogn)。
这个倍增会找一个符合条件的最大值,但是我们需要的是一个最小值,所以ans+c[k+qpow(2,j)]<a[i]+1
,这里用了
<
<
<而不是
≤
\leq
≤,就是寻找小于目标值的最大值,最后再
+
1
+1
+1,就得到了等于目标值的最小值。
总之是一个树状数组灵活应用的一个解法。
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=1e5+10;
int n,a[maxn],c[maxn];
int qpow(int a,int b){
int ret=1;
while(b){
if(b&1) ret*=a;
a*=a,b>>=1;
}
return ret;
}
void add(int x,int v){
for(;x<maxn;x+=(x&-x)) c[x]+=v;
}
int main(){
scanf("%d",&n);
for(int i=2;i<=n;i++) scanf("%d",&a[i]);
for(int i=1;i<=n;i++) c[i]=i-(i-(i&-i));
for(int i=n;i>=1;i--){
int p=log2(n),k=0,ans=0;
for(int j=p;j>=0;j--)
if(k+qpow(2,j)<=n&&ans+c[k+qpow(2,j)]<a[i]+1)
k+=qpow(2,j),ans+=c[k];
a[i]=k+1,add(k+1,-1);
}
for(int i=1;i<=n;i++) printf("%d\n",a[i]);
}
update(2022-3-26)
最近做天梯赛、笔试题的时候,几次都需要用到 “树状数组+二分/倍增”。翻了翻书,复习了 “树状数组+倍增” 的这个知识点,感觉又有新的收获吧。再贴一个今天写的代码,希望面试顺利、学业有成。
#include<bits/stdc++.h>
using namespace std;
constexpr int N=8010;
int n,a[N],c[N],sum[N];
void add(int x,int v){
for(;x<N;x+=(x&-x)) c[x]+=v;
}
int main(){
cin>>n;
for(int i=1;i<N;i++) sum[i]=min(i,n);
for(int i=1;i<N;i++) c[i]=sum[i]-sum[i-(i&-i)];
for(int i=2;i<=n;i++) cin>>a[i],a[i]++;
a[1]=1;
for(int i=n;i>=1;i--){
int p=0,ans=0;
for(int j=(1<<12);j;j>>=1)
if(p+j<N&&ans+c[p+j]<a[i]) ans+=c[p+j],p+=j;
add(p+1,-1);
a[i]=p+1;
}
for(int i=1;i<=n;i++) cout<<a[i]<<"\n";
}