Created
January 13, 2012 07:40
-
-
Save kuoe0/1605015 to your computer and use it in GitHub Desktop.
[UVa] 501 - Black Box - http://kuoe0.ch/362/uva-501-black-box/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdio> | |
#include <algorithm> | |
#include <queue> | |
#include <vector> | |
using namespace std; | |
int main() | |
{ | |
int t; | |
scanf( "%d", &t ); | |
while ( t-- ) | |
{ | |
int n, m, x; | |
scanf("%d %d", &n, &m ); | |
queue< int > add, get; | |
for ( int i = 0; i < n; ++i ) { | |
scanf( "%d", &x ); | |
add.push( x ); | |
} | |
for ( int i = 0; i < m; ++i ) { | |
scanf( "%d", &x ); | |
get.push( x ); | |
} | |
int cnt = 0; | |
priority_queue< int, vector< int >, greater< int > > MinHeap; | |
priority_queue< int, vector< int >, less< int > > MaxHeap; | |
while ( !add.empty() ) { | |
if ( !MinHeap.empty() && add.front() > MaxHeap.top() ) | |
MinHeap.push( add.front() ); | |
else | |
MaxHeap.push( add.front() ); | |
add.pop(); | |
while ( !get.empty() && MaxHeap.size() + MinHeap.size() == get.front() ) { | |
while ( MaxHeap.size() != cnt ) { | |
if ( MaxHeap.size() < cnt ) { | |
MaxHeap.push( MinHeap.top() ); | |
MinHeap.pop(); | |
} | |
else if ( MaxHeap.size() > cnt ) { | |
MinHeap.push( MaxHeap.top() ); | |
MaxHeap.pop(); | |
} | |
} | |
printf( "%d\n", MinHeap.top() ); | |
++cnt, get.pop(); | |
} | |
} | |
if ( t ) | |
puts( "" ); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment