Sicily 1063. Who's the Boss

题目大意:按照薪水和身高排序,薪水与身高最高的为BOSS。求给定ID人的BOSS及下属数目。

解题思路:

1.惯例先吐槽。N次TLE,最后0.98险过。

2.根据题意,按照要求条件建立N叉树,最后输出某节点的父节点ID及子节点个数。

3.建树过程分为三个步骤:

a)首先按照薪水排序,薪水最少的员工为下属员工最少的员工(即为叶节点);

b)从工资最低的员工开始遍历,第一个比其身高的人即为直属BOSS (从根节点开始建立节点关系);

c)再从头遍历将所有员工的下属个数加回至直属BOSS的下属个数中(建树之后扫描全树更新权值)。


// 1063. Who's the Boss
// 查找下属数目应从下属最少的开始遍历,如无下属的员工
// 下属数目和薪水成正比 

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

using namespace std;

struct Record{
    int id,salary,height;
    int sub_num,boss_id; 
    vector<int> subordinates; 
    
    bool operator < (const Record& r)const{
        return salary > r.salary;    
    }
}employees[30001];

int main(){
    int m,num_employees,num_queries;
   
    cin >> m;
    while( m-- ){
        
        int i,j,k;
    
        cin >> num_employees >> num_queries;
        
        for( i = 0; i < num_employees; ++i ){
            cin >> employees[i].id >> employees[i].salary >> employees[i].height;
            
            employees[i].subordinates.clear();
            employees[i].sub_num = 0;
            employees[i].boss_id = 0;
        }
        
        // 按薪水降序排序,薪水最少为下属员工最少的员工 
        sort( employees, employees + num_employees );  
        
        // 建立N叉树:从工资最低的员工开始遍历,第一个比其身高的人即为直属BOSS 
        for( i = num_employees - 1 ; i >= 0; --i ){
            for ( j = i - 1; j >= 0; --j ){             
                if( employees[i].height <= employees[j].height ){
                    employees[i].boss_id = employees[j].id;  
                    employees[j].subordinates.push_back(i);    
                    break;        
                }    
            }
        } 
        
        for( i = 0; i < num_employees; ++i ){
            vector<int> tmp_v = employees[i].subordinates;
            int length = employees[i].subordinates.size();
            
            int count = 0;
            for( int j = 0; j < length; ++j ){
                count += employees[ tmp_v[j] ].sub_num + 1;    
            }
            employees[i].sub_num = count;         
        } 
        
        int id;
        while( num_queries-- ){          
            cin >> id;           
            
            for( j = 0; j < num_employees; ++j ){
                 if( employees[j].id == id ) break;    
            }
            cout << employees[j].boss_id << " " 
                 << employees[j].sub_num << endl;    
        }    
    }

    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值