标签:sicily
Time Limit: 1 secs, Memory Limit: 32 MB
Several surveys indicate that the taller you are, the higher you can climb the corporate ladder. At TALL Enterprises Inc. this "de facto standard" has been properly formalized: your boss is always at least as tall as you are. Furthermore, you can safely assume that your boss earns a bit more than you do. In fact, you can be absolutely sure that your immediate boss is the person who earns the least among all the employees that earn more than you and are at least as tall as you are. Furthermore, if you are the immediate boss of someone, that person is your subordinate, and all his subordinates are your subordinates as well. If you are nobody‘s boss, then you have no subordinates. As simple as these rules are, many people working for TALL are unsure of to whom they should be turning in their weekly progress report and how many subordinates they have. Write a program that will help in determining for any employee who the immediate boss of that employee is and how many subordinates they have. Quality Assurance at TALL have devised a series of tests to ensure that your program is correct. These test are described below.
On the first line of the input is a single positive integer n, telling the number of test scenarios to follow. Each scenario begins with a line containing two positive integers m and q, where m (at most 30000) is the number of employees and q (at most 200) is the number of queries. The following m lines each list an employee by three integers on the same line: employee ID number (six decimal digits, the first one of which is not zero), yearly salary in Euros and finally height in m (1 microm= 10^-6 meters - accuracy is important at TALL). The chairperson is the employee that earns more than anyone else and is also the tallest person in the company. Then there are q lines listing queries. Each query is a single legal employee ID.
The salary is a positive integer which is at most 10 000 000. No two employees have the same ID, and no two employees have the same salary. The height of an employee is at least 1 000 000 microm and at most 2 500 000 microm.
For each employee ID x in a query output a single line with two integers y k, separated by one space character, where y is the ID of x‘s boss, and k is the number of subordinates of x. If the query is the ID of the chairperson, then you should output 0 as the ID of his or her boss (since the chairperson has no immediate boss except, possibly, God).
2 3 3 123456 14323 1700000 123458 41412 1900000 123457 15221 1800000 123456 123458 123457 4 4 200002 12234 1832001 200003 15002 1745201 200004 18745 1883410 200001 24834 1921313 200004 200002 200003 200001
123457 0 0 2 123458 1 200001 2 200004 0 200004 0 0 3
挺好的一道题,找到直接的boss和下属数,这个0.49s版本:
#include <iostream> #include <algorithm> #include <stdio.h> using namespace std; struct People { int id, salary, height, boss, subordinates; }p[30005]; bool cmp_salary(const People& a, const People& b) {//按照薪水排序,方便找老板和下属 return a.salary <= b.salary; } bool cmp_id(const People& a, const People &b) {//按照id排序,方便查找 return a.id <= b.id; } int find(int target, int front, int back) {//二分法找id if (front <= back) { int mid = (front + back) / 2; if (p[mid].id == target) return mid; else if (target < p[mid].id) return find(target, front, mid - 1); else return find(target, mid + 1, back); } else return 0; } int main() { int case_num, p_num, q_num, i, j, target, pos; scanf("%d", &case_num); while (case_num--) { scanf("%d%d", &p_num, &q_num); for (i = 0; i < p_num; i++) { scanf("%d%d%d", &p[i].id, &p[i].salary, &p[i].height); p[i].boss = 0; p[i].subordinates = 0; } sort(p, p + p_num, cmp_salary);//按照薪水排序 for (i = 0; i < p_num; i++) { for (j = i + 1; j < p_num; j++) { if (p[j].height >= p[i].height) {//找到的第一个就是直接老板 p[j].subordinates += p[i].subordinates + 1;//找到的老板的下属数等于当前员工的下属数加上这名员工自己 p[i].boss = p[j].id; break;//记得break,不然下属数会有很多重复 } } } sort(p, p + p_num, cmp_id);//按照id排序 while (q_num--) { scanf("%d", &target); pos = find(target, 0, p_num - 1); printf("%d %d\n", p[pos].boss, p[pos].subordinates); } } return 0; }
标签:sicily
原文地址:http://blog.csdn.net/u012925008/article/details/44762919