← 返回 amazon 的题目列表Manager With Highest Average Salary Among Reports
类型:online_judge
Problem: Employee Reporting Structure and Average Salary
You are given a list of employee records. Each record contains:
employee_id: the employee's ID
manager_id: the ID of the employee's direct manager; -1 if the employee has no manager
salary: the employee's salary
Implement the following features:
Find the manager whose direct reports have the highest average salary. Return that manager's ID and the average salary of their direct reports.
Given a manager ID, compute the average salary of all employees in that manager's reporting tree. The reporting tree includes both direct and indirect reports, but excludes the manager themself.
An employee with no reports is not a candidate for feature 1. For feature 2, if the given manager has no reports, output 0.00.
If multiple managers have the same highest average salary in feature 1, return the manager with the smallest ID.
Input Format
The first line contains an integer n, the number of employees.
The next n lines each contain three integers:
employee_id manager_id salary
The last line contains one integer query_manager_id, the manager ID for feature 2.
Output Format
Output two lines:
The answer for feature 1: manager_id average_salary
The answer for feature 2: query_manager_id average_salary
Average salaries should be printed with exactly two decimal places.
If no manager has any reports, output the following as the first line:
-1 0.00
Constraints
1 <= n <= 10^5
employee_id values are unique
manager_id = -1 or exists as an employee_id
Each employee has at most one direct manager
The reporting relationship is acyclic
0 <= salary <= 10^9
Example
Input
6
1 -1 100
2 1 80
3 1 120
4 2 70
5 2 90
6 3 200
1
Output
3 200.00
1 112.00