2103 - 黑匣子

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

我这里有个黑匣子,以及i,初始的时候黑匣子里面什么数据都没有,并且i为0

我们可以通过ADD(X)函数将X放入黑匣子内,黑匣子内的数据总是以不递减的方式排列,不管新插入的是什么数据。我们也可以通过get()函数得到黑匣子里面排在第i位置的数据,之后i会变成i + 1。

例如 1.ADD(1)  --------- 1

 2.ADD(3)  ---------- 1 3

 3.ADD(5)  -----------1 3 5

 4.ADD(3)  -----------1 3 3 5

 GET()    -----------1 3 3 5     i = 0   返回1

 GET()    -----------1 3 3 5     i = 1   返回3

 5.ADD(-1000) -------- -1000  1  3  3  5

 GET()    ----------- -1000  1  3  3  5  i = 2  返回 3


题目输入

第一行为M, N。M表示总共要插入黑匣子M个数据(M ≤ 30000),N代表总共要使用GET()的询问次数 (1 ≤ N ≤ M)。

第二行为M个数据(插入数据的绝对值不超过2 000 000 000)

第三行为询问值,表示在插入第几个数据之后进行GET()询问。(这里的询问值都是按照非递减的顺序排列,并且保证每次询问都是可以正确返回结果的)

题目输出

每次询问返回的值

输入/输出样例

输入格式

5 3
1 3 5 3 -1000
4 4 5

输出格式

1
3
3

C语言解答

#include <stdio.h>
#include <stdlib.h>

struct list{
    int num;
    struct list *next;
};
typedef struct list Sqlist;

Sqlist* qAdd(Sqlist*, const int);
int qDel(Sqlist**);
Sqlist* ADD(Sqlist*, const int);
int GET(Sqlist*, const int);

int main(){
	
	int M, N, i, j, k, command, tmp, num;
	Sqlist *head;
	Sqlist *rear;
	
	head = (Sqlist*)malloc(sizeof (Sqlist));
	rear = (Sqlist*)malloc(sizeof (Sqlist));
	
	while (EOF != scanf("%d%d", &M, &N)){
		
		head->next = NULL;
		rear->next = rear;
		
		for (i = 0; i < M; i++){
			scanf("%d", &num);
			rear = qAdd(rear, num);
		}
		
		i = 0;
		k = 0;
		for (j = 0; j < N; j++){
			scanf("%d", &command);
			while (k < command){
				head = ADD(head, qDel(&rear));
				k++;
			}
			tmp = GET(head, i);
			i++;
			printf("%d\n", tmp);
		}
	}
	
	return 0;
}

Sqlist* ADD(Sqlist* head, const int num)
{
	Sqlist *p, *q, *s;
	p = head;
	while (q = p->next){
		if (q == NULL){
			break;
		}
		if (q->num < num){
			p = q;
		}else{
			break;
		}
	}
	s = (Sqlist*)malloc(sizeof (Sqlist));
	s->num = num;
	s->next = q;
	p->next= s;
	
	return head;
}

int GET(Sqlist* head, const int num)
{
	int i;
	Sqlist *q;
	q = head->next;
	
	for (i = 0; i < num; i++){
		if (q == NULL){
			break;
		}
		q = q->next;	
	}
	
	return q->num;
}

Sqlist* qAdd(Sqlist* rear, const int num)
{
	Sqlist *head, *s;
	s = (Sqlist*)malloc(sizeof (Sqlist));
	s->num = num;
	s->next = rear->next;;
	rear->next = s;
	rear = s;
	return rear;
}

int qDel(Sqlist** _rear)
{
	int tmp;
	Sqlist *head, *q;
	head = (*_rear)->next;
	q = head->next;
	head->next = q->next;
	tmp = q->num;
	free(q);
	return tmp;
}

C++解答

#include<stdio.h>
#include<queue>
#include<vector>
#include <cstring>
#include<algorithm>
#define MAX 30010

using namespace std;

int num[MAX], a[MAX];

int main(){
	int i, j, n, m, tem;
	//freopen("1.in", "r", stdin);
	//freopen("1.out", "w", stdout);
	while(scanf("%d%d", &n, &m) != EOF){
		priority_queue<int, vector<int>, greater<int> > Squeue;
		priority_queue<int, vector<int>, less<int> > Gqueue;
		memset(a, 0, sizeof(a));
		for(i = 1; i <= n; i++){
			scanf("%d", &num[i]);
		}
		for(i = 1; i <= m; i++){
			scanf("%d", &a[i]);
		}
		j = 1;
		for(i = 1; i <= n; i++){
			Squeue.push(num[i]);
			if(!Gqueue.empty() && Squeue.top() < Gqueue.top()){
				int S = Squeue.top();
				int G = Gqueue.top();
				Squeue.pop();
				Gqueue.pop();
				Squeue.push(G);
				Gqueue.push(S);
			}
			while(a[j] == i){
				printf("%d\n", Squeue.top());
				Gqueue.push(Squeue.top());
				Squeue.pop();
				j++;
			}
		}
	}
	return 0;
}