您的位置:首页 > 产品设计 > UI/UE

URAL 1026. Questions and answers

2007-11-04 21:18 525 查看
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, 0, sizeof(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] == 0) continue;




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]);






}




}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: