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)