hdu 1541 Stars【树状数组简单应用】

来源:互联网 发布:沈飞 知乎 编辑:程序博客网 时间:2024/05/21 10:54

链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1541

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28619#problem/C

Stars

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


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
51 15 17 13 35 5
 

Sample Output
12110
 

Source
Ural Collegiate Programming Contest 1999
 

Recommend
LL


题意:

                给你 N 个星星的坐标.
            求出他们的水平 level 。再依次输出水平 level 为  0 到 N-1 的个数
            星星的水平:就是输出当前星星的左下角的星星的个数。
                                 比如说如果满足 star1.x <= star2.x && star1.y <= star2.y 那么 star2 的 level 就加一。 

算法: 树状数组


思路: 

               很裸的树桩数组有没有哭当时怎么就没有想到,怎么就没有想到Orz
       
      特别是题目中已经很明显的指出了:
              Stars are listed in ascending order of Y coordinate. 
              Stars with equal Y coordinates are listed in ascending order of X coordinate.
              按照 Y 的升序输入, Y 相同的按照 X 的升序输入。排序处理都给你免了。。。    
              因为是按照顺序输入的, 那么完全不用管 Y 只用算出前面出现过的横坐标不大于当前星星的数目就是当前星星的平。

              PS:如果没有 Y  那么只有一行 X ,上面的思路很好理解吧。
                   现在再考虑添加了 Y 的情况, 因为是按照顺序输入的, 
                   所以当前星星 i 前面的星星 j (0<j<i)的 Y 全都满足 star[j].y <= star[i].y
                   所以添加一个星星,只用在它的横坐标的位置把星星数目 +1 即可。

code:

Accepted154146MS412K1077 BC++
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;const int maxn = 15000+10;const int maxX = 32000+10;int c[maxX];int level[maxn];int lowbit(int x){    return x&(-x);}void add(int x, int d) //d = 1{    while(x <= 32000) //星星的横坐标不超过 32000    {        c[x] += d; //c[x]++        x += lowbit(x);    }}int sum(int x){    int ret = 0;    while(x > 0)    {        ret += c[x];        x -= lowbit(x);    }    return ret;}int main(){    int n;    while(scanf("%d", &n) != EOF)    {        memset(c,0,sizeof(c));        memset(level,0,sizeof(level));        int x,y;        for(int i = 0; i < n; i++)        {            scanf("%d%d", &x,&y);            x++; // 树状数组的下标从 1 开始, 星星的下标可能是 0, 把星星整体往右移一个单位, 不影响结果            level[sum(x)]++; //水平为 sum(x) 的星星加一 ,printf("test: %d\n", sum(x));            add(x, 1); // 横坐标为 X+1 的星星数目 +1        }        for(int i = 0; i < n; i++)            printf("%d\n", level[i]);    }}


原创粉丝点击