Sicily 1063 Who's the Boss

来源:互联网 发布:win10rar解压软件下载 编辑:程序博客网 时间:2024/05/20 14:17

    这道题的题意有误,错在这一句: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) {// godtree.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


原创粉丝点击