Hdu 4557 If You Are the One (non-violent, Balance Tree treap)

Source: Internet
Author: User

If You Are the One
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission (s): 98 Accepted Submission (s): 60

 

Problem Description
As a member of the 2013 graduates in 6.99 million, James lost his job on the day of graduation due to the macroeconomic downturn!
After a hard job search process, James can understand the difficulty of employment for graduates. Therefore, he is now preparing to create a job-seeking intermediary company dedicated to IT talents-If You Are the One, HR Development Co., Ltd.
Based on work requirements, James evaluated a comprehensive competence value for each student based on the resume description of the job-seeking student. The competency value is a positive integer smaller than or equal to 20. The higher the value, the higher the ability. When a company tries to recruit IT staff (only one person per time), IT needs to propose a minimum requirement for comprehensive capabilities. If there are qualified talents in the talent pool, the recruitment will be successful. Of course, if multiple students meet the needs of recruitment companies at the same time, James always gives priority to the talents with low competence in view of the scarcity of high-capacity talents; if more than one person meets the requirements, James recommends the first student to apply for a job.
It should be noted that at the beginning, the company's talent pool was empty, and a student could only sign a contract with a company. If the recommendation is successful, the student information needs to be deleted from the talent pool.

 


Input
The first line of the input data is a positive integer T (1 <= T <= 20), indicating that there are T groups of test data;
The first line of each group of test data is an integer n (0 <= n <= 1000), indicating that n events occurred successively by time. In the next n rows, each line describes an event. For an event, the first string is "Add" or "Find", where "Add" indicates that a student has joined the talent pool, and "Find" indicates that a company wants to recruit a person.
If the string is "Add", there will be a string s and a number d separated by spaces, indicating the student's name and comprehensive ability value, respectively, the name consists of lower-case letters, which are not empty and cannot exceed 15 characters. If the string is "Find", a number is followed to indicate the minimum requirements of the recruitment company for comprehensive talent capabilities.

 


Output
For each group of test data, the first line outputs "Case # c:" (excluding quotation marks)
C is the number of test data groups, starting from 1.
Then, n rows are output, indicating the result of n events.
If this event is about adding talent information to the database, please output the number of people in the talent pool after adding the information;
If this event is for enterprise recruitment, please output the name of the talent to be hired. If no talent meets the requirements, please output "WAIT ..."

 


Sample Input
1
5
Add lcy 1
Add lyd 19
Find 11
Find 13
Add zxs 10


Sample Output
Case #1:
1
2
Lyd
WAIT...
2


Source
 
 


Recommend
Liuyiding
[Cpp]
// Treap Balance Tree Solution
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <cstdlib>
# Include <ctime>
# Include <string>
Using namespace std;
# Define N 10010
Int sum = 0;
/*************************************** *******/
# Define nil 0
Struct Node {
Int k, w, l, r;
String s;
Void init (string str, int v ){
K = v; l = r = nil; w = rand ();
S = str;
}
} T [N];
Class Treap {
Public:
Int root, w;
Treap () {root = w = nil; T [0]. init ("", 0); T [0]. w = 0x3fffffff ;}
Void cut (int x, int & l, int & r, int k) {// x split into l, r
If (x = nil) {l = r = nil; return ;}
If (T [x]. k <= k ){
L = x;
Cut (T [x]. r, T [l]. r, r, k );
} Else {
R = x;
Cut (T [x]. l, l, T [r]. l, k );
}
}
Void merge (int & x, int l, int r) {// l r merge to x
If (l = nil | r = nil) {x = (l = nil? R: l); return ;}
If (T [l]. w <T [r]. w ){
Merge (T [l]. r, T [l]. r, r );
X = l;
} Else {
Merge (T [r]. l, l, T [r]. l );
X = r;
}
}
Void insert (string s, int k) {// insert k
Int x = ++ w, l, r;
T [x]. init (s, k );
Cut (root, l, r, k );
Merge (l, l, x );
Merge (root, l, r );
}
Void find (int k) {// locate and delete the k Precursor
Int x, l, r, f = nil;
Bool B = false;
Cut (root, l, r, k-1 );
If (r! = Nil ){
Sum --;
B = true;
X = r;
While (T [x]. l! = Nil) f = x, x = T [x]. l;
Cout <T [x]. s <endl;
If (f = nil) merge (r, T [x]. l, T [x]. r );
Else merge (T [f]. l, T [x]. l, T [x]. r); // Delete x
} Else printf ("WAIT... \ n ");
Merge (root, l, r );
}
Void dfs (int x ){
If (x = nil) return;
Dfs (T [x]. l );
Cout <T [x]. k <"";
Dfs (T [x]. r );
}
};
/*************************************** ********/
Int main (){
Int I, j, k, T, n, t = 1;
Char ch [50];
Scanf ("% d", & T );
While (T --){
Treap treap;
Scanf ("% d", & n );
Printf ("Case # % d: \ n", t ++ );
Sum = 0;
While (n --){

Scanf ("% s", ch );
Switch (ch [0]) {
Case 'A ':
Scanf ("% s % d", ch, & k );
Treap. insert (ch, k );
Printf ("% d \ n", ++ sum );
Break;
Case 'F ':
Scanf ("% d", & k );
Treap. find (k );
Break;
}
}
}
Return 0;
}

// Treap Balance Tree Solution
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <cstdlib>
# Include <ctime>
# Include <string>
Using namespace std;
# Define N 10010
Int sum = 0;
/*************************************** *******/
# Define nil 0
Struct Node {
Int k, w, l, r;
String s;
Void init (string str, int v ){
K = v; l = r = nil; w = rand ();
S = str;
}
} T [N];
Class Treap {
Public:
Int root, w;
Treap () {root = w = nil; T [0]. init ("", 0); T [0]. w = 0x3fffffff ;}
Void cut (int x, int & l, int & r, int k) {// x split into l, r
If (x = nil) {l = r = nil; return ;}
If (T [x]. k <= k ){
L = x;
Cut (T [x]. r, T [l]. r, r, k );
} Else {
R = x;
Cut (T [x]. l, l, T [r]. l, k );
}
}
Void merge (int & x, int l, int r) {// l r merge to x
If (l = nil | r = nil) {x = (l = nil? R: l); return ;}
If (T [l]. w <T [r]. w ){
Merge (T [l]. r, T [l]. r, r );
X = l;
} Else {
Merge (T [r]. l, l, T [r]. l );
X = r;
}
}
Void insert (string s, int k) {// insert k
Int x = ++ w, l, r;
T [x]. init (s, k );
Cut (root, l, r, k );
Merge (l, l, x );
Merge (root, l, r );
}
Void find (int k) {// locate and delete the k Precursor
Int x, l, r, f = nil;
Bool B = false;
Cut (root, l, r, k-1 );
If (r! = Nil ){
Sum --;
B = true;
X = r;
While (T [x]. l! = Nil) f = x, x = T [x]. l;
Cout <T [x]. s <endl;
If (f = nil) merge (r, T [x]. l, T [x]. r );
Else merge (T [f]. l, T [x]. l, T [x]. r); // Delete x
} Else printf ("WAIT... \ n ");
Merge (root, l, r );
}
Void dfs (int x ){
If (x = nil) return;
Dfs (T [x]. l );
Cout <T [x]. k <"";
Dfs (T [x]. r );
}
};
/*************************************** ********/
Int main (){
Int I, j, k, T, n, t = 1;
Char ch [50];
Scanf ("% d", & T );
While (T --){
Treap treap;
Scanf ("% d", & n );
Printf ("Case # % d: \ n", t ++ );
Sum = 0;
While (n --){

Scanf ("% s", ch );
Switch (ch [0]) {
Case 'A ':
Scanf ("% s % d", ch, & k );
Treap. insert (ch, k );
Printf ("% d \ n", ++ sum );
Break;
Case 'F ':
Scanf ("% d", & k );
Treap. find (k );
Break;
}
}
}
Return 0;
}


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.