ACM/ICPC Priority Queue + Set IO buffer (TSH oj-schedule (Task Scheduler))

Source: Internet
Author: User

A bare priority queue (maximum heap) problem, but there are other common queues of practice. This problem I did two days, the results found that the input and output is too frequent, has been only a 55% of the data, the other is tle, if the input and output data into the buffer, and then the full area output, you can reduce the IO time consumption to very low.

Task Scheduler (Schedule)

Description

The Task Scheduler used by a high-performance computing cluster (HPC cluster) is different. For simplicity, it is assumed that the cluster does not support multitasking at the same time, so only a single task is in the execution state at the same moment. In the initial state, each task is given a priority by an integer called the priority number, the smaller the priority , and the lower the ASCII dictionary order of the task name is preferred if the priority series is equal . Thereafter, the CPU and other resources are always occupied by the task with the smallest priority, and each task is calculated, then the priority series is selected to the next task. However, the task here usually does not exit immediately after the calculation, but doubles the priority series (the time required to double the calculation can be ignored) and continues to participate in the dispatch; only when the priority progression is not less than 2^32 , does it really exit

Your task is to predict the execution sequence of a batch of compute tasks according to the above scheduling principle based on the initial priority setting.

input

The first behavior is separated by a space of two integers n and m,n for the initial total number of tasks, m for the predicted task execution sequence length, each line at the end of a newline character

The following n rows contain an integer and a string consisting of no more than 8 lowercase letters and numbers. The former is the initial priority of the task, and the latter is the task name. Spaces are separated between numbers and strings

Output

Up to M rows, each containing a string. The names of the first m tasks in the execution sequence are given in order of execution, and if the execution sequence is less than M, then all tasks before the task of the output scheduler are processed.

Example

Input

3 31 hello2 world10 test

Output

hellohelloworld
Limit

0≤n≤4,000,000

0≤m≤2,000,000

0 < Initial priority for each task < 2^32

There will be no duplicate tasks.

Time: 2 sec

Memory: MB

 Problem Solving Ideas:

First is the priority queue (Max heap), for the discussion here, see my first article data structure (heap) of algorithm notes (POJ 2051)

I have built the heap algorithm and the insertion algorithm after listening to the Deng Junhui Teacher's MOOC after the optimization, batch build heap operation can be the time of comprehensive efficiency through the filter optimization to O (N), relative to the first contact with the heap when there is a greater increase, The code of the handwritten heap can also be made more concise. The detailed algorithm participates in the following code.

Next is about fast input and output (fastio), I am here with the structure of encapsulation, the creation of object IO can complete the operation of the constructor, including stdin and stdout two flow , one is the maximum 4 million times the input, one is the maximum 2 million times the output, Believe that this can reduce the input and output at this time to 10 bits or even single digits, the input and output of time consumption to a very low level.

A simple description of the function used here to set the file buffer and another similar function:

Set File buffer functions
void Setbuf (FILE *stream,char *buf);

void Setvbuf (FILE *stream,char *buf,int type,unsigned size);
These two functions will allow the user to create their own file buffers after the file is opened, without using the fopen () function to open the default buffer set by the file.

For the Setbuf () function, buf indicates the buffer length, which is determined by the value of the macro bufsize defined in Stdio.h, which defaults to 512 bytes. When buf is empty, the SETBUF function causes the file I/O to be buffered.

For the SETVBUF function, a buffer is allocated by the malloc function, and the parameter size indicates the length of the buffer.

The type represents the buffered types, and the values can take the following values:

_IOFBF files are fully buffered, that is, when the buffer is full, the file can be read and written
_IOLBF file line buffer, that is, when a buffer receives a newline character, it can read and write to the file.

_IONBF file does not buffer, ignoring the value of buf,size, directly read and write files, no longer through the file buffer buffer

The code is written in imitation of a blog, here to express our thanks: Terence-yang

The specific code is as follows:

  

1 //Priority Queue + fast input/output (batch)2 //time:1508ms memory:109488k (no.20)3#include <iostream>4#include <cstdio>5#include <cstring>6 using namespacestd;7 8 #defineMAX 40000059 #defineLchild (x) ((x) <<1)Ten #defineRchild (x) (((x) <<1) + 1) One #definePRIOR (A,x,y) (A[x]>a[y]? ( x):(y)) A  - intN, M; - Const Long LongINF = (Long Long)1<< +;//upper limit of priority series the Const intSIZE =1<< +;//Buffer size -  - /*fast input/output buffer settings*/ - structFastIO { +     CharInbuf[size]; -     CharOutbuf[size]; + FastIO () { A setvbuf (stdin,inbuf,_iofbf,size); at setvbuf (stdout,outbuf,_iofbf,size); -     } - }io; -  - structTask { -     Charword[9]; in     Long Longv; -     BOOL operator> (Task &a) {/*overloading as precedence comparer*/ to         returnV < A.V | | v = = a.v && strcmp (Word, A.word) <0; +     } - }task[max]; the  * /*find the highest priority between parent and child in place of parent*/ $ intReplacepa (intx)Panax Notoginseng { -     intPA =x; the     if(Rchild (x) <=N) +PA =PRIOR (Task, X, PRIOR (Task, Lchild (x), Rchild (x))); A     Else if(Lchild (x) <=N) thePA =PRIOR (Task, X, Lchild (x)); +     returnPA; - } $  $ /*Filter Down (adjust the heap downward)*/ - voidPercolatedown (intx) - { the     intRP =Replacepa (x); -      while(RP! =x) {Wuyi swap (TASK[RP], task[x]); thex =RP; -RP =Replacepa (x); Wu     } - } About  $ /*Bulk Build heap (stacking)*/ - voidheapify () - { -      for(inti = n/2; I >=1; i--) A Percolatedown (i); + } the  - intMain () $ { thescanf"%d%d", &n, &m); the      for(inti =1; I <= N; i++) thescanf"%lld%s", &task[i].v, Task[i].word); the heapify (); -  in      for(inti =0; n && i < m; i++) the     { theprintf"%s\n", task[1].word); Abouttask[1].V *=2; the         if(task[1].V >=INF) thetask[1] = task[n--]; thePercolatedown (1); +     } -  the     return 0;Bayi}

ACM/ICPC Priority Queue + Set IO buffer (TSH oj-schedule (Task Scheduler))

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.