您的位置:首页 > 其它

sicily 1063 Who's the boss

2014-10-15 00:30 288 查看
题目大意是给一串员工的ID,工资和身高(其中,ID和工资是唯一的),然后让我们根据特定的ID,找出这个员工的老板是谁以及下属的个数。判断员工A是员工B的老板的标准是:A的身高大于等于B的身高,且在比B工资高的员工当中,A的工资是最低的。因此我们只需将员工按照工资排序,然后再根据身高来找老板和下属就可以了。但这道题还有一个需要注意的点,举个例子来说:

111 1900  2000

222 2000 1900

333 1100 1000

444 1200 1100

按工资由低到高排序后是333,444,111,222. 此时,333和444都是111的下属,尽管333和444身高,工资都小于222的,但222却不是他们的上司。这放在现实中也很容易理解,比如说A和B是两个不同部门的经理,即A和B地位相等,但是A的下属却不是B的下属。

下面是我的源码:

// Problem#: 1063
// Submission#: 3019936
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ // All Copyright reserved by Informatic Lab of Sun Yat-sen University

#include<iostream>
#include<vector>
#include<map>
#include<algorithm>

using namespace std;

struct Employee {
int id;
int salary;
int height;
Employee(int i, int s, int h) {
id = i;
salary = s;
height = h;
}
};

bool cmp(Employee e1, Employee e2) {
return e1.salary < e2.salary;
}

bool operator<(Employee e1, Employee e2) {
return e1.height <= e2.height;
}

bool operator>(Employee e1, Employee e2) {
return e1.height >= e2.height;
}

int main() {
int N;
int q, m;
int id, salary, height, query;
vector<Employee> employees;
map<int, int> temp;
int queries[200];
cin >> N;
for (int n = 0; n < N; n++) {
employees.clear();
temp.clear();
cin >> m >> q;
for (int i = 0; i < m; i++) {
cin >> id >> salary >> height;
employees.push_back(Employee(id, salary, height));
}
for (int i = 0; i < q; i++) {
cin >> query;
queries[i] = query;
}

sort(employees.begin(), employees.end(), cmp);
//用map 建立索引
for (int i = 0; i < m; i++) {
temp[employees[i].id] = i;
}

for (int i = 0; i < q; i++) {
int index = temp[queries[i]];
int subordinates = 0;
// 找下属的个数,当两个员工无法比较时跳出循环
for (int j = index - 1; j >=0; j--) {
if (employees[j] < employees[index]) {
subordinates++;
} else {
break;
}
}
// 找上司
int boss = 0;
for (int j = index + 1; j < m; j++) {
if (employees[j] > employees[index]) {
boss = employees[j].id;
break;
}
}
cout << boss << " " << subordinates << endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sicily