本文整理汇总了C++中pop_heap函数的典型用法代码示例。如果您正苦于以下问题:C++ pop_heap函数的具体用法?C++ pop_heap怎么用?C++ pop_heap使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了pop_heap函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: make_heap
ListNode *mergeKLists(vector<ListNode *> &lists) {
// wyuan; 10/11/2014; Maitain a min heap. Use make_heap in <algorithm>
if(lists.empty())
return NULL;
int size = lists.size();
// vector<node> heap(size); Which one is better?
vector<node> heap;
heap.reserve(size);
for(int i = 0; i < size; i++)
heap.push_back(node(lists[i]));
make_heap(heap.begin(), heap.end(), greater<node>());
ListNode *head = new ListNode(0); // Init!!!!!!
ListNode *pre = head;
pop_heap(heap.begin(), heap.end(), greater<node>());
node minNode = heap.back();
heap.pop_back();
while(minNode.val != INT_MAX)
{
pre->next = minNode.from;
pre = pre->next;
ListNode *next = minNode.from->next;
heap.push_back(node(next));
push_heap(heap.begin(), heap.end(), greater<node>());
pop_heap(heap.begin(), heap.end(), greater<node>());
minNode = heap.back();
heap.pop_back();
}
ListNode *ret = head->next;
delete head;
return ret;
}
示例2: addNum
void addNum(int num) {
if (upper.size() == 0) {
upper.push_back(num);
} else if (upper.size() == lower.size()) {
if (num > lower[0]) {
upper.push_back(num);
push_heap(upper.begin(), upper.end(), greater<int>());
} else {
lower.push_back(num);
push_heap(lower.begin(), lower.end());
int val = lower[0];
pop_heap(lower.begin(), lower.end());
lower.pop_back();
upper.push_back(val);
push_heap(upper.begin(), upper.end(), greater<int>());
}
} else {
if (num > upper[0]) {
upper.push_back(num);
push_heap(upper.begin(), upper.end(), greater<int>());
int val = upper[0];
pop_heap(upper.begin(), upper.end(), greater<int>());
upper.pop_back();
lower.push_back(val);
push_heap(lower.begin(), lower.end());
} else {
lower.push_back(num);
push_heap(lower.begin(), lower.end());
}
}
}
示例3: gen_dirch_initialize
/* store the alpha values and build a beta_tree for the generator */
void gen_dirch_initialize(gen_dirch_param *gen, int numd, const double *alp)
{
int *heap; int heap_size;
int leaf, internal; /* indexes of nodes in tree */
double *alpha; /* shorthand pointer for gen->alpha */
gen->num_dim = numd;
/* allocate space for the alphas---both for the leaves and the
* internal nodes
*/
gen->alpha = alpha = (double*)calloc(2*numd-1, sizeof(double));
assert(gen->alpha);
/* allocate space for the internal nodes */
gen->betas = (gen_beta_param*) calloc(numd-1, sizeof(gen_beta_param));
assert(gen->betas);
gen->left = (int*) calloc(numd-1, sizeof(int));
assert(gen->left);
gen->right = (int*) calloc(numd-1, sizeof(int));
assert(gen->right);
/* Build a "Huffman encoding" tree using a heap.
* First, fill the heap array with the indexes of the leaves
*/
heap = (int*)calloc(numd, sizeof(int));
for (leaf=numd-1; leaf>=0; leaf--)
{ alpha[leaf] = alp[leaf];
heap[leaf] = leaf;
}
heapify(heap, alpha, numd);
heap_size=numd;
internal = 0; /* number of internal nodes created so far */
while(heap_size>=2)
{ gen->left[internal] = pop_heap(heap, alpha, &heap_size);
gen->right[internal] = pop_heap(heap, alpha, &heap_size);
gen_beta_initialize(&(gen->betas[internal]),
alpha[gen->left[internal]], alpha[gen->right[internal]]);
alpha[internal+numd] = alpha[gen->left[internal]]
+ alpha[gen->right[internal]];
push_heap(heap, alpha, &heap_size, internal+numd);
internal++;
}
gen->root_index = internal+numd-1;
assert(gen->root_index == 2*numd-2);
free(heap);
}
示例4: make_heap
void Word2Vec::create_huffman_tree()
{
size_t vocab_size = vocab.size();
vector<Word *> heap = vocab;
make_heap(heap.begin(), heap.end(), comp);
for(size_t i = 0; i < vocab_size - 1; ++i)
{
pop_heap(heap.begin(), heap.end(), comp);
Word *min_left = heap.back(); heap.pop_back();
pop_heap(heap.begin(), heap.end(), comp);
Word *min_right = heap.back(); heap.pop_back();
Word *w = new Word(i + vocab_size, min_left->count + min_right->count, "", min_left, min_right);
heap.push_back(w);
push_heap(heap.begin(), heap.end(), comp);
}
//traverse huffman tree,get code
list<tuple<Word *, vector<size_t>, vector<size_t>>> stack;
stack.push_back(make_tuple(heap[0], vector<size_t>(), vector<size_t>()));
while(!stack.empty())
{
auto n = stack.back();
stack.pop_back();
Word *n_w = get<0>(n);
if(n_w->index < vocab_size)
{
n_w->codes = get<1>(n);
n_w->points = get<2>(n);
}
else
{
auto codes_left = get<1>(n);
auto codes_right = codes_left;
codes_left.push_back(0);
codes_right.push_back(1);
auto points = get<2>(n);
points.emplace_back(n_w->index - vocab_size);
stack.emplace_back(make_tuple(n_w->left, codes_left, points));
stack.emplace_back(make_tuple(n_w->right, codes_right, points));
}
}
}
示例5: memset
bool Graph::pfs()
{
int v, j, ww, x;
bool caled[MaxVertex];
int wt[MaxVertex];
vector<Node> myheap;
memset(father, 0xff, sizeof(int)*V);
memset(caled, 0, sizeof(bool)*V);
memset(wt, 0xff, sizeof(int)*V);
myheap.clear();
father[s] = s;
myheap.push_back(Node(s, INT_MAX));
// assert(s != t);
while(!myheap.empty())
{
v = myheap[0].v;
ww = myheap[0].wt;
pop_heap(myheap.begin(), myheap.end(), cmp);
myheap.pop_back();
if(caled[v]) continue;
caled[v] = 1;
for(j = 0; j < V; ++j)
if(cap[v][j]>0 && !caled[j])
{
x = ww<cap[v][j] ? ww : cap[v][j];
if(x <= wt[j]) continue;
father[j] = v;
if(j == t) return 1;
wt[j] = x;
myheap.push_back(Node(j, x));
push_heap(myheap.begin(), myheap.end(), cmp);
}
}
return 0;
}
示例6: main
int main () {
unsigned int n, i;
unsigned int x;
double med;
int v[250000/2+3];
i = 0;
cin >> n;
for(; i < n/2+1; ++i) cin >> v[i];
make_heap(v,v+n/2+1);
for(; i < n; ++i){
cin >> v[n/2+1];
push_heap(v,v+n/2+2);
pop_heap(v,v+n/2+2);
}
if (n % 2 != 0)
med = v.back();
else {
x = v.back();
v.pop_back();
med = (v.back()*0.5 + x*0.5);
}
cout << setiosflags (ios::fixed) << setprecision(1) << med << endl;
return 0;
}
示例7: top
const N PriorityQueue<N, Val>::minPrioirty() {
//removes the top element of the queue.
const N minnode = top();
pop_heap(nodetracker.begin(), nodetracker.end(), NodeComparator());
nodetracker.pop_back();
return minnode;
}
示例8: while
void
TimeScheduler::update(float time)
{
while(!schedule.empty() && schedule.front().wakeup_time < time) {
HSQOBJECT thread_ref = schedule.front().thread_ref;
sq_pushobject(global_vm, thread_ref);
sq_getweakrefval(global_vm, -1);
HSQUIRRELVM scheduled_vm;
if(sq_gettype(global_vm, -1) == OT_THREAD &&
SQ_SUCCEEDED(sq_getthread(global_vm, -1, &scheduled_vm))) {
if(SQ_FAILED(sq_wakeupvm(scheduled_vm, SQFalse, SQFalse, SQTrue))) {
std::ostringstream msg;
msg << "Couldn't wakeup scheduled squirrel VM: ";
sq_getlasterror(scheduled_vm);
if(sq_gettype(scheduled_vm, -1) != OT_STRING) {
msg << "(no info)";
} else {
const char* lasterr;
sq_getstring(scheduled_vm, -1, &lasterr);
msg << lasterr;
}
log_warning << msg.str() << std::endl;
sq_pop(scheduled_vm, 1);
}
}
sq_release(global_vm, &thread_ref);
sq_pop(global_vm, 2);
pop_heap(schedule.begin(), schedule.end());
schedule.pop_back();
}
}
示例9: s0
void CAstar::Astar(CAstarNode*& result)
{
CAstarNode s0(m_hgevStart,0,CalculateH(m_hgevStart),NULL);
//open表中插入起点
m_vOpenList.push_back(s0);
make_heap(m_vOpenList.begin(),m_vOpenList.end());
// CAstarNode* min;
while(!m_vOpenList.empty())
{
//pop出堆顶节点
pop_heap(m_vOpenList.begin(),m_vOpenList.end(),cmpAstarNode);
result = new CAstarNode(m_vOpenList.back().m_hgevCoordinate,m_vOpenList.back().m_nG,
m_vOpenList.back().m_nH, m_vOpenList.back().m_pParent);
//若节点是目标节点,则返回
if(result->m_hgevCoordinate == m_hgevGoal)
{
// memcpy(result,&min,sizeof(CAstarNode));
return ;
}
m_vOpenList.pop_back();
// make_heap(m_vOpenList.begin(),m_vOpenList.end());
//扩展该节点
ExtendNode(result);
// make_heap(m_vOpenList.begin(),m_vOpenList.end());
m_vClosedList.push_back(*result);
}
result = NULL;
}
示例10: clustering_ctg
PWDB* clustering_ctg(PWDB *db, uint32_t min_overlap, float het) {
PWDB *ret;
ret = db;
uint32_t i, j, p, q;
PWcontig *poped;
while ((poped = pop_heap(db->hp)) != NULL) {
if (poped->overlap >= min_overlap && (poped->het - het <= 0)) {
p = poped->id0;
q = poped->id1;
for (i = p; i != (ref_ctglist(ret->ctgv, i))->cls_id; i = (ref_ctglist(ret->ctgv, i))->cls_id)
ref_ctglist(ret->ctgv, i)->cls_id = ref_ctglist(ret->ctgv, ref_ctglist(ret->ctgv, i)->cls_id)->cls_id;
for (j = q; j != (ref_ctglist(ret->ctgv, j))->cls_id; j = (ref_ctglist(ret->ctgv, j))->cls_id)
ref_ctglist(ret->ctgv, j)->cls_id = ref_ctglist(ret->ctgv, ref_ctglist(ret->ctgv, j)->cls_id)->cls_id;
if (i == j) continue;
if (ref_ctglist(ret->ctgv, i)->sz < ref_ctglist(ret->ctgv, j)->sz) {
ref_ctglist(ret->ctgv, i)->cls_id = j;
ref_ctglist(ret->ctgv, j)->sz += ref_ctglist(ret->ctgv, i)->sz;
} else {
ref_ctglist(ret->ctgv, j)->cls_id = i;
ref_ctglist(ret->ctgv, i)->sz += ref_ctglist(ret->ctgv, j)->sz;
}
}
}
return ret;
}
示例11: mergeKLists
ListNode* mergeKLists(vector<ListNode*>& lists) {
vector<ListNode *> minHeap;
for (auto list: lists) {
if (list != nullptr) {
minHeap.push_back(list);
}
}
make_heap(minHeap.begin(), minHeap.end(), compare);
ListNode *res = new ListNode(-1);
ListNode *cur = res;
while (!minHeap.empty()) {
pop_heap(minHeap.begin(), minHeap.end(), compare);
ListNode *node = minHeap.back();
minHeap.pop_back();
cur->next = node;
cur = cur->next;
if (node->next) {
minHeap.push_back(node->next);
push_heap(minHeap.begin(), minHeap.end(), compare);
}
}
return res->next;
}
示例12: solve
int solve(const IntVec& A, const IntVec& B, int k)
{
IntVec q(k);
int n = 0;
int NA = A.size() >= k ? k : A.size();
int NB = B.size() >= k ? k : B.size();
for (int i = 0; i < NA; ++i) {
for (int j = 0; j < NB; ++j) {
int s = A[i] + B[i];
if (n == k) {
if (s < q[0]) {
pop_heap(q.begin(), q.end());
q[n - 1] = A[i] + B[j];
push_heap(q.begin(), q.begin() + n);
}
} else {
q[n++] = A[i] + B[j];
push_heap(q.begin(), q.begin() + n);
}
// cout << n << "," << q << endl;
}
}
// cout << q << endl;
return q[0];
}
示例13: dijkstra
//T: O(ElogV), M: theta(V + E)
void dijkstra(vector<vector<Aresta> > &g, int r) {
int n = g.size();
vector<Vertice> Q;
d.clear();
d.resize(n, INT_MAX);
vector<bool> cor(n, false);
Q.push_back(r);
d[r] = 0;
cor[r] = true;
while (!Q.empty()) {
int u = Q[0];
//if(procurado == u) return;
pop_heap(Q.begin(), Q.end(), comp);
Q.pop_back();
for (int i = 0; i < g[u].size(); i++) {
if (d[u] + g[u][i].p < d[g[u][i].v])
d[g[u][i].v] = d[u] + g[u][i].p; //relaxamento
if (!cor[g[u][i].v]) {
cor[g[u][i].v] = true;
Q.push_back(g[u][i].v);
}
}
make_heap(Q.begin(), Q.end(), comp);
}
}
示例14: pop_heap
void BBTreeTrain::pop() {
pop_heap(nodes_.begin(), nodes_.end(), compare_);
log_info("Popping node %d %f", nodes_.back()->id(), nodes_.back()->score());
// Going to pop the opt node
//check_val(optNode_ != NULL, "ERROR: popping and optNode is not in the queue.");
// if (nodes_.back() == optNode_) {
// optNode_ = NULL;
// numWrongAction_ = 0;
// } else {
// Stop it when it goes too far away
if (optNode_) {
if (nodes_.back() != optNode_) {
++numWrongAction_;
debug("num wrong action: %d", numWrongAction_);
debug("opt node: %d", optNode_->id());
if (numWrongAction_ > 100) {
nodes_.pop_back();
optNode_->setScore(1000);
make_heap(nodes_.begin(), nodes_.end(), compare_);
assert(nodes_.front() == optNode_ && "Fail to promote optNode");
//pop_heap(nodes_.begin(), nodes_.end(), compare_);
debug("promote opt node");
numWrongAction_ = 0;
//optNode_ = NULL;
return;
}
} else {
optNode_ = NULL;
}
}
nodes_.pop_back();
}
示例15: getNewsFeed
/** Retrieve the 10 most recent tweet ids in the user's news feed. Each item
* in the news feed must be posted by users who the user followed or by the
* user herself. Tweets must be ordered from most recent to least recent. */
vector<int> getNewsFeed(int userId) {
vector<pair<Tweet *, Tweet *>> h;
for (const int &u : following[userId]) {
vector<Tweet> &t = tweets[u];
if (t.size() > 0) {
h.emplace_back(t.data(), t.data() + t.size() - 1);
}
}
auto &t = tweets[userId];
if (t.size() > 0) {
h.emplace_back(t.data(), t.data() + t.size() - 1);
}
auto f = [](const pair<Tweet *, Tweet *> &x,
const pair<Tweet *, Tweet *> &y) {
return x.second->time > y.second->time;
};
make_heap(h.begin(), h.end(), f);
const int nums = 10;
vector<int> result;
result.reserve(nums);
for (int i = 0; i < nums && !h.empty(); ++i) {
pop_heap(h.begin(), h.end(), f);
pair<Tweet *, Tweet *> &next = h.back();
result.push_back(next.second->id);
if (next.first == next.second--)
h.pop_back();
else
push_heap(h.begin(), h.end(), f);
}
return result;
}