HNUOJ_10069

来源:互联网 发布:淘宝买aj的正品店 编辑:程序博客网 时间:2024/04/28 03:27

星星的等级Time Limit: 3000ms, Special Time Limit:6000ms, Memory Limit:65536KBTotal submit users: 93, Accepted users: 43Problem 10069 : No special judgementProblem description天空中有很多星星,我们把天空看作一个二维的平面,则可以将每一个星星按照物理位置给一个坐标值(x,y)。
并且定义两个星星a,b的等级高低关系可以有如下约束来确定:
如果 Xa <= Xb 且 Ya >= Yb ,则认为星星a的等级比b高(若两个星星在同一位置,则认为它们等级相同)。
现在的问题是:假定天空中有N颗星星,并且知道每一颗星星的坐标。你能够很快计算出来对于每一颗星星,有多少星星等级比他高吗?
我们希望你的计算速度越快越好,同时不希望你用暴力搜索的方法解决该问题,因为天空中的星星很多很多,以现在计算机的速度也要很久的,我们只取了一小部分都有100000个了,暂时就计算这么多。 
Input输入将有很多组测试数据,每个测试数据都是以一个整数N(1 <= N <= 100000)开始, 随后有N行, 每行2个整数X, Y (1<=X,Y<=10^9), 则是N颗行星的位置坐标。 一直计算到输入结束为止。
Output对于每一组测试数据,输出一行,由N个数字组成,按照输入的星星的顺序,每一个数字表示有多少星星等级大于该星星,数字之间有且只有一个空格,行尾请不要输出多余的空格。
Sample Input

31 20 33 4
Sample Output
1 0 0
Problem SourceHNU


一个比较类似的问题就是POJ_2352 Stars

Stars 


Time Limit: 1000MS   

Memory Limit: 65536K 

Total Submissions: 22464   

Accepted: 9787 


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  

1 1 

5 1 

7 1 

3 3

5 5 


Sample Output  


Hint  

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed. 


Source  

Ural Collegiate Programming Contest 1999       


可以用线段数做,也可以用树状数组。 还是感觉树状数组比较简单一些。

/*POJ 2352 Stars就是求每个小星星左小角的星星的个数。坐标按照Y升序,Y相同X升序的顺序给出由于y轴已经排好序,可以按照x坐标建立一维树状数组*/#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>using namespace std;const int MAXN=15010;const int MAXX=32010;int c[MAXX];//树状数组的c数组int cnt[MAXN];//统计结果int lowbit(int x){    return x&(-x);}void add(int i,int val){    while(i<=MAXX)    {        c[i]+=val;        i+=lowbit(i);    }}int sum(int i){    int s=0;    while(i>0)    {        s+=c[i];        i-=lowbit(i);    }    return s;}int main(){    //freopen("in.txt","r",stdin);    //freopen("out.txt","w",stdout);    int n;    int x,y;    while(scanf("%d",&n)!=EOF)    {        memset(c,0,sizeof(c));        memset(cnt,0,sizeof(cnt));        for(int i=0;i<n;i++)        {            scanf("%d%d",&x,&y);            //加入x+1,是为了避免0,X是可能为0的            int temp=sum(x+1);            cnt[temp]++;            add(x+1,1);        }        for(int i=0;i<n;i++)         printf("%d\n",cnt[i]);    }    return 0;}


思路:先对星星按照x的值由小到大排序,x相等的按照y排序(就是在插入y的时候把x相等,y较小的先插入)。然后我们在插入y的值,因为x已经排好序了,我们要求得就是y之前有多少数已经插入了。这样求得的就是这颗星的等级。

补充:用数状数组时,我们其实虚构了一个数组B(初始元素都为0,表示没有数插入,有一个数加入就加1,我们求得就是插入的数的个数的总和)。

#include<iostream>#include<stdio.h>#include<stdlib.h>#include<algorithm>#include<cstring>using namespace std;const int NUM = 15005;const int MAXSIZE = 32005;struct node{int x;int y;friend bool operator<(const node& e1,const node& e2){if(e1.x == e2.x) return e1.y<e2.y;return e1.x<e2.x;}};node data[NUM];int count1[MAXSIZE];int degree[NUM];int n;int max1;int lowbit(int l){return l&(-l);}void update(int l){while(l<=max1){count1[l]+=1;l += lowbit(l);}}int getSum(int l){int sum = 0;while(l>0){sum+=count1[l];l-=lowbit(l);}return sum;}int main(){scanf("%d",&n);max1 = 0;memset(count1,0,sizeof(int)*MAXSIZE);memset(degree,0,sizeof(int)*NUM);for(int i =1;i<=n;i++){scanf("%d%d",&data[i].x,&data[i].y);data[i].y+=1;if(data[i].y>max1) max1 = data[i].y;}sort(data+1,data+n+1);for(int i =1;i<=n;i++){degree[getSum(data[i].y)]++;update(data[i].y);}for(int i = 0;i<=n-1;i++){cout<<degree[i]<<endl;}return 0;}

结论:树状数组的实现,空间复杂度低,编程复杂度低,容易扩展到多维情况,是一个可以很高效的进行区间统计的数据结构。

但它的适用范围小,对其可以进行的运算也有限制。比如说如果每次修改的是某一段区间,而非一个元素,维护代价还是相当高的。


关于树状数组

http://lib.csdn.net/article/c/41020

http://blog.csdn.net/int64ago/article/details/7429868

http://www.cnblogs.com/iwantstrong/p/5925595.html


树状数组

#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int MAXN = 32005;int c[MAXN],level[MAXN],n;int lowbit(int x){return x & (-x);}// 求前n项的和int sum(int n){    int sum = 0;    while(n > 0){        sum += c[n];        n -= lowbit(n);    }    return sum;}// 增加某个元素的大小void add(int x){    while(x <= MAXN){        ++c[x];        x += lowbit(x);    }}int main(){    int n,x,y;    while(~scanf("%d",&n)){        memset(level, 0, sizeof(level));        memset(c, 0, sizeof(c));        for(int i=0; i<n; ++i) {            scanf("%d%d",&x,&y);            ++x;            level[sum(x)]++;            add(x);        }        for(int i=0; i<n; ++i)            printf("%d\n",level[i]);    }    return 0;}


线段树

#include<iostream>#include<cstdio>#include<cstring>#define mid ((left+right)>>1)#define lson rt<<1,left,mid#define rson rt<<1|1,mid+1,rightusing namespace std;const int MAXN = 32005;int sum[MAXN<<2],level[MAXN<<2];void update(int rt,int left,int right,int data){    ++sum[rt];    if(left==right) return;    if(data <= mid) update(lson,data);    else update(rson,data);}int query(int rt,int left,int right,int l,int r){    if(left==l && right==r) {        return sum[rt];    }    int m = mid;    if(r <= m) return query(lson,l,r);    else if(l > m) return query(rson,l,r);    else return query(lson,l,m)+query(rson,m+1,r);}int main(){    int n,x,y;    while(~scanf("%d",&n)){        memset(sum, 0, sizeof(sum));        memset(level, 0, sizeof(level));        for(int i=0; i<n; ++i){            scanf("%d%d",&x,&y);            ++x;            ++level[query(1,1,MAXN,1,x)];            update(1,1,MAXN,x);        }        for(int i=0; i<n; ++i)            printf("%d\n",level[i]);    }    return 0;}



0 0
原创粉丝点击