HDU 4006 the kth great number (priority queue)

Source: Internet
Author: User

Simple and naked priority queue question!

The children were stunned. I made a mistake several times !!!

The question requires that the number k be output. The value is always updated, so the common method will time out,

In this case, you can use a value to record the number of inputs. If the number is smaller than K, the number entered is firmly in the column,

Otherwise, the number entered is compared with the number of K in the column. If the number is greater than the number of K in the column, delete the number of K in the column!

# Include <stdio. h>

# Include <string. h>
# Include <queue>
Using namespace STD;

Struct Node
{
Int X;
Friend bool operator <(node A, Node B)
{
Return A. x> B. X;
}
};

Int main ()
{

Int N, K, I, m, count;
Node cur;
Char STR [10];
While (scanf ("% d", & N, & K )! = EOF)
{
Priority_queue <node> q;
Count = 0;
For (I = 0; I <n; I ++)
{
Scanf ("% s", STR );
If (STR [0] = 'I ')
{
Scanf ("% d", & M );
Cur. x = m;
Count ++;
If (count <= K)
{
Q. Push (cur );

}
Else

{// At the beginning, we wrote cur = Q. top (); If (M> cur. x) Well, it's wrong here. cur has assigned a value on it, and assigned a value here. It's decisive to lose the original data, so wrong, dizzy!

// Low-level error !!

If (M> q. Top (). X)
{
Q. Pop ();
Q. Push (cur );
}
}
}
Else
{
Cur = Q. Top ();
Printf ("% d \ n", cur. X );

}
}
}
Return 0;
}

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4006

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.