URAL 1026. Questions and answers

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

At first, I assumed this is a sorting question and spent much time on sorting the input record list(I've tried both BST and BBST(AVL)) and they can got AC but doesn't work well. After that I got some hints from my friend Mr Luan which gave the hashtable based approach which quite suit the problem here and it beat the time limit. Apparently that hashtable is the primary actor here and it gives O(N+k) time complexity:

Here are all the codes:

#use Binary Sort Tree

/* using Binary Sort Tree 

time: 0.375 

memory: 4567 KB 
*/


#include 
< cstdio >
#include 
< iostream >

#define  MAXNUM 100000
#define  MAXVAL 5000
#define  MAXQRY 100


using   namespace  std;


typedef 
struct  BSTNode
{
    
int value;
    
struct BSTNode* lchild;
    
struct BSTNode* rchild;
}
 BSTNode;


void  InsertNode(BSTNode **  root,  int  val);
void  Traverse(BSTNode *  root);


// global variables
int  records[MAXNUM + 1 ];

BSTNode
*  sort_tree  =  NULL;

int  index  =   0 ;

void  main()
{
int i=0;
int rcount = 0;
int qcount = 0;

int curval = 0;


//read records and sort them through BST
scanf("%d"&rcount);

for(i=0;i<rcount;++i)
{
scanf(
"%d"&curval);

InsertNode(
&sort_tree, curval);

}



i
=0;


index 
= 0;
//tranverse the tree to get the records in order
Traverse(sort_tree);


char str[4];
scanf(
"%s",str);

//print out query results
scanf("%d"&qcount);


for(i =0;i<qcount;++i)
{
    scanf(
"%d"&curval);
    printf(
"%d ", records[curval]);
}


}



void  InsertNode(BSTNode **  root,  int  val)
{
    
if(*root == NULL)
    
{
        BSTNode
* temp = (BSTNode*)malloc(sizeof(BSTNode));

        temp
->value = val;
        temp
->lchild = NULL;
        temp
->rchild = NULL;

        
*root = temp;
    }

    
else if(val < (*root)->value)
    
{
        InsertNode(
&((*root)->lchild), val);
    }
else
    
{
        InsertNode(
&((*root)->rchild), val);
    }


}


void  Traverse(BSTNode *  root)
{
    
if(root->lchild != NULL)
    
{
        Traverse(root
->lchild);
    }


    
//store current value
    
//printf("%d ", root->value);
    records[++index] = root->value;

    
if(root->rchild != NULL)
    
{
        Traverse(root
->rchild);
    }

}

#use BBST(AVL TREE)

 

/*
using Balanced(AVL) Binary Sort Tree

time:0.406s
memory: 4567 KB 

*/



#include 
< cstdio >
#include 
< iostream >

#define  MAXNUM 100000
#define  MAXVAL 5000
#define  MAXQRY 100


#define  LH +1
#define  EH 0
#define  RH -1


using   namespace  std;


typedef 
struct  BSTNode
{
    
int value;
    
int bf;//balance factor
    struct BSTNode* lchild;
    
struct BSTNode* rchild;
}
 BSTNode;


void  InsertNode(BSTNode **  root,  int  val,  bool *  taller);

void  L_Rotate(BSTNode **  root);
void  R_Rotate(BSTNode **  root);

void  Left_Balance(BSTNode **  root);
void  Right_Balance(BSTNode **  root);

void  Traverse(BSTNode *  root);





// global variables
int  records[MAXNUM + 1 ];

BSTNode
*  sort_tree  =  NULL;

int  index  =   0 ;

void  main()
{
int i=0;
int rcount = 0;
int qcount = 0;

int curval = 0;


//read records and sort them through BST
scanf("%d"&rcount);

for(i=0;i<rcount;++i)
{
scanf(
"%d"&curval);

bool taller = false;

InsertNode(
&sort_tree, curval, &taller);

}



i
=0;


index 
= 0;
//tranverse the tree to get the records in order
Traverse(sort_tree);


char str[4];
scanf(
"%s",str);

//print out query results
scanf("%d"&qcount);


for(i =0;i<qcount;++i)
{
    scanf(
"%d"&curval);
    printf(
"%d ", records[curval]);
}






}



void  InsertNode(BSTNode **  root,  int  val,  bool *  taller)
{
    
if(*root == NULL)
    
{
        BSTNode
* temp = (BSTNode*)malloc(sizeof(BSTNode));

        temp
->value = val;
        temp
->lchild = NULL;
        temp
->rchild = NULL;

        
*root = temp;


        
*taller = true;
    }

    
else if(val < (*root)->value)
    
{
        InsertNode(
&((*root)->lchild), val, taller);

        
if(*taller)
        
{
            
switch((*root)->bf)
            
{
            
case LH:

                Left_Balance(root);;
                
*taller = false;
                
break;

            
case EH:

                (
*root)->bf = LH;
                
*taller = true;
                
break;

            
case RH:

                (
*root)->bf = EH;
                
*taller = false;
                
break;
            }


        }

    }
else
    
{
        InsertNode(
&((*root)->rchild), val,taller);

        
if(*taller)
        
{
            
switch((*root)->bf)
            
{
            
case LH:

                (
*root)->bf = EH;
                
*taller = false;
                
break;

            
case EH:

                (
*root)->bf = RH;
                
*taller = true;
                
break;

            
case RH:

                Right_Balance(root);
                
*taller = false;
                
break;
            }

        }

    }


}




void  L_Rotate(BSTNode **  root)
{
    BSTNode
* rc = (*root)->rchild;

    (
*root)->rchild = rc->lchild;
    rc
->lchild = *root;
    
*root = rc;
}


void  R_Rotate(BSTNode **  root)
{
    BSTNode
* lc = (*root)->lchild;

    (
*root)->lchild = lc->rchild;
    lc
->rchild  = *root;
    
*root = lc;

}



void  Left_Balance(BSTNode **  root)
{
    BSTNode
* lc =  (*root)->lchild;

    
switch(lc->bf)
    
{
    
case LH:
        
        (
*root)->bf = lc->bf = EH;
        R_Rotate(root);

        
break;
    
case RH:

        BSTNode
* rc = lc->rchild;

        
switch(rc->bf)
        
{
        
case LH:
            (
*root)->bf = RH;
            lc
->bf = EH;
            
break;
        
case EH:
            (
*root)->bf = lc->bf = EH;
            
break;
        
case RH:
            (
*root)->bf = EH;
            lc
->bf = LH;
            
break;
        }


        rc
->bf = EH;
        L_Rotate(
&((*root)->lchild));
        R_Rotate(root);
        
break;
    }

}


void  Right_Balance(BSTNode **  root)
{
    BSTNode
* rc = (*root)->rchild;

    
switch(rc->bf)
    
{
    
case RH:

        (
*root)->bf = rc->bf = EH;
        L_Rotate(root);
        
break;
    
case LH:

        BSTNode
* lc = rc->lchild;

        
switch(lc->bf)
        
{
        
case LH:
            rc
->bf =  RH;
            (
*root)->bf = EH;
            
break;
        
case EH:
            (
*root)->bf = rc->bf = EH;
            
break;
        
case RH:
            rc
->bf = EH;
            (
*root)->bf = LH;
            
break;
        }


        lc
->bf = EH;
        R_Rotate(
&((*root)->rchild));
        L_Rotate(root);

        
break;
    
    }



}



void  Traverse(BSTNode *  root)
{
    
if(root->lchild != NULL)
    
{
        Traverse(root
->lchild);
    }


    
//store current value
    
//printf("%d ", root->value);
    records[++index] = root->value;

    
if(root->rchild != NULL)
    
{
        Traverse(root
->rchild);
    }

}

#using hashtable for sorting approach:

 

#include  < cstdio >
#include 
< iostream >


using   namespace  std;


#define  MAXNUM 100000
#define  MAXVAL 5000
#define  MAXQRY 100

int  hash[MAXVAL + 1 ];     /* store number of each value within [1, 5000] */

int  records[MAXNUM + 1 ];   /*store the sorted number array*/

void  main()
{

int i =0, j= 0,k=0;
int curval = 0;
int count = 0;

memset(hash, 
0sizeof(int* (MAXVAL+1));

scanf(
"%d"&count);

for(i=0;i<count;++i)
{
scanf(
"%d"&curval);

hash[curval]
++;

}



/*sort the records*/
for(i=1,j=0;i<= MAXVAL;++i)
{
    
if(hash[i] == 0continue;

    k
=0;
    
while(k++ < hash[i])
    
{
        records[
++j] = i;
    }


}



char str[4];

scanf(
"%s", str);

scanf(
"%d"&count);

for(i=0;i<count;++i)
{
scanf(
"%d"&curval);

printf(
"%d ", records[curval]);


}


}


 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值