poj 2352 Stars
来源:互联网 发布:stata和python 编辑:程序博客网 时间:2024/06/06 09:51
Stars
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 33003 Accepted: 14422
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.
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
Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.
由于y坐标是升序的且坐标不重复,所以在星星A后面输入的星星的x,y坐标不可能都小于等于星星A。假如当前输入的星星为(3,3),易得我们只需要去找树状数组中小于等于3的值就可以了,即Getsum(3)。注意:A[i]表示x坐标为i的个数,C[]为A[]的树状数组,那么GetSum(i)就是序列中前i个元素的和,即x小于等于i的星星数。
本题还是一点要注意:星星坐标的输入可以是(0,0),所以我们把x坐标统一加1,(加1 后不影响所有所以数据间大小关系)然后用树状数组实现。
#include <map>#include <set>#include <stack>#include <queue>#include <cmath>#include <vector>#include <cstdio>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>using namespace std;#define INF 0x3f3f3f3f#define inf -0x3f3f3f3f#define FOR(a,b) for(int i = a ; i < b ; i++)#define mem0(a) memset(a,0,sizeof(a))#define mem1(a) memset(a,-1,sizeof(a))#define FOPENIN(IN) freopen(IN, "r", stdin)#define FOPENOUT(OUT) freopen(OUT, "w", stdout)const int NUM =35000;int lowbit(int x){ return x&(-x);}int N,lev[NUM],c[NUM];int Getsum(int x){ int ans = 0 ; while(x) { ans += c[x]; x -= lowbit(x); } return ans;}void add(int x){ while(x <= NUM){ c[x]+=1 ; x += lowbit(x); }}int main(){ while(scanf("%d",&N)!=EOF){ mem0(c); int x,y; for(int i = 0 ; i < N ; i++){ scanf("%d %d",&x,&y); x++; lev[Getsum(x)]++;//lev用来记录等级为i的个数 add(x); } for(int i = 0 ;i < N ; i++) printf("%d\n",lev[i]); } return 0;}
0 0
- POJ 2352 STARS
- POJ 2352 Stars
- Poj 2352 Stars
- poj 2352 Stars
- poj 2352 Stars
- poj 2352 Stars
- POJ 2352 Stars
- POJ 2352 Stars
- POJ-2352-Stars
- POJ 2352 Stars
- Poj 2352 Stars 题解
- poj 2352 stars
- POJ 2352 stars
- POJ 2352 - Stars
- POJ 2352 Stars
- POJ 2352 Stars
- poj 2352 Stars
- poj 2352 Stars
- BFS+状态压缩DP+二分枚举+TSP
- QT学习笔记2:创建对话框
- Aspose操作Excel和Word
- Clustering By Fast Search And Find Of Density Peaks -- Sci14发表的聚类算法
- set---Andy's First Dictionary
- poj 2352 Stars
- 数据库事务的概念
- distributor之Interrupt Clear-Enable Registers, GICD_ICENABLERn
- 代码分析工具understand
- 【第二章 线性表导图】
- 顺序表的查找
- hdu1372 Knight Moves(BFS)
- 【探索】QQ弹窗广 告解决方法
- 鸟哥的私房菜:Bash shell(三)-命令别名与历史指令