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