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的下属。
下面是我的源码:
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 1063.Who's the boss
- sicily 1063. Who's the Boss
- [sicily online]1063. Who's the Boss
- Sicily 1063. Who's the Boss
- 1063. Who's the Boss
- (利用树的遍历求解层次性问题8.4.10)POJ 1634 Who's the boss?(求解某一个节点的父亲以及他的孩子的数目)
- ZOJ1989 POJ1634 Who's the Boss
- pku 1634 Who's the boss? stl排序+二分查找
- The Art of Intimacy_Personal Boundaries at Work -- Who's the Boss?
- 2018_2_13_Who's the boss?_层次_排序_思维
- poj 1634 Who's the boss?
- POJ 1634 Who's the boss?
- Sicily 1063 Who's the Boss
- POJ 1634 Who's the boss? 笔记
- RunningMapReduceExampleTFIDF - hadoop-clusternet - This document describes how to run the TF-IDF MapReduce example against ascii books. - This project is for those who wants to experiment hadoop as a skunkworks in a small cluster (1-10 nodes) - Google Pro
- [导入][阅读] Sun's New Boss: The Same as the Old Boss?
- [sicily]1157. The hardest problem
- bzoj1753 [Usaco2005 qua]Who's in the Middle
- Sicily 1155. Can I Post the letter
- HDU 4059 The Boss on Mars(数论)