hdoj 1541 Stars
2015-11-28 19:57
417 查看
Stars
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6874 Accepted Submission(s): 2722
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
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.
[align=left]Sample Input[/align]
5 1 1 5 1 7 1 3 3 5 5
[align=left]Sample Output[/align]
1 2 1 1 0
思路:找出每个星星的左下方包括正下方和正左方有多少个星星,星星的数量代表它的级数,再算出从0级到n-1级的个数,依次输出。
代码:
#include<stdio.h> #include<string.h> #include<algorithm> #define M 33000 using namespace std; int sum[M*4]; int pos[M]; void pushup(int node) { sum[node]=sum[node*2]+sum[node*2+1]; } void build(int l,int r,int node) { sum[node]=0; if(l==r) return ; int mid=(l+r)/2; build(l,mid,node*2); build(mid+1,r,node*2+1); pushup(node); } int query(int l,int r,int L,int R,int node) { if(L<=l&&R>=r) return sum[node]; int mid=(l+r)/2; int ans=0; if(L<=mid) ans+=query(l,mid,L,R,node*2); if(R>mid) ans+=query(mid+1,r,L,R,node*2+1); return ans; } void update(int l,int r,int L,int node) { if(l==r) { sum[node]+=1; return ; } int mid=(l+r)/2; if(L<=mid) update(l,mid,L,node*2); else update(mid+1,r,L,node*2+1); pushup(node); } int main() { int n,i,x,y,a; while(scanf("%d",&n)!=EOF) { build(1,M,1); memset(pos,0,sizeof(pos)); for(i=0;i<n;i++) { scanf("%d%d",&x,&y); a=query(1,M,1,x+1,1); pos[a]++; update(1,M,x+1,1); } for(i=0;i<n;i++) { printf("%d\n",pos[i]); } } return 0; }
相关文章推荐
- viewpager的入门使用(1)
- UVA 12575 Sin Cos Problem
- 用jdbc返回集合数据时的一些小问题
- 事物处理
- 滤波电容、去耦电容、旁路电容作用
- Android调用系统相机方法及保存照片方式
- php PDO调用带有out参数的存储过程(原创)
- centos7下使用yum安装mysql
- [LeetCode]002-AddTwoNumber
- CentOS修改系统时间
- c++11,线程池之二--有等待线程池中任务完成功能的线程池
- UIImagePickerController详解
- 随意细解:UI -- 标签视图控制器
- ContentProvider实现数据共享1
- hdoj 第几天?
- hdu 2602 Bone Collector(01背包)
- 朴素贝叶斯算法理解
- 连接String与Int(转)
- cocoapods的使用
- 【Python】计算文件MD5 和 SHA1