Stars
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6676 Accepted Submission(s): 2659
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.
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.
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.
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
1 1
5 1
7 1
3 3
5 5
Sample Output
1
2
1
1
0
2
1
1
0
题解:给出n个点的坐标,定义一个点的等级为这个点的左下方的点的个数,输出等级为0的点的个数,等级为1的点的个数,一直输出到等级为n-1的点的个数
做这个题真是曲折啊,没读清题意就开始写,首先Stars are listed in ascending order of Y coordinate,Stars with equal Y coordinates are listed in ascending order of X coordinate没看到,人家题上给的数据本来就是以y轴升序排列的,自己还傻逼的排序,另外The output should contain N lines这句没看见,自己是以空格输出。。。然后就是思路了,自己竟然想着把点都存在一个数组里面,再用二分去找点的个数。。。。好麻烦的思路,直接ans【sum】++不就妥了么。。。。最后还是超时,怎么办呐,极限数据不对,哪个数据?0呗,如果是0点,update就死循环了。。。
可能麻烦点,但自己的代码还是对的,以后写代码还是要看清题,找到最简单的思路,写的也好写点;
简单代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
//#define LOCAL
const int MAXM=;
const int MAXN=;
/*struct Node{
int x,y;
};*/
int tree[MAXM+],ans[MAXN];//,die[MAXN];
//Node dt[MAXN];
/*int cmp(Node a,Node b){
if(a.y!=b.y){
return a.y<b.y;
}
else return a.x<b.x;
}*/
int lowbit(int x){
return x&(-x);
}
void update(int x){
while(x<=MAXM){
tree[x]++;
x+=lowbit(x);
}
}
int SUM(int x){
int temp=;
while(x>){
temp+=tree[x];
x-=lowbit(x);
}
return temp;
}
/*int erfen(int l,int r,int x){
int mid;
while(l<=r){
mid=(l+r)>>1;
if(die[mid]>x)r=mid-1;
else l=mid+1;
}
return l;
}*/
int main(){
/* #ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif*/
int N;
int a;
while(~scanf("%d",&N)){
memset(tree,,sizeof(tree));
memset(ans,,sizeof(ans));
for(int i=;i<N;i++){
scanf("%d%*d",&a);
a++;
ans[SUM(a)]++;update(a);
// scanf("%d%d",&dt[i].x,&dt[i].y);
}
//sort(dt,dt+N,cmp);
/* for(int i=0;i<N;i++){
die[i]=SUM(dt[i].x);
update(dt[i].x);
}
sort(die,die+N);
for(int i=0;i<N;i++){
int temp=0,t=erfen(0,N-1,i);
// printf("%d**%d**%d\n",die[t-1],i,t);
temp=t;
ans[i]=temp;
// printf("%d\n",ans[i]);
}*/
for(int i=;i<N;i++){
/* if(i){
printf(" ");
}*/
// printf("%d",i==0?ans[i]:ans[i]-ans[i-1]);
printf("%d\n",ans[i]);
} }
return ;
}
刚开始写的也ac了:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
//#define LOCAL
const int MAXM=;
const int MAXN=;
struct Node{
int x,y;
};
int tree[MAXM+],die[MAXN],ans[MAXN];
Node dt[MAXN];
/*int cmp(Node a,Node b){
if(a.y!=b.y){
return a.y<b.y;
}
else return a.x<b.x;
}*/
int lowbit(int x){
return x&(-x);
}
void update(int x){
while(x<=MAXM){
tree[x]++;
x+=lowbit(x);
}
}
int SUM(int x){
int temp=;
while(x){
temp+=tree[x];
x-=lowbit(x);
}
return temp;
}
int main(){
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
int N;
while(~scanf("%d",&N)){
for(int i=;i<N;i++){
scanf("%d%d",&dt[i].x,&dt[i].y);
dt[i].x++;
}
//sort(dt,dt+N,cmp);
memset(tree,,sizeof(tree));
for(int i=;i<N;i++){
die[i]=SUM(dt[i].x);
update(dt[i].x);
}
sort(die,die+N);
for(int i=,j=;i<N;i++){
int temp=;
while(die[j]==i){
j++;temp++;
}
ans[i]=temp;
}
for(int i=;i<N;i++){
//if(i)printf(" ");
printf("%d\n",ans[i]);
}
// puts("");
}
return ;
}
刚开始发现超时改成二分找了,更快点的最后也ac了:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
//#define LOCAL
const int MAXM=;
const int MAXN=;
struct Node{
int x,y;
};
int tree[MAXM+],die[MAXN],ans[MAXN];
Node dt[MAXN];
/*int cmp(Node a,Node b){
if(a.y!=b.y){
return a.y<b.y;
}
else return a.x<b.x;
}*/
int lowbit(int x){
return x&(-x);
}
void update(int x){
while(x<=MAXM){
tree[x]++;
x+=lowbit(x);
}
}
int SUM(int x){
int temp=;
while(x){
temp+=tree[x];
x-=lowbit(x);
}
return temp;
}
int erfen(int l,int r,int x){
int mid;
while(l<=r){
mid=(l+r)>>;
if(die[mid]>x)r=mid-;
else l=mid+;
}
return l;
}
int main(){
#ifdef LOCAL
freopen("data.in","r",stdin);
freopen("data.out","w",stdout);
#endif
int N;
while(~scanf("%d",&N)){
for(int i=;i<N;i++){
scanf("%d%d",&dt[i].x,&dt[i].y);
dt[i].x++;
}
//sort(dt,dt+N,cmp);
memset(tree,,sizeof(tree));
for(int i=;i<N;i++){
die[i]=SUM(dt[i].x);
update(dt[i].x);
}
sort(die,die+N);
for(int i=;i<N;i++){
int temp=,t=erfen(,N-,i);
// printf("%d**%d**%d\n",die[t-1],i,t);
temp=t;
ans[i]=temp;
// printf("%d\n",ans[i]);
}
for(int i=;i<N;i++){
//if(i)printf(" ");
printf("%d\n",i==?ans[i]:ans[i]-ans[i-]);
}
// puts("");
}
return ;
}
线段树来一发:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
#define L tree[root].l
#define R tree[root].r
#define S tree[root].sum
#define lson root<<1,l,mid
#define rson root<<1|1,mid+1,r struct Node{
int l,r,sum;
};
Node tree[MAXN<<];
int res[MAXN>>];
int ans;
void build(int root,int l,int r){
L=l;R=r;
S=;
if(l==r)return;
int mid=(l+r)>>;
build(lson);
build(rson);
}
void update(int root,int x){
if(L==x&&R==x){
S++;
return;
}
int mid=(L+R)>>;
if(mid>=x)update(root<<,x);
else update(root<<|,x);
S=tree[root<<].sum+tree[root<<|].sum;
}
void query(int root,int x){
if(L>=&&R<=x){
ans+=S;
return;
}
int mid=(L+R)>>;
if(mid>=)query(root<<,x);
if(mid<x)query(root<<|,x);
}
int main(){
int N,a;
while(~scanf("%d",&N)){
build(,,MAXN-);
memset(res,,sizeof(res));
for(int i=;i<N;i++){
scanf("%d%*d",&a);
a++;
ans=;
query(,a);
res[ans]++;
update(,a);
}
for(int i=;i<N;i++)printf("%d\n",res[i]);
}
return ;
}