The kth great Number_ priority Queue _priority

Source: Internet
Author: User
Tags ming

The kth great numbertimelimit:2000/1000 MS (java/others) memorylimit:65768/65768 K (java/others) 64-bit integer IO forma T:%i64dProblem Descriptionxiao Ming and Xiao Bao is playing a simple Numbers game. In a round Xiao Ming can choose to write down a number, or ask Xiao Bao What's the kth great number is. Because the number written by Xiao Ming are too much, Xiao Bao is feeling giddy. Now, try-to-help Xiao bao.inputthere is several test cases. For each test case, the first line of input contains the positive integer n, K. then n lines follow. If Xiao Ming Choose to write down a number, there'll be is a "I" followed by a number that Xiao Ming would write down. If Xiao Ming Choose to ask Xiao Bao, there would be a "Q" and then your need to output the kth great number. Outputthe output consists of one integer representing the largest number of islands that all lie on one line. Sampleinput
8 3I 1I 2I 3QI 5QI 4Q
Sampleoutput
123
Hint
Xiao Ming won ' t ask Xiao Bao the kth great number when the number of the written number is smaller than K. (1<=k<=n& lt;=1000000).

Test instructions The main idea, is to give a series of numbers, to fix the number of the first few, details please see Sampleinput
This question is a template problem for the priority queue. Of course, C + + can be used in the STL. But think about it, do it yourself.
Code
#include <cstdio>#include<algorithm>#include<ctime>#include<cstdlib>using namespaceStd;typedefintElemtype;ConstElemtype all =1000005; elemtype m1[all];//using function pointers and type redefinition, you can encapsulate a function template. voidHeap_push (Elemtype [],int,BOOL(*CMP) (Elemtype, Elemtype));voidHeap_pop (Elemtype [],int,BOOL(*CMP) (Elemtype, Elemtype));BOOLcmp_min (elemtype elema, Elemtype elemb);intMainvoid){    intm, N, TMP; Charch[5];  while(SCANF ("%d%d", &n, &m)! =EOF) {         for(intI=0; I < m; ++i) {scanf ("%s%d", CH, m1+i);        Heap_push (M1, I, cmp_min); }         for(intI=m; I < n; ++i) {scanf ("%s", CH); if(ch[0] =='Q') {printf ("%d\n", m1[0] ); }            Else{scanf ("%d", &tmp); if(Cmp_min (m1[0], tmp) {Heap_pop (M1, M-1, cmp_min); m1[m-1] =tmp; Heap_push (M1, M-1, cmp_min); }            }        }    }    return 0;}BOOLcmp_min (elemtype elema, Elemtype elemb) {returnElema <Elemb;}voidHeap_push (Elemtype container[],intNumBOOL(*CMP) (Elemtype Elema, Elemtype elemb)) {     while(Num && cmp (container[num], container[(num-1)/2]) {swap (container[num], container[(Num-1)/2 ] ); Num= (num-1)/2; }}voidHeap_pop (Elemtype container[],intNumBOOL(*CMP) (Elemtype Elema, Elemtype elemb)) {Swap (container[0], container[num]); --num; Elemtype Spot=0; BOOLIsreturn =false;  while(2*spot+2<= num && (CMP (container[2*spot+1], Container[spot]) | | CMP (container[2*spot+2], Container[spot] )) {if(CMP (container[2*spot+1], container[2*spot+2]) {Swap (container[2*spot+1], container[spot]); Spot=2*spot+1; }        Else{swap (container[2*spot+2], container[spot]); Spot=2*spot+2; } Isreturn=true; }    if(! Isreturn &&2*spot+1<= num && cmp (container[2*spot+1], container[spot]) {swap (container[spot], container[2*spot+1 ] ); }}
View Code

The kth great Number_ priority Queue _priority

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.