sicily 1063

Description
Several surveys indicate that the taller you are, the higher youcan climb the corporate ladder. At TALL Enterprises Inc. this "defacto standard" has been properly formalized: your boss is alwaysat least as tall as you are. Furthermore, you can safely assumethat your boss earns a bit more than you do. In fact, you can beabsolutely sure that your immediate boss is the person who earnsthe least among all the employees that earn more than you and areat least as tall as you are. Furthermore, if you are the immediateboss of someone, that person is your subordinate, and all hissubordinates are your subordinates as well. If you are nobody'sboss, then you have no subordinates. As simple as these rules are,many people working for TALL are unsure of to whom they should beturning in their weekly progress report and how many subordinatesthey have. Write a program that will help in determining for anyemployee who the immediate boss of that employee is and how manysubordinates they have. Quality Assurance at TALL have devised aseries of tests to ensure that your program is correct. These testare described below.
Input

On the first line of the input is a single positive integer n,telling the number of test scenarios to follow. Each scenariobegins with a line containing two positive integers m and q, wherem (at most 30000) is the number of employees and q (at most 200) isthe number of queries. The following m lines each list an employeeby three integers on the same line: employee ID number (six decimaldigits, the first one of which is not zero), yearly salary in Eurosand finally height in m (1 microm= 10^-6 meters - accuracy isimportant at TALL). The chairperson is the employee that earns morethan anyone else and is also the tallest person in the company.Then there are q lines listing queries. Each query is a singlelegal employee ID.

The salary is a positive integer which is at most 10 000 000. Notwo employees have the same ID, and no two employees have the samesalary. The height of an employee is at least 1 000 000 microm andat most 2 500 000 microm.

Output
For each employee ID x in a query output a single line with twointegers y k, separated by one space character, where y is the IDof x's boss, and k is the number of subordinates of x. If the queryis the ID of the chairperson, then you should output 0 as the ID ofhis or her boss (since the chairperson has no immediate bossexcept, possibly, God).
Sample Input
 Copy sampleinput to clipboard
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
Sample Output
123457 0
0 2
123458 1
200001 2
200004 0
200004 0
0 3
题目是英文的,略长略蛋疼,整理一下,题目大意如下:
讲的是一个薪水和身高的问题。如果A的薪水和身高都比B高,那么A就是B的上司;进一步地,如果A是比B的薪水高的人中薪水最少的,并且A的身高至少和B一样高,那么A就是B的直接上司。 
      我的解法是,首先将所有人按薪水从少到多排好序,然后依次往上检索,找到一个身高大于等于当前身高的人,那么这个人就是当前人的直接上司,下属人数是原有下属人数加上当前人的下属人数之和再加1(即当前人)。

#include 
#include
#include
using namespace std; 

struct Node
{
int ID, salary,height;
int boss; //直属老板的ID 
int subCount; // 下属的数量
};

bool cmpBySalary(constNode& s1, const Node& s2) // 薪水比较函数 
{
return s1.salary< s2.salary;
}

int main()
{
int T, m, q;
scanf("%d",&T);
Nodedata[30000];

while(T--)
{
scanf("%d%d",&m, &q);
for(int i = 0; i< m; i++)
{
scanf("%d%d%d",&data[i].ID, &data[i].salary,&data[i].height);
data[i].boss =data[i].subCount = 0;
}
sort(data, data+m,cmpBySalary); // 按薪水从低到高排序 
for(int i = 0, j; i< m; i++)
for(j = i+1; j< m; j++)
{
if(data[i].height<= data[j].height) //判断老板ID和下属数目 
{
data[i].boss =data[j].ID;
data[j].subCount +=data[i].subCount + 1; 
break;
}
}
    
for (int i = 0, j; i< q; i++) // 线性查找 
{ 
int num;
          scanf( "%d",&num);  
          for( j = 0; j< m; j++)
          {
          if(data[j].ID ==num)
          break;
          }
          printf( "%d %d\n",data[j].boss, data[j].subCount );  
       } 
}
return 0;
}        


你可能感兴趣的:(Programming)