博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDOJ 1541 Stars 树状数组
阅读量:4217 次
发布时间:2019-05-26

本文共 2546 字,大约阅读时间需要 8 分钟。

Stars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11481    Accepted Submission(s): 4568


Problem Description
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.
 

Input
The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
 

Output
The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.
 

Sample Input
 
5 1 1 5 1 7 1 3 3 5 5
 

Sample Output

1

2

1

1

0

题目大意:统计不同度数 星的个数,度数规则为,其他星的x,y同时低于(包含等于)该星时候,该星度数加1,直到该星最大读数.

题目输入以y升序排序,在y相同时候以x升序排列.

这题 因为题目数据的排列规则,后面的数据必然是y大于等于前者的,所以知道判断某个数据之前的x坐标情况,只要在某个星之前的x坐标小于该星x坐标,则度加1,所以,此题可以转化为求到某个x时候其之前有多少个x坐标。(类似与求逆序数)

可以借助树状数组,这里把x+1作为下标(同求逆序数),用x+1因为数据有0,防止进入死循环。

树状数组的n(更新用的)是数据的规模(此题就是下表X的范围),不能与输入的样本个数混为一谈,此题刚开始把两个n弄成了一个,导致半天才找到错误

#include
using namespace std;const int MAXN = 15000+5;int C[32000+5];int lev[MAXN];int n = 32000+5;//关键int lowBit(int x){ return x&(-x);}void add(int i, int x){ while(i <= n){ C[i] = C[i] + x; i += lowBit(i); }}int getSum(int i){ int sum = 0; while(i > 0){ sum += C[i]; i -= lowBit(i); } return sum;}int main() { int p; while(scanf("%d",&p) != EOF){//这里不要弄混p,n memset(lev,0,sizeof(lev)); memset(C,0,sizeof(C)); int x,y; for(int i = 0; i < p; ++i){ scanf("%d%d",&x,&y);//y其实不用 ++x; ++lev[getSum(x)];//统计度数 add(x,1);//此x标记为1 } for(int i = 0; i < p; ++i){ printf("%d\n",lev[i]); } } return 0; }

转载地址:http://seimi.baihongyu.com/

你可能感兴趣的文章
单例模式(singleton),工厂方法模式(factory),门面模式(facade)
查看>>
抽象模式,适配器模式(Adapter),模板方法模式(Template method)
查看>>
建造者模式(builder),桥梁模式(bridge mode),命令模式(Command mode)
查看>>
装饰模式(Decorator),迭代器模式(Iterator),组合模式(composite)
查看>>
观察者模式(Observer),责任链模式,访问者模式(Visitor)
查看>>
状态模式(State)
查看>>
快速排序
查看>>
插入算法
查看>>
希尔排序
查看>>
选择排序
查看>>
归并排序
查看>>
归并排序
查看>>
插入排序进行链表排序
查看>>
android实现截图功能
查看>>
android 网络连接状态判断与数据类型
查看>>
android webview 实现网页加载进度
查看>>
《人性的弱点》
查看>>
《大师们是如何工作的》
查看>>
c++ 中的多重继承和其权限问题
查看>>
那些年
查看>>