列表

详情


NC51010. Black Box

描述

Our Black Box represents a primitive database. It can save an integer array and has a special i variable. At the initial moment Black Box is empty and i equals 0. This Black Box processes a sequence of commands (transactions). There are two types of transactions:
ADD (x): put element x into Black Box;
GET: increase i by 1 and give an i-minimum out of all integers containing in the Black Box. Keep in mind that i-minimum is a number located at i-th place after Black Box elements sorting by non- descending.
Let us examine a possible sequence of 11 transactions:
Example 1
N Transaction i Black Box contents after transaction Answer
(elements are arranged by non-descending)
  1.  ADD(3) 0 3
  2.  GET 1 3 3
  3. ADD(1) 1 1, 3
  4. GET 2 1, 3 3
  5. ADD(-4) 2 -4, 1, 3
  6. ADD(2) 2 -4, 1, 2, 3
  7. ADD(8) 2 -4, 1, 2, 3, 8
  8. ADD(-1000) 2 -1000, -4, 1, 2, 3, 8
  9. GET 3 -1000, -4, 1, 2, 3, 8 1
  10. GET 4 -1000, -4, 1, 2, 3, 8 2
  11. ADD(2) 4 -1000, -4, 1, 2, 2, 3, 8
It is required to work out an efficient algorithm which treats a given sequence of transactions. The maximum number of ADD and GET transactions: 30000 of each type.
Let us describe the sequence of transactions by two integer arrays:
  1. A(1), A(2), ..., A(M): a sequence of elements which are being included into Black Box. A values are integers not exceeding 2 000 000 000 by their absolute value, M <= 30000. For the Example we have A=(3, 1, -4, 2, 8, -1000, 2).
  2. u(1), u(2), ..., u(N): a sequence setting a number of elements which are being included into Black Box at the moment of first, second, ... and N-transaction GET. For the Example we have u=(1, 2, 6, 6).
The Black Box algorithm supposes that natural number sequence u(1), u(2), ..., u(N) is sorted in non-descending order,and for each pan inequality is valid. It follows from the fact that for the p-element of our u sequence we perform a GET transaction giving p-minimum number from our A(1), A(2), ..., A(u(p)) sequence.

输入描述

Input contains (in given order): M, N, A(1), A(2), ..., A(M), u(1), u(2), ..., u(N). All numbers are divided by spaces and (or) carriage return characters.

输出描述

Write to the output Black Box answers sequence for a given sequence of transactions, one number each line.

示例1

输入:

7 4
3 1 -4 2 8 -1000 2
1 2 6 6

输出:

3
3
1
2

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 14ms, 内存消耗: 1252K, 提交时间: 2020-03-25 16:13:41

/*
	动态中位数的升级版本
	使用对顶堆
	以i为分割点,大根堆存储前半段,小根堆存储后半段
	大根堆元素个数控制为i个,这样大根堆堆顶就是第i小的元素 
*/ 
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=2e5+10;
int n,m,a[N],K;
priority_queue<int> q_max;
priority_queue<int,vector<int>,greater<int> > q_min;
int main()
{
	scanf("%d%d",&m,&n);
	for(int i=1;i<=m;i++)
		scanf("%d",&a[i]);
	int now=1;
	for(int i=1;i<=n;i++)
	{
		int x;
		scanf("%d",&x);//表示当插入了x个元素之后,就进行GET操作 
		for(int j=now;j<=x;j++)
		{
			q_max.push(a[j]);
			if(q_max.size()==i)
				q_min.push(q_max.top()),q_max.pop(); 
		}
		now=x+1;
		printf("%d\n",q_min.top());
		q_max.push(q_min.top());
		q_min.pop();
	}
	return 0;
}

C++(g++ 7.5.0) 解法, 执行用时: 13ms, 内存消耗: 740K, 提交时间: 2022-10-18 08:38:54

#include<bits/stdc++.h>
using namespace std;
int m,n;
int a[200010];
int main(){
	scanf("%d %d",&m,&n);
	priority_queue<int>A;
	priority_queue<int,vector<int>,greater<int> >B;
	for(int i = 1; i <= m; i++){
		scanf("%d",&a[i]);
	}
	int id = 1;
	for(int i = 1; i <= n; i++){
		int u;
		scanf("%d",&u);
		for(int j = id; j <= u; j++){
			A.push(a[j]);
			if(A.size() == i) B.push(A.top()),A.pop();
		}
		id = u + 1;
		printf("%d\n",B.top());
		A.push(B.top());
		B.pop();
	}
	return 0;
}

C++ 解法, 执行用时: 34ms, 内存消耗: 780K, 提交时间: 2022-01-22 12:38:47

#include <bits/stdc++.h>
using namespace std;
const int N=9999999;
int n, m,a[N],q[N],k=1;
vector<int>v; 
int main() 
{
	cin>>n>>m;
	for(int i=1;i<= n;i++) 
	cin>>a[i];
	for(int i=1;i<=m;i++) 
	cin>>q[i];
	sort(q+1,q+1+m);
	for(int i=1;i<=n;i++) 
	{
		v.insert(lower_bound(v.begin(),v.end(),a[i]),a[i]);
		while(q[k]==i) 
		{
			cout<<v[k-1]<<endl;
			k++;
		}
	}
}

上一题