zoukankan      html  css  js  c++  java
  • HDU 1541 Stars(树状数组)

    Stars

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


    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
     
    题意:天文学家经常观察星象图。星象图中用平面上的点来表示一颗星星,每一颗星星都有一个笛卡尔坐标。设定星星的等级为其左下角星星的总数。天文学家们想知道星星等级的分布情况。比如上图,5号星星的等级为3(其左下角有编号为1、2、4的星星共三颗)。2号星星和4号星星的等级为1。在上图中只有一颗星星等级为0,两颗星星等级为1,一颗星星等级为2,一颗星星等级为3。
    给定一个星象图,请你写一个程序计算各个等级的星星数目。输入的第一行包含星星的总数N (1<=N<=15000)。接下来N行,描述星星的坐标(X,Y)(X和Y用空格分开,0<=X,Y<=32000)。星象图中的每个点处最多只有一颗星星。所有星星按Y坐标升序排列。Y坐标相等的星星按X坐标升序排列。
     
    /*
    前面的必定比当前的低
    只需要考虑在当前的左边的
    */

    给出(x,y)只要求1~~x的和即可

     1 #include <iostream>
     2 #include <algorithm>
     3 #include<cstdio>
     4 #include<string>
     5 #define n 32002 //n=32000时WA了N多次... 
     6 int c[n + 5];
     7 int total[n + 5];//存该等级有多少颗星星
     8 
     9 void add(int k)
    10 {
    11     while (k <= n)
    12     {
    13         c[k] ++;
    14         k += (k&(-k));
    15     }
    16 }
    17 
    18 int sum(int k)//算它是几等级
    19 {
    20     int s = 0;
    21     while (k > 0)//算前k的和,就是它的等级
    22     {
    23         s += c[k];
    24         k -=(k&(-k));
    25     }
    26     return s;
    27 }
    28 
    29 int main()
    30 {
    31     int i, j, x, y, nn;
    32     while (scanf("%d", &nn) != EOF)
    33     {
    34         memset(c, 0, sizeof(c));
    35         memset(total, 0, sizeof(total));
    36         for (i = 1; i <= nn; i++)
    37         {
    38             scanf("%d%d", &x, &y);//由于坐标x可能为0,因此输入坐标要+1,不然会超时0&(-0)=0;
    39             add(x + 1);
    40             total[sum(x + 1) - 1]++;//减回去
    41             /*x++;
    42             total[sum(x)]++;
    43             add(x);*/
    44         }
    45         for (i = 0; i < nn; i++)
    46             printf("%d
    ", total[i]);
    47     }
    48     return 0;
    49 }
  • 相关阅读:
    Mysql 解压安装
    线程进程池,协程,IO模型
    并发编程 线程
    并发编程 进程
    socket 套接字
    网络编程
    面向对象(反射,元类) 排序方法
    面向对象(多态,类方法,魔法方法)
    Day22 面向对象(继承封装)
    php一行代码获取本周一,本周日,上周一,上周日,本月一日,本月最后一日,上月一日,上月最后一日日期 转
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/13271159.html
Copyright © 2011-2022 走看看