Ultraviolet A 501-Black Box (priority queue)

Source: Internet
Author: User

Ultraviolet A 501-Black Box (priority queue)

Link to the question: Ultraviolet A 501-Black Box

There is a set in which the given elements enter the order of the set. Now there are Q queries. Given that each query enters the set after the first few elements, for each I query, the number of I in the output set.

Solution: Use two priority queues for maintenance. If queue a has a higher priority value and queue B has a lower priority value, before the I-th inquiry, ensure that a queue has I-1 element elements, and the rest is smaller than B, then each time you ask to output the first element of B queue, and put it in queue.

#include 
  
   #include 
   
    #include 
    
     #include using namespace std;const int maxn = 30005;int N, M, arr[maxn], v[maxn];void solve () {    priority_queue
     
       a;    priority_queue
      
       , greater
       
         > b; for (int i = 1; i <= N; i++) { a.push(arr[i]); b.push(a.top()); a.pop(); while (v[i]--) { printf("%d\n", b.top()); a.push(b.top()); b.pop(); } }}int main () { int cas; scanf("%d", &cas); while (cas--) { int x; memset(v, 0, sizeof(v)); scanf("%d%d", &N, &M); for (int i = 1; i <= N; i++) scanf("%d", &arr[i]); for (int i = 0; i < M; i++) { scanf("%d", &x); v[x]++; } solve(); if (cas) 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.