/*1063. Who's the Boss
题目大意:给出员工ID,收入,身高。 当a收入大于b且身高大于b,
且a的收入高于b的量是最小时,a为b的直接上司
b的下属也是a的下属。
对查询某个ID,输出其直属上司以及该ID的下属数量
方法:1.先把员工按照收入从大到小降序排列。
2.从底部开始往上遍历数组,依次找出每个员工的直属上司,将自身
fatherID = 上司所处数组下标i
并且初始化下属数量childNum = 0, 同时在相应上司的child
集合中push_back(下属所处数组下标i)
当没有上司时,其fatherID = -1;
3、再从底部往上遍历数组,当发现其下属容器chilid不为空时,
遍历容器,把其下属的childNum+1 加到自身的childNum上
4、针对每一个查询的ID,找出相应的ID后输出结果
*/
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
struct Employee{
int ID, salary, height, fatherID, childNum;
vector<int> child;
};
bool compare(Employee a, Employee b){
return a.salary > b.salary ;
}
int main ()
{
int testN;
int employeeNum, queryNum;
scanf("%d", &testN);
//cin >> testN;
Employee employee[30000];
int query[200];
int ID, salary, height;
for(int i=1; i<=testN; i++){
//cin >> employeeNum >> queryNum;
scanf("%d %d", &employeeNum,&queryNum);
for(int k=0; k<employeeNum; k++){
//cin >> employee[k].ID >> employee[k].salary >> employee[k].height;
scanf("%d %d %d", &employee[k].ID, &employee[k].salary , &employee[k].height);
employee[k].childNum = 0;
employee[k].child.clear();
}
sort(employee,employee+employeeNum, compare);
for(int j=employeeNum-1; j>=0; j--){
int k;
for(k=j-1; k>=0; k--){
if(employee[k].height>= employee[j].height)
break;
}
if(k>=0){
employee[j].fatherID = k;
employee[k].child.push_back(j);
}
else
employee[j].fatherID = -1;
}
for(int j=employeeNum-1; j>=0; j--){
if(employee[j].child.size() >0){
for(int k=0; k <employee[j].child.size(); k++ ){
employee[j].childNum += employee[employee[j].child[k]].childNum + 1;
}
}
}
for(int k=0; k<queryNum; k++)
{
int queryID ;
//cin >> queryID;
scanf("%d", &queryID);
int j;
for(j=0; j<employeeNum; j++)
if(employee[j].ID == queryID)
break;
if(employee[j].fatherID == -1)
//cout << "0 " << employee[j].childNum << endl;
printf("0 %d\n", employee[j].childNum);
else
//cout << employee[employee[j].fatherID].ID << " " << employee[j].childNum << endl;
printf("%d %d\n", employee[employee[j].fatherID].ID,employee[j].childNum );
}
}
system("pause");
return 0;
}
09-03