POJ 1634 Who's the boss?

时间:2023-03-08 22:27:02
POJ 1634 Who's the boss?

题意:

一个员工A的直接上司是那些薪水大于A,并且身高>=A的人中薪水最少的一个。
主席CEO的薪水最高,且身高也是最高的。
有多组数据。
每组数据给出m个员工,和q个询问。
每个员工有id、薪水、身高。
对于每个询问,给出某个id,让你求该员工的直接上司的id和该员工的下属的个数。
若该员工是CEO,则他的上司的id为0。

先将员工按薪水从小到大排序,然后for循环一遍,求某一节点的直接父亲,顺带求节点的孩子个数。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <string>
#include <vector>
#include <map> using namespace std;
const int maxn=;
int m,q;
int fa[maxn]; //存储父亲的编号
int head[maxn];
int num[maxn]; //存储以该节点为子树的包含的定点个数
int idx[]; //存储id对映的编号,原先用map存储的,时间是3500ms,后改用数组,时间800+ms
int tot;
//map<int,int> maps;
struct Edge{
int next,to;
}edge[maxn];
void add(int u,int v){
edge[tot].next=head[u];
edge[tot].to=v;
head[u]=tot++;
}
struct Staff{
int idx,id,salary,h;
bool operator<(const Staff tmp)const{
return salary<tmp.salary;
}
}staff[maxn]; void init(){
memset(head,-,sizeof(head));
for(int i=;i<maxn;i++)
num[i]=;
tot=;
//maps.clear();
memset(fa,,sizeof(fa));
} int main()
{
int t,id;
staff[].id=;
cin>>t;
while(t--){
init();
scanf("%d%d",&m,&q);
for(int i=;i<=m;i++){
scanf("%d%d%d",&staff[i].id,&staff[i].salary,&staff[i].h);
}
sort(staff+,staff+m+);
for(int i=;i<=m;i++){
//maps[staff[i].id]=i;
idx[staff[i].id]=i;
for(int j=i+;j<=m;j++){
if(staff[j].salary>staff[i].salary && staff[j].h>=staff[i].h){
add(j,i);
fa[i]=j;
num[j]+=num[i];
break;
}
}
}
int u;
for(int i=;i<=q;i++){
scanf("%d",&id);
u=idx[id];
//printf("%d %d\n",staff[fa[u]].id,num[u]-1);
printf("%d %d\n",staff[fa[u]].id,num[u]-);
}
}
return ;
}