[sicily online]1063. Who's the Boss

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

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.

Input

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.

Output

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).

Sample Input

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

题目分析:

对薪水进行排序,然后依次遍历,连着符合上下级关系的(也就是薪水低的身高高)的记录下来,然后后面比较。最后建立一索引。

ps  0.9s通过,但是有几位大神是0.1s通过,希望能指教

/*
*/
#include<iostream>
#include <iomanip>
#include<stdio.h>
#include<cmath>
#include<iomanip>
#include<list>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
#include <stack>
#include<queue>
#include<string.h>
using namespace std;

typedef struct PERSON
{
	int id;
	int sals;
	int height;
	int subordinatesNum;//雇员的数目
	int bossIndex;//老边的编号
}Person;

bool cmp(const Person &x1,const Person &x2)
{
	return x1.sals>x2.sals;
}

int main()
{
	int n;
	scanf("%d",&n);
	for(int xx=0;xx<n;xx++)
	{
		int m,q;
		scanf("%d%d",&m,&q);
		vector<Person> data;
		data.reserve(30010);
		for(int i=0;i<m;i++)
		{
			int hao,sals,height;
			scanf("%d%d%d",&hao,&sals,&height);
			Person tmpPer;
			tmpPer.id=hao;
			tmpPer.sals=sals;
			tmpPer.height=height;
			tmpPer.subordinatesNum=0;
			tmpPer.bossIndex=-1;
			data.push_back(tmpPer);
		}
		sort(data.begin(),data.end(),cmp);
		vector<int> noParent;//标记没有boss的
		for(int i=m-2;i>=0;i--)
		{
			vector<int>::iterator ite;

			for(ite=noParent.begin();ite!=noParent.end();)
			{
				if(data[i].height>=data[*ite].height)
				{
					data[i].subordinatesNum+=data[*ite].subordinatesNum+1;
					data[*ite].bossIndex=i;
					ite=noParent.erase(ite);
				}
				else ite++;
			}
			if(data[i].height>=data[i+1].height)
			{
				data[i].subordinatesNum+=data[i+1].subordinatesNum+1;
				data[i+1].bossIndex=i;
			}
			else noParent.push_back(i+1);
		}
		map<int,int> index;
		vector<int>::size_type j;
		for(j=0;j<data.size();j++)//构建索引
		{
			index[data[j].id]=j;
		}
		for(int i=0;i<q;i++)
		{
			int qu;
			scanf("%d",&qu);
			if(data[index[qu]].bossIndex==-1)
				printf("0 ");
			else printf("%d ",data[data[index[qu]].bossIndex].id);
			printf("%d\n",data[index[qu]].subordinatesNum);
		}
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值