Sicily 1063 Who's the Boss

    这道题的题意有误,错在这一句:In fact, you can be absolutely sure that your immediate boss is the person who earns the least among all the employees that earn more than you and are at least as tall as you are.根据这一句,题目给出的案例二,2003是比2002工资多的人里工资最少的,那么2003即是2002的直接上司。这样就与答案不符,而且也与2003比2002矮矛盾。实际上题目想表达的意思是:员工的工资比他所有的直系上司要低。根据这个意思作法如下。结果能够AC。


#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <climits>

using namespace std;

struct Node {
    int id;
    int salary;
    int height;
    int boss;
    vector<int> children;
};

int subordinatesCount(const vector<Node> &tree, int index) {
    if (tree[index].children.empty())
        return 0;
    else {
        int count=0;
        for (vector<int>::const_iterator iter=tree[index].children.begin();
            iter!=tree[index].children.end(); iter++) {
                count++;
                count+=subordinatesCount(tree, *iter);
            }
        return count;
    }
}

int main() {
    int cases;
    cin>>cases;
    while (cases--) {
        int m, q;
        cin>>m>>q;
        vector<Node> tree;
        map<int, int> table;
        if (m) {
			// god
			tree.push_back(Node());
			tree[0].id=0;
			tree[0].salary=INT_MAX;
			tree[0].height=INT_MAX;
            while  (m--) {
                tree.push_back(Node());
                cin>>tree[tree.size()-1].id
                    >>tree[tree.size()-1].salary
                    >>tree[tree.size()-1].height;
            }
            bool moreSalary(Node a, Node b);
            sort(tree.begin(), tree.end(), moreSalary);
            // 建立tree里id到索引的映射表
            for (int i=0; i<tree.size(); i++)
                table[tree[i].id]=i;
            // 构建树
            tree[0].boss=-1;	// big boss
            for (int i=1; i<tree.size(); i++) {
                int boss=i;
                while (tree[--boss].height<tree[i].height);
                // until tree[boss].height>=tree[i].height
                tree[i].boss=boss;
                tree[boss].children.push_back(i);
            }
        }
        while (q--) {
            int id;
            cin>>id;
            cout<<tree[tree[table[id]].boss].id<<" "
                <<subordinatesCount(tree, table[id])<<"\n";
        }
    }
    return 0;
}

bool moreSalary(Node a, Node b) {
    return a.salary>b.salary;
}

/**************************************************************************
特殊情况总要加以特别考虑,比如这道题里big boss,它们总是不同于一般情况的逻辑。
***************************************************************************/

// by wbchou
// Jan 31th, 2013


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值