Black Box
http://poj.org/problem?id=1442
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<algorithm> 3 #include<queue> 4 #define MAXN 30010 5 long long a[MAXN],b[MAXN]; 6 using namespace std; 7 int main() 8 { 9 priority_queue<long long ,vector<long long >,greater<long long> >q; //从小到大排列 小根堆 10 priority_queue<long long ,vector<long long>,less<long long> >p;// 从大到小排列 大根堆 11 int n,m; 12 long long t; 13 scanf("%d%d",&n,&m); 14 for(int i=0;i<n;i++) 15 scanf("%lld",&a[i]); 16 for(int i=0;i<m;i++) 17 scanf("%lld",&b[i]); 18 int tt=0; 19 for(int j=0;j<m;j++) 20 { 21 for( int i=tt;i<b[j];i++) 22 { 23 q.push(a[i]); 24 if(!p.empty()&&q.top()<p.top()) //对每一步add之后操作 25 { 26 t=q.top(); 27 q.pop(); 28 q.push(p.top()); 29 p.pop(); 30 p.push(t); 31 } 32 } 33 tt=b[j]; 34 printf("%lld\n",q.top()); 35 p.push(q.top()); 36 q.pop(); 37 } 38 return 0; 39 }
另外的做法,这是我用堆写的。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #define MAXN 60010 5 long long a[MAXN],b[MAXN],c[MAXN],d[MAXN]; 6 using namespace std; 7 int len=0,len1=0; 8 void up1(int n) 9 { 10 a[++len]=n; 11 int p=len; 12 int q=p/2; 13 long long m=a[p]; 14 while(q>=1&&m<a[q]) 15 { 16 a[p]=a[q]; 17 p=q; 18 q=p/2; 19 } 20 a[p]=m; 21 } 22 void up2(int n) 23 { 24 b[++len1]=n; 25 int p=len1; 26 int q=p/2; 27 long long m=b[p]; 28 while(q>=1&&m>b[q]) 29 { 30 b[p]=b[q]; 31 p=q; 32 q=p/2; 33 } 34 b[p]=m; 35 } 36 void down1(int p) 37 { 38 a[1]=a[len--]; 39 int q=p*2; 40 long long m=a[p]; 41 while(q<=len) 42 { 43 if(q<len&&a[q]>a[q+1]) 44 q++; 45 if(a[q]>=m) break; 46 else 47 { 48 a[p]=a[q]; 49 p=q; 50 q=p*2; 51 } 52 } 53 a[p]=m; 54 } 55 int pop1() 56 { 57 long long r=a[1]; 58 return r; 59 } 60 int pop2() 61 { 62 long long r=b[1]; 63 return r; 64 } 65 void down2(int p) 66 { 67 b[1]=b[len1--]; 68 int q=p*2; 69 long long m=b[p]; 70 while(q<=len1) 71 { 72 if(q<len1&&b[q]<b[q+1]) 73 q++; 74 if(b[q]<=m) break; 75 else 76 { 77 b[p]=b[q]; 78 p=q; 79 q=p*2; 80 } 81 } 82 b[p]=m; 83 } 84 85 int main() 86 { 87 int n,m; 88 memset(a,0,sizeof(a)); 89 memset(b,0,sizeof(b)); 90 scanf("%d%d",&n,&m); 91 for(int i=0; i<n; i++) 92 scanf("%lld",&c[i]); 93 for(int j=0; j<m; j++) 94 scanf("%lld",&d[j]); 95 int tt=0; 96 for(int i=0; i<m; i++) 97 { 98 for(int j=tt; j<d[i]; j++) 99 { 100 up1(c[j]); 101 if(len1!=0&&a[1]<b[1]) 102 { 103 int t=pop1(); 104 down1(1); 105 up1(pop2()); 106 down2(1); 107 up2(t); 108 } 109 } 110 tt=d[i]; 111 printf("%lld\n",a[1]); 112 up2(pop1()); 113 down1(1); 114 } 115 return 0; 116 }