本文共 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 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.
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.
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.
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/