-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVBOSS.cpp
More file actions
136 lines (98 loc) · 2.45 KB
/
VBOSS.cpp
File metadata and controls
136 lines (98 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <stdio.h>
#include <utility>
#include <queue>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
struct employee
{
int id;
int salary;
int height;
int parent_index;
int sub_ordinates;
int cur;
bool processed;
bool operator < (const employee& e) const
{
return (salary < e.salary);
}
};
// problem states explictly that no two employees
// have same salary.
struct salary_predicate
{
inline bool operator() (const employee& struct1, const employee& struct2)
{
if(struct1.height == struct2.height)
return struct1.salary < struct2.salary;
return (struct1.height < struct2.height);
}
};
const int MAX_EMPLOYEES = 30000;
const int MAX_QUERIES = 200;
employee employees[MAX_EMPLOYEES];
int queries[MAX_QUERIES];
int main()
{
int test_cases;
scanf("%d", &test_cases);
while(test_cases--)
{
int employeeCount, queryCount;
scanf("%d %d", &employeeCount, &queryCount);
int i = 0;
int j = 0;
while(i < employeeCount)
{
employees[i].parent_index = -1;
employees[i].sub_ordinates = 0;
scanf("%d %d %d", &employees[i].id, &employees[i].salary, &employees[i].height);
i++;
}
map<int, int> mapper;
while(j < queryCount)
{
scanf("%d", &queries[j]);
mapper.insert(pair<int, int>(queries[j], -1));
j++;
}
// now step1; sort employees structure based on height&SALARY!
sort(employees, employees + employeeCount, salary_predicate());
for(int b = 0; b < employeeCount; b++)
employees[b].cur = b;
typedef map<employee, int> map_t;
map_t xc;
for(int i = 0; i < employeeCount; i++)
xc.insert(pair<employee, int>(employees[i], 1));
// start with smallest.
for(int v = 0; v < employeeCount; v++)
{
xc.erase(employees[v]);
map_t::iterator it = xc.lower_bound(employees[v]);
if(it != xc.end())
{
employee boss = it->first;
employees[v].parent_index = boss.cur;
employees[boss.cur].sub_ordinates += employees[v].sub_ordinates + 1;
}
}
for(int k = 0; k < employeeCount; k++)
{
employees[k].cur = k;
if(mapper.find(employees[k].id) != mapper.end())
mapper[employees[k].id] = k;
}
// time to print it out.
for(int b = 0; b < queryCount; b++)
{
int id = queries[b];
int index = mapper[id];
int parent_index = employees[index].parent_index;
int parent = parent_index < 0 ? 0 : employees[parent_index].id;
printf("%d %d\r\n", parent, employees[index].sub_ordinates);
}
}
return 0;
}