• Stars


    Stars

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


    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
     
    树状数组模板题,
    题意:题意中有暗示,输入坐标按Y增长,如果Y相等,就按X增长,
    所以用一维树状数组就可以解决.
     
     1 #include <iostream>
     2 #include <cstring>
     3 #define N 32005
     4 using namespace std;
     5 
     6 int tree[N],in[N],n;
     7 int lowbit(int x){
     8     return x&(-x);
     9 }
    10 
    11 void update(int x){
    12     while(x<N){
    13         tree[x]++;
    14         x+=lowbit(x);
    15     }
    16 }
    17 
    18 int add(int x){
    19     int sum = 0;
    20     while(x>0){
    21         sum+=tree[x];
    22         x -= lowbit(x);
    23     }
    24     return sum;
    25 }
    26 
    27 int main(){
    28     while(cin>>n){
    29         memset(tree,0,sizeof(tree));
    30         memset(in,0,sizeof(in));
    31         for(int i=0;i<n;i++){
    32             int x,y;
    33             cin>>x>>y;
    34             in[add(x+1)]++;
    35             update(x+1);
    36         }
    37         for(int i=0;i<n;i++){
    38             cout<<in[i]<<endl;
    39         }
    40     }
    41     return 0;
    42 }
     
  • 相关阅读:
    Java 第十一届 蓝桥杯 省模拟赛 洁净数
    Java 第十一届 蓝桥杯 省模拟赛 第十层的二叉树
    Java 第十一届 蓝桥杯 省模拟赛 第十层的二叉树
    Java 第十一届 蓝桥杯 省模拟赛 第十层的二叉树
    Java 第十一届 蓝桥杯 省模拟赛 70044与113148的最大公约数
    Java 第十一届 蓝桥杯 省模拟赛 70044与113148的最大公约数
    20. Valid Parentheses
    290. Word Pattern
    205. Isomorphic Strings
    71. Simplify Path
  • 原文地址:https://www.cnblogs.com/zllwxm123/p/9340733.html
Copyright © 2020-2023  润新知