Poj1442 (black box)

Source: Internet
Author: User

Address: Black Box

 

Question:

There is a black box for two operations, one is add to save the number in the box, the other is get to find the number in the box, the default box is always arranged from small to large, and then a (1), a (2 ).... A (M) is the number of boxes to be saved, u (1), u (2 ),.... the number of U (n) is not the number of the given number, but here the number of U (n) is inserted into the box a (m, here there is a P (1 <= P <= N) and U are in the same sequence. Output p-minimum number from our A (1), a (2 ),..., A (U (p) sequence. P starts from 1 to n, and the number of P outputs the smallest number.

 

Solution:

First, sort timeout is used for each loop.

Code:

 1 const int M=30005; 2 int s2[M],s1[M],s3[M]; 3 int main() 4 { 5     int m,n; 6     priority_queue<int >Q; 7     while(scanf("%d%d",&m,&n)!=EOF) 8     { 9         memset(s1,0,sizeof(s1));10         memset(s2,0,sizeof(s2));11         int i,j,k;12         for(i=1;i<=m;i++)13             scanf("%d",&s1[i]);14         int d=1;15         int cnt=0;16         for(i=1;i<=n;i++)17         {18             scanf("%d",&s2[i]);19             for(j=d;j<=s2[i];j++)20             {21                 d=j+1;22                 s3[++cnt]=s1[j];23             }24             sort(s3,s3+cnt+1);25             printf("%d\n",s3[i]);26         }27     }28     return 0;29 }
View code

The worst time complexity of sort is N * n, which is generally nlog (N). Therefore, it takes a lot of time for each loop.

The time complexity of the priority queue is the worst log (n ).

First, use the priority queue to create a minimum heap and a maximum heap, so that the maximum heap always stores a small number of P before. Therefore, the top heap of the maximum heap is the get required by the question. The minimum heap always stores the remaining number. The key is how to push to the max, and Min heap. When P = 1. If Q2 is blank, push all of the shortest heap q1.top q1.top () to q2. At this time, there is a number in Q2. When p = 2, you need to push to Q1. If there is a comparison between q2.top () and then swap the maximum and minimum stack top, finally, pass q1.top () of the minimum heap to the maximum q1.top (), and so on... this ensures that Q2 always stores a small number of the first p.

Code:

1 # include <algorithm> 2 # include <iostream> 3 # include <sstream> 4 # include <cstdlib> 5 # include <cstring> 6 # include <cstdio> 7 # include <string> 8 # include <bitset> 9 # include <vector> 10 # include <queue> 11 # include <stack> 12 # include <cmath> 13 # include <list> 14/ /# include <map> 15 # include <set> 16 using namespace STD; 17 /************************************** */18 # define ll long long19 # define in T64 _ int6420 /*********************************** * ***/21 const int INF = 0x7f7f7f7f; 22 const double EPS = 1e-8; 23 const double PIE = ACOs (-1.0); 24 const int d1x [] = {0,-, 1 }; 25 const int d1y [] = {-,}; 26 const int d2x [] = {0,-, 1}; 27 const int d2y [] =, -}; 28 const int FX [] = {-1,-1,-,}; 29 const int FY [] = {-, 1, -,-, 1}; 30/* vector <int> map [N]; Map [A]. push_back (B); int Len = m AP [v]. size (); */31 /************************************ * **/32 void openfile () 33 {34 freopen ("data. in "," rb ", stdin); 35 freopen (" data. out "," WB ", stdout); 36} 37 priority_queue <int> qi1; 38 priority_queue <int, vector <int>, greater <int> qi2; 39/*********************** Lili split line, the above is the template section ***************/40 const int M = 30005; 41 int S1 [m], s2 [m]; 42 int main () 43 {44 int M, N; 45 priority_queue <int> Q2; 46 priority_q Ueue <int, vector <int>, greater <int> q1; 47 While (scanf ("% d", & M, & N )! = EOF) 48 {49 memset (S1, 0, sizeof (S1); 50 memset (S2, 0, sizeof (S2); 51 int I, J, K; 52 for (I = 1; I <= m; I ++) 53 scanf ("% d", & S1 [I]); 54 int d = 1; 55 For (I = 1; I <= N; I ++) 56 {57 scanf ("% d", & S2 [I]); 58 for (j = D; j <= S2 [I]; j ++) 59 {60 d = J + 1; 61 q1.push (S1 [J]); 62 if (! Q2.empty () & q1.top () <q2.top () 63 {64 int V1 = q1.top (); 65 int v2 = q2.top (); 66 q1.pop (); 67 q1.push (V2); 68 q2.pop (); 69 q2.push (V1); 70} 71} 72 int v = q1.top (); 73 q2.push (V); 74 q1.pop (); 75 printf ("% d \ n", q2.top (); 76} 77} 78 return 0; 79}
View code

 

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.