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

Source

​USACO 2003 U S Open Orange​

Time Limit: 1000MS

 

Memory Limit: 65536K

Total Submissions: 13107


 

Accepted: 8398

算法分析:

题意:

给出的数字表示从第2到第n只牛左边有几个比它序号小的牛,要求每个位置的牛的编号是多少

分析:

1.首先,我们能分析出最后一个数是确定的,因为前面已经知道多少个比它小的数了。

2.如果你要确定某个数字p,那么我们就要知道p的前面有多少个数字比它小,记为m,p的后面有多少个数字比他小,记为n,那么p的位置就该在这个数列中的第m+n+1处,我们就二分这个p,拿每次得到的mid来和m+n+1来比较大小,如果m+n+1>mid就说明mid取小了,这个时候,我们就把left = mid+1;如果m+n+1<mid,那就说明mid取大了,这个时候我们就要把mid = right.

结合12,需要从前往后推。

  关于一个数字后面比他小的数字的个数如何求解:树状数组。

  关于一个数字前面有多少个数字比他小的问题,我们再输入的过程中已经知道了。

 

代码实现:

# include<cstdio>
# include<iostream>
# include<fstream>
# include<algorithm>
# include<functional>
# include<cstring>
# include<string>
# include<cstdlib>
# include<iomanip>
# include<numeric>
# include<cctype>
# include<cmath>
# include<ctime>
# include<queue>
# include<stack>
# include<list>
# include<set>
# include<map>
using namespace std;
const int MAXN=8000+5;
typedef long long ll;
int n;
int c[MAXN],a[MAXN],ans[MAXN];
int lowbit(int i)
{
return i&(-i);
}
ll sum(int x){
ll sum = 0;
while(x){
sum += c[x];
x -= lowbit(x);
}
return sum;
}
void add(int x, ll val){
while(x <= n){
c[x] += val;
x += lowbit(x);
}
}
int se(int ln)
{
int l=1,r=n;
while(l<=r)
{

int mid=(l+r)>>1;
//cout<<mid<<endl;
int rn=sum(mid);
if(ln+rn+1<=mid)
{
r=mid-1;
}
else
{
l=mid+1;
}
}
return l;
}
int main()
{
while(scanf("%d",&n)!=-1)
{
memset(c,0,sizeof(c));
memset(ans,0,sizeof(ans));
for(int i=2;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=n;i>=1;i--)
{
int pos=se(a[i]);
add(pos,1);
ans[i]=pos;
}
for(int i=1;i<=n;i++)
printf("%d\n",ans[i]);

}
return 0;

}