POJ 2051 Argus (simple title, heap sort or priority queue)

Source: Internet
Author: User

is a data structure problem, using the heap for weight adjustment and sequencing, each adjustment is O (n) complexity, very efficient.

The first view of the problem can be done with a priority queue, it should be very simple.

In fact, most priority queues are implemented through heaps.

There were some problems with the writing:

1, the second Fork root node subscript is obviously not 0;

2. If there is a widening boundary after the clearance, be careful;

3, in the iterative cycle of comparison, pay particular attention to the exact comparison of who, don't put yourself around dizzy.

AC Code:

#include <iostream>#include<cstdio>#include<cstdlib>#include<cstring>using namespacestd;//Error Correction method: Take a simple sample to run, print the wrong location//Two fork tree note numbering starting from 1//bounds-sensitive after clearancestructnode{intnow,no,period;};Const intmaxn=3005; node N[MAXN];voidDownintPintLen) {    intq=p*2; Node A=N[p];  while(q<Len) {    //There is a dead loop here, because my two-fork tree is numbered from 0, and obviously 0*2^n=0; a bit mentally retarded bug.        if(q+1<len) {//This sentence must not be less (after the bounds of demarcation need to be judged beyond the bounds)            if(n[q].now>n[q+1].now) {q++; }            Else if(n[q].now==n[q+1].now&&n[q].no>n[q+1].no) {q++; }        }                //if (n[q].now>n[p].now) {//who the hell is the comparison object!         if(n[q].now>A.now) {             Break; }        Else if(n[q].now==a.now&&a.no<n[q].no) {//is a in the comparison             Break; } N[p]=N[q]; P=q,q=2*p; } N[p]=A;}voidMakeminheap (intLen) {     for(intJ= (len-1)/2; j>=1; j--){        //cout<<j<<endl;Down (J,len); }}intMainvoid){    strings; CIN>>s; intI=1;  while(s[0]!='#') {cin>>n[i].no>>N[i].period; N[i].now=N[i].period; I++; CIN>>s; }    intlen=i; intl; CIN>>l;    Makeminheap (len);  while(l--) {cout<<n[1].no<<Endl; n[1].now+=n[1].period; Down (1, Len); }    return 0;}

POJ 2051 Argus (simple title, heap sort or priority queue)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.