-
Notifications
You must be signed in to change notification settings - Fork 0
/
ByteDance.cpp
62 lines (46 loc) · 1014 Bytes
/
ByteDance.cpp
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
#include<tuple>
#include<vector>
#include<queue>
#include<iostream>
template<typename T>
std::vector<T> order(std::vector<std::vector<T>> vecs)
{
struct Obj{
std::vector<T> * vec;
size_t pos=0;
bool operator<(const Obj& rop)const
{
return this->vec->at(this->pos)>rop.vec->at(rop.pos);
}
};
std::vector<T> res;
std::priority_queue<Obj> heap;
for( auto&vec:vecs)
{
heap.push((Obj){&vec,0});
}
while(!heap.empty())
{
auto tup = heap.top();
heap.pop();
if(res.empty()||tup.vec->at(tup.pos)!=*(res.rbegin()))
{
res.push_back(tup.vec->at(tup.pos));
}
++(tup.pos);
if(tup.pos<tup.vec->size())
{
heap.push(tup);
}
}
return res;
}
int main(void)
{
std::vector<std::vector<int>> vecs{{1,4,5},{1,3,4},{2,6}};
auto res = order(vecs);
for(const auto& n:res)
{
std::cout<<n<<" ";
}
}