URAL 1028. Stars

problem url: http://acm.timus.ru/problem.aspx?space=1&num=1028

Got two methods here. One uses hash table which will give you O(Nsqrt(N)) ,  another uses segment tree(binary search..) which gives you O(Nlog(N))

**hash table approach:

#include  < cstdio >
#include 
< cstddef >
#include 
< iostream >
#include 
< math.h >

#define  MAX_XY 32000
#define  MAX_COUNT 15000

using   namespace  std;

void  main()
{
int count = 0;
int i=0;
int sx,sy;

int bSize =0 ;  //bucket size
int bNum = 0;    //total bucket number

int p[MAX_XY+1= {0};

int* lList = NULL;
int *= NULL;

scanf(
"%d"&count);

bSize 
= (int)sqrt((double)MAX_XY);
bNum 
= (MAX_XY+1/ bSize + 1

= (int*)malloc(sizeof(int)*bNum);
memset(b, 
0sizeof(int)*bNum);

lList 
= (int*)malloc(sizeof(int* count);
memset(lList, 
0sizeof(int)*count);

for(i =0;i<count;++i)
{
int bCur = 0;//current bucket index
int lCur = 0;//current level

scanf(
"%d %d"&sx, &sy);

bCur 
= sx / bSize;

////calculate level
int j = 0;

for(j =0; j<bCur;++j)
{
    lCur 
+= b[j];
}


for(j= bSize*bCur; j<=sx; ++j)
{
    lCur 
+= p[j];
}


lList[lCur]
++;
///

b[bCur]
++;
p[sx]
++;
}


//print out result
for(i=0;i<count;++i)
{
    printf(
"%d ",lList[i]);
}


free(b);
free(lList);
}

 **segment tree approach:

#include  < cstdio >
#include 
< cstddef >
#include 
< iostream >
#include 
< math.h >

#define  MAX_XY 32000
#define  MAX_COUNT 15000

int  vals[MAX_XY  * 4   + 1 =   {0} ;
int  lList[MAX_COUNT];

using   namespace  std;

void  insertstar( int  p,  int  b,  int  e,  int  x)
{
    
int m = (b+e) >> 1;

    
if(e != b)
    
{
        
if(x <= m)
        
{
            insertstar(p
<<1, b, m, x);
            
        }
else
        
{
            insertstar((p
<<1)+1, m+1, e, x);
        }

    }

    vals[p] 
++;
}


int  getlevel( int  p,  int  b,  int  e,  int  x)
{
    
int m = (b+e) >> 1;

    
if( e == b)
    
{
        
return vals[p];
    }

    
else
    
{
        
if( x == e)
        
{
            
return vals[p];
        }

        
else if(x > m)
        
{    
            
return getlevel(p<<1, b, m, m) + getlevel((p<<1)+1,m+1, e, x);
        }
else
        
{
            
return getlevel(p<<1, b, m, x);
        }

    }

}


void  main()
{
int count = 0;
int i=0;
int sx,sy;

//get star number
//cin>>count;
scanf("%d"&count);

memset(lList, 
0sizeof(int)*MAX_COUNT);

for(i =0;i<count;++i)
{
int lCur = 0;//current level

//cin>>sx>>sy;
scanf("%d %d"&sx, &sy);

//get level
lCur = getlevel(10, MAX_XY, sx);

lList[lCur]
++;

insertstar(
1,0,MAX_XY,sx); 
///
}


for(i=0;i<count;++i)
{
    printf(
"%d ",lList[i]);
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值