Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given
star. Astronomers want to know the distribution of the levels of the stars.
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.
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.
5 1 1 5 1 7 1 3 3 5 5
1 2 1 1 0
#include<iostream>
#include<cstdio>
#include<cstring>
#define N 32005
using namespace std;
int sum[N<<2],num[15002];
void update(int rf,int l,int r,int a)
{
if(l==r)
{
sum[rf]++;
return;
}
int mid=(l+r)>>1;
if(a<=mid)
update(rf*2,l,mid,a);
else
update(rf*2+1,mid+1,r,a);
sum[rf]=sum[rf*2]+sum[rf*2+1];
}
int query(int rf,int l,int r,int a,int b)
{
if(a<=l&&b>=r)
{
return sum[rf];
}
int mid=(l+r)>>1;
int ans=0;
if(a<=mid)
ans+=query(rf*2,l,mid,a,b);
if(b>mid)
ans+=query(rf*2+1,mid+1,r,a,b);
return ans;
}
int main()
{
int n,y,i,x;
while(~scanf("%d",&n))
{
memset(sum,0,sizeof(sum));
memset(num,0,sizeof(num));
for(i=1; i<=n; i++)
{
scanf("%d%d",&x,&y);
update(1,0,N,x);
num[query(1,0,N,0,x)]++;
}
for(i=1; i<=n; i++)
printf("%d\n",num[i]);
}
return 0;
}
本文介绍了一个天文学领域的算法问题,即如何通过编程计算星图中每个星星的级别,并统计各个级别的星星数量。级别定义为一个星星左侧下方星星的数量。文章提供了一个具体的C++实现方案,利用线段树进行区间更新和查询。
309

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



