非诚勿扰 - HDU 4557 - Treap入门

链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=4557


题目:

Problem Description

  作为2013年699万应届毕业生中的一员,由于宏观经济的不景气,小明在毕业当天就华丽丽地失业了!
  经历了千难万苦的求职过程,小明特别能理解毕业生的就业之难,所以,他现在准备创建一家专门针对IT人才的求职中介公司——非诚勿扰人力资源开发有限公司。
  基于工作的需要,小明根据求职学生的简历描述为每人评定了一个综合能力值,能力值是一个小于等于20的正整数,值越高表示能力越强。当有公司试图招聘IT人员的时候(每次只招聘1名),需要提出一个综合能力的最低需求,若人才库中有符合要求的人才,则一定能成功招聘。当然,若有多名学生同时满足招聘公司的需求,鉴于高能力人才的稀缺,小明总是优先把能力值低的人才推荐过去;如果依然有多名人员符合要求,则小明就把其中最早来求职的那位学生推荐过去。
  需要说明的是,刚开始的时候,公司的人才库为空,而且一名学生只能和一个企业签约,如果推荐成功,则该名学生的信息需要从人才库中删除。

Input

  输入数据的第一行是一个正整数T(1 <= T <= 20), 表示有T组测试数据;
  每组测试数据第一行是一个整数n(0 <= n <= 1000),表示按照时间先后发生了n次事件。接下来的n行,每行描述一次事件。对于一次事件,先是一个字符串”Add”或者”Find”,其中”Add”表示有一名学生加入了人才库,”Find”表示有企业想招聘一名人员。
如果字符串是”Add”,则后面将有一个字符串s和一个数字d,用空格隔开,分别表示该名学生的名字和综合能力值,名字由小写字母组成,不为空且长度不超过15;如果字符串是”Find”,则后面将有一个数字,表示招聘公司对人才综合能力的最低要求。

Output

对于每组测试数据,第一行输出”Case #c:”(不包含引号)
c是测试数据的组数,从1开始。
然后输出n行,表示n次事件的结果
如果本次事件是添加人才信息入库,则请输出加入该信息后,人才库内的人员数量;
如果本次事件是企业来招聘,则请输出将被录用的人才名字,如果没有人才符合要求,就请输出”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


思路:

  改一下搜索函数,记录下第一个大于等于题目所给的权就可以,记得能力值相同的情况下来得晚的位次就靠后。


实现:

#include <iostream>
#include <algorithm>
#include <set>
#include <string>
#include <vector>
#include <queue>
#include <map>
#include <stack>
#include <list>
#include <iomanip>
#include <functional>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>

#define read read()
#define edl putchar('\n')
#define clr(a, b) memset(a,b,sizeof a)

using namespace std;
int Size;
struct node {
    node *ch[2];
    string name;
    int v, fix, date, size;
    node(string name, int v, int date):name(name), v(v),fix(rand()), date(date), size(1) {
        ch[0] = ch[1] = NULL;
    }
    int compare(int v_, int date_) {
        if(v == v_) {
            if(date == date_) return -1;
            return date_ < date ? 0 : 1;
        }
        return v_ < v ? 0 : 1;
    }
    void maintain() {
        size = 1;
        if(ch[0] != NULL) size += ch[0]->size;
        if(ch[1] != NULL) size += ch[1]->size;
    }
}*root;

void rotate(node *&t, int d) {
    if(t == NULL) return ;
    node *tmp = t->ch[d^1];
    t->ch[d^1] = tmp->ch[d];
    tmp->ch[d] = t;
    t->maintain();
    tmp->maintain();
    t = tmp;
}

void insert(node *&t, string name, int v, int date) {
    if(t == NULL) {
        t = new node(name, v, date);
        Size++;
    }
    else {
        int d = t->compare(v, date);
        insert(t->ch[d], name, v, date);
        if(t->ch[d]->fix > t->fix) rotate(t, d^1);
    }
    t->maintain();
}

void remove(node *&t, int v, int date) {
    if(t == NULL) return ;
    int d = t->compare(v, date);
    if(d == -1) {
        Size--;
        node *tmp = t;
        if(t->ch[0] == NULL) {
            t = t->ch[1];
            delete tmp;
        } else if(t->ch[1] == NULL) {
            t = t->ch[0];
            delete tmp;
        } else {
            int k = t->ch[0]->fix > t->ch[1]->fix ? 1 : 0;
            rotate(t,k);
            remove(t->ch[k],v,date);
        }
    } else remove(t->ch[d], v, date);
    if(t != NULL) t->maintain();
}

void findsuc(int v, node *&ret) {
    node *t = root;
    ret = NULL;
    while(t != NULL) {
        if(t->v >= v) {
            ret = t;
            t = t->ch[0];
        } else t = t->ch[1];
    }
}

void print(node *t) {
    if(t == NULL) return ;
    print(t->ch[0]);
    cout << t->name << ' ';
    print(t->ch[1]);
}

void clear(node *&t) {
    if(t == NULL) return;
    if(t->ch[0] != NULL) clear(t->ch[0]);
    if(t->ch[1] != NULL) clear(t->ch[1]);
    delete t;
    t = NULL;
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
    int t, v, n;
    string str;
    node *tmp;
    cin >> t;
    for(int cas = 1 ; cas <= t ; cas++) {
        Size = 0;
        clear(root);
        cout << "Case #" << cas << ":\n";
        cin >> n;
        for(int i=1 ; i<=n ; i++){
            cin >> str;
            if(str == "Find") {
                cin >> v;
                findsuc(v,tmp);
                if(tmp == NULL) {
                    puts("WAIT...");
                } else {
                    cout << tmp->name << '\n';
                    remove(root, tmp->v, tmp->date);
                }
            } else {
                cin >> str >> v;
                insert(root, str, v, i);

                if(root == NULL) puts("0");
                else cout << root->size << '\n';
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值