Ultraviolet A 501-Black Box (priority queue)

Source: Internet
Author: User

Ultraviolet A 501-Black Box (priority queue)
Ultraviolet A 501-Black Box

Question Link

Given some operations, add a number each time, and then output the current number smaller than I during the I-th query.

Idea: using two priority queues is equivalent to dividing the sequence into two sequences. The answer for each query is the header of the next queue.

Code:

#include 
 
  #include 
  
   #include 
   
    #include 
    
     using namespace std;const int N = 30005;int t, m, q, vis[N], num[N];void solve() {    priority_queue
     
       a;    priority_queue
      
       , greater
       
         > b; for (int i = 1; i <= m; i++) {a.push(num[i]);b.push(a.top());a.pop();while (vis[i]--) { printf("%d\n", b.top()); a.push(b.top()); b.pop();} }}int main() { scanf("%d", &t); while (t--) {scanf("%d%d", &m, &q);for (int i = 1; i <= m; i++) scanf("%d", &num[i]);memset(vis, 0, sizeof(vis));while (q--) { scanf("%d", &num[0]); vis[num[0]]++;}solve();if (t) printf("\n"); } return 0;}
       
      
     
    
   
  
 


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.