Sicily-1063

一.题意

一个员工是另外一个员工的老板必须满足的条件是作为老板的员工的薪水salary必须大于这个员工,而且作为老板的员工的身高height要大于等于这个员工。首先按照薪水的多少从小到大进行排序,然后找每一个员工的直属老板。注意老板的下属的数量为其下属的下属之和。

二.用结构体。为了方便查询再加设一个按id号排序的数组。

三. 注意员工老板的后代包括这些员工后代的和。每次vector都要清空一次。

四. 代码

 //
// main.cpp
// sicily-1063
//
// Created by ashley on 14-10-13.
// Copyright (c) 2014年 ashley. All rights reserved.
// #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
typedef struct
{
int idNumber;
int salary;
int height;
int bossId;
int subordinates;
}employee;
vector<employee> allEmployees;
employee sortByID[];
int query[];
bool compare(employee left, employee right)
{
return left.salary < right.salary;
}
int main(int argc, const char * argv[])
{
int cases;
int employeeCounter, queryCounter;
int id, sal, hei;
cin >> cases;
while (cases--) {
allEmployees.clear();
cin >> employeeCounter >> queryCounter;
for (int i = ; i < employeeCounter; i++) {
cin >> id >> sal >> hei;
allEmployees.push_back(employee{id, sal, hei, , });
}
for (int i = ; i < queryCounter; i++) {
cin >> query[i];
}
sort(allEmployees.begin(), allEmployees.begin() + employeeCounter, compare);
//找boss
for (int i = ; i < employeeCounter; i++) {
int key = -;
for (int j = i + ; j < employeeCounter; j++) {
if (allEmployees[j].height >= allEmployees[i].height) {
key = j;
break;
}
}
if (key != -) {
allEmployees[i].bossId = allEmployees[key].idNumber;
allEmployees[key].subordinates = allEmployees[key].subordinates + allEmployees[i].subordinates + ;
}
sortByID[allEmployees[i].idNumber] = allEmployees[i];
}
for (int i = ; i < queryCounter; i++) {
cout << sortByID[query[i]].bossId << " " << sortByID[query[i]].subordinates << endl;
}
}
return ;
}
上一篇:MySQL主键设计盘点


下一篇:【CF573D】Bear and Cavalry 线段树