一.题意
一个员工是另外一个员工的老板必须满足的条件是作为老板的员工的薪水salary必须大于这个员工,而且作为老板的员工的身高height要大于等于这个员工。首先按照薪水的多少从小到大进行排序,然后找每一个员工的直属老板。注意老板的下属的数量为其下属的下属之和。
二.用结构体。为了方便查询再加设一个按id号排序的数组。
三. 注意员工老板的后代包括这些员工后代的和。每次vector都要清空一次。
四. 代码
1 // 2 // main.cpp 3 // sicily-1063 4 // 5 // Created by ashley on 14-10-13. 6 // Copyright (c) 2014年 ashley. All rights reserved. 7 // 8 9 #include <iostream> 10 #include <vector> 11 #include <algorithm> 12 using namespace std; 13 typedef struct 14 { 15 int idNumber; 16 int salary; 17 int height; 18 int bossId; 19 int subordinates; 20 }employee; 21 vector<employee> allEmployees; 22 employee sortByID[1000000]; 23 int query[200]; 24 bool compare(employee left, employee right) 25 { 26 return left.salary < right.salary; 27 } 28 int main(int argc, const char * argv[]) 29 { 30 int cases; 31 int employeeCounter, queryCounter; 32 int id, sal, hei; 33 cin >> cases; 34 while (cases--) { 35 allEmployees.clear(); 36 cin >> employeeCounter >> queryCounter; 37 for (int i = 0; i < employeeCounter; i++) { 38 cin >> id >> sal >> hei; 39 allEmployees.push_back(employee{id, sal, hei, 0, 0}); 40 } 41 for (int i = 0; i < queryCounter; i++) { 42 cin >> query[i]; 43 } 44 sort(allEmployees.begin(), allEmployees.begin() + employeeCounter, compare); 45 //找boss 46 for (int i = 0; i < employeeCounter; i++) { 47 int key = -1; 48 for (int j = i + 1; j < employeeCounter; j++) { 49 if (allEmployees[j].height >= allEmployees[i].height) { 50 key = j; 51 break; 52 } 53 } 54 if (key != -1) { 55 allEmployees[i].bossId = allEmployees[key].idNumber; 56 allEmployees[key].subordinates = allEmployees[key].subordinates + allEmployees[i].subordinates + 1; 57 } 58 sortByID[allEmployees[i].idNumber] = allEmployees[i]; 59 } 60 for (int i = 0; i < queryCounter; i++) { 61 cout << sortByID[query[i]].bossId << " " << sortByID[query[i]].subordinates << endl; 62 } 63 } 64 return 0; 65 }
时间: 2024-11-10 03:26:44