Lost Cows
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 11614 | Accepted: 7465 |
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.
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.
* 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
数据小,可以直接n^2暴力
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
int i,x,t,n,a[8005],ans[8005],book[8005];
cin>>n;
for(i=2;i<=n;i++)
cin>>a[i];
book[a[n]+1]=1;
ans[n]=a[n]+1;
for(i=n-1;i>=1;i--)
{
t=a[i];
for(x=1;x<=n;x++)
{
if(!book[x])
{
if(t==0)
{
ans[i]=x;
book[x]=1;
break;
}
t--;
}
}
}
for(i=1;i<=n;i++)
cout<<ans[i]<<endl;
return 0;
}
也可以用树状数组加二分,速度更快一些,设已知逆序数为a1,a2,a3....an ,要求的原序列为ans1,ans2,ans3.....ansn,首先可以确定的是最后一个数的值,ansn=an+1
因为所有数都在他之前,而又知道了他之前有多少个比他小的,即可确定这个数是多少,之后就可用同样的道理从后往前求接下来的数,
倒数第二个数也就是不算最后一个数,从1开始第a(n-1)+1个数
例如样例
a:0 1 2 1 0
ansn=1,求ans(n-1)时就不要考虑1这个数,第a(n-1)+1=1+1=2 个数,也就是3
用树状数组实现,刚开始每个点都赋成1,求出一个点,就把这个点更新成0,二分优化查询
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
#include<iostream>
#include<map>
#include<vector>
#include<math.h>
using namespace std;
int n,tree[100000],ans[100000],a[100000],book[100000];
int sum(int x)
{
int sum=0;
while(x>0)
{
sum+=tree[x];
x-=x&-x;
}
return sum;
}
int add(int x,int num)
{
while(x<=n)
{
tree[x]+=num;
x+=x&-x;
}
return 0;
}
int main()
{
int i,j;
cin>>n;
for(i=2;i<=n;i++)
cin>>a[i];
a[1]=0;
for(i=1;i<=n;i++)
{
add(i,1);
}
ans[n]=a[n]+1;
add(a[n]+1,-1);
for(i=n-1;i>=1;i--)
{
int xx,le=1,ri=n,mid;
while(le<=ri)
{
mid=(le+ri)>>1;
if(sum(mid)<a[i]+1)
{
le=mid+1;
}
else
{
ri=mid-1;
xx=mid;
}
}
ans[i]=xx;
add(xx,-1);
}
for(i=1;i<=n;i++)
cout<<ans[i]<<endl;
return 0;
}