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]);
}
}
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]);
}
}
相关文章推荐
- ural 1026. Questions and Answers 查询
- URAL 1026 Questions and Answers
- URAL 1026 — Questions and Answers
- URAL 1026 - Questions and Answers(计数排序)
- ural 1026 Questions and Answers
- BNU Questions and answers
- 30 Java Exception Handling Interview Questions And Answers
- C# Interview Questions and Answers
- poj 2371 Questions and answers
- Questions and Answers During the First Session
- Pku acm 2371 Questions and answers 排序算法解题报告(三)----二叉查找数(BST)排序
- mysql interview questions and answers
- Some Essential JavaScript Questions And Answers(1)
- BNU Questions and answers
- 69 Spring Interview Questions and Answers – The ULTIMATE List--reference
- Core Java Interview Questions and Answers
- POJ 2371 Questions and answers(水~)
- BW delta queue (RSA7): Questions and answers
- Top 10 tricky Java interview questions and answers
- Struts2 Interview Questions and Answers