| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 13167 | Accepted: 5654 |
Description
For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.
You are to write a program that will count the amounts of the stars of each level on a given map.
Input
Output
Sample Input
5 1 1 5 1 7 1 3 3 5 5
Sample Output
1 2 1 1 0
Hint
Source
#include<stdio.h>
int level[15005];//放的是星星的层次
int a[32010];//放的是树状数组,是N的
inline int Lowbit(int x)
{
return x&(-x);//计算的是2^P,P就是X的0的个数
}
void Update(int x)//更新,每加入一个星星,那条路都要更新
{
for(int i=x;i<32002;i+=Lowbit(i))//直到最大值,都要更新
a[i]++;
}
int Getsum(int x)//取和,就是以X为最大值那个点
{
int temp=0;
for(int i=x;i>0;i-=Lowbit(i))
temp+=a[i];//真正明白那个图,这是不会重复计算的
return temp;
}
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
for(int i=0; i<=n; i++)
level[i]=a[i]=0;
int k=n;
while(k--)
{
int x,y;
scanf("%d%d",&x,&y);
level[Getsum(x+1)]++;//注意是X+1
Update(x+1);
}
for(int i=0;i<n;i++)
printf("%d/n",level[i]);
}
return 0;
}
本文介绍了一种使用树状数组高效统计星图中各星层级数量的方法。通过输入星星的坐标,程序能快速计算并输出每个层级星星的数量。该算法适用于大规模数据处理,在天文学等领域有实际应用价值。
711

被折叠的 条评论
为什么被折叠?



