PAT 1057 分块思想

题目

Stack is one of the most fundamental data structures, which is based on the principle of Last In First Out (LIFO). The basic operations include Push (inserting an element onto the top position) and Pop (deleting the top element). Now you are supposed to implement a stack with an extra operation: PeekMedian – return the median value of all the elements in the stack. With N elements, the median value is defined to be the (N/2)-th smallest element if N is even, or ((N+1)/2)-th if N is odd.

Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (≤105
​​ ). Then N lines follow, each contains a command in one of the following 3 formats:

Push key
Pop
PeekMedian

where key is a positive integer no more than 10​5
​​ .

Output Specification:
For each Push command, insert key into the stack and output nothing. For each Pop or PeekMedian command, print in a line the corresponding returned value. If the command is invalid, print Invalid instead.

Sample Input:
17
Pop
PeekMedian
Push 3
PeekMedian
Push 2
PeekMedian
Push 1
PeekMedian
Pop
Pop
Push 5
Push 4
PeekMedian
Pop
Pop
Pop
Pop

Sample Output:

Invalid
Invalid
3
2
2
1
2
4
4
5
3
Invalid

题目最难的是要求动态获得中位数,所以先学习一下分块思想

分块思想

  1. 如果要动态获得一个中位数,最基本的方法是将所有数据排好序,然后取中位数的位置,也就是 N / 2(这里举例偶数), 但是要排整个数据从获得中位数的角度来说是没必要的,因为除了中位数左右以外,其余的数据不需要整体都有序
  2. 所以可以记录每个部分的值的个数,比如一共数据中有20个数据,我们只需要知道前9个是哪个范围的,然后从那个范围往后找第一个数据就是中位数
  3. 这样的话很容易联想到了hash,可以使用一个table数组,以下标为数据,数组值为该数字的个数。
  4. 这里开始引入分块,如果只是单纯地记录hash值,要从头累加一遍需要的时间仍然是O(N)级的,这里就联系到了第二点所说的我们并不需要整体有序,也就是说我们只需要知道每个部分有几个就好了
  5. 将数组分块,比如已知数组最大值是9(不考虑有数字是0),我们可以分成三组,分别是 1~ 3 ,4~5,6 ~ 9,我们只需要知道每部分的数字个数就可以大量减少时间
  6. 一般分组个数为 √最大值 + 1
  7. 假设要找第K大的数据,则可以从0开始求和block[]数组(分块数组)的每一个值,直到大于k,则证明第k大的值一定在这里,然后再从这个块內找
  8. 这样的话,搜索的成本就从O(N)下降到了O(√N)

代码和思路

  1. 有了上述的思想,我们就可以顺利的找到中位数,因为中位数就是第 N / 2 大的元素(以偶数为例)
  2. 剩余的Push和Pop其实直接用STL就行,也不会超时的。也就是说没必要把三种操作都放在stack里
  3. 然后就是读入问题,因为在读Push的时候有空格,所以要用getline读入,使用getline和string对时间影响也不大
  4. 然后判断的时候只需要取字符串的第二个字符看一下是啥就行
#include<cstdio>
#include<stack>
#include<string>
#include<iostream>
using namespace std;
const int maxn = 100010;
const int sqrN = 316;

stack<int> s;
int table[maxn];
int block[maxn];
int n;

void Push(int x) {
	s.push(x);
	block[x / 316]++;
	table[x]++;
}

void Pop() {
	int x = s.top();
	s.pop();
	block[x / 316]--;
	table[x]--;
	printf("%d\n", x);
}

void PeekMedian(int k) {
	int sum = 0;
	int index = 0;
	while (sum < k) {
		sum += block[index];
		index++;
	}
	index--;
	sum -= block[index];
	int j = index * 316;
	while (sum < k) {
		sum += table[j];
		j++;
	}
	printf("%d\n", --j);
}

int main() {
	string str;
	scanf("%d", &n);
	getchar();
	for (int i = 0; i < n; i++) {
		getline(cin, str);
		char flag = str[1];
		if (flag == 'u') {
			int key = 0;
			int j = 5;
			while (j < str.length()) {
				key = key * 10;
				key += str[j] - '0';
				j++;
			}
			Push(key);
		}
		else if (flag == 'o') {
			if (s.size() == 0) printf("Invalid\n");
			else Pop();
		}
		else if (flag == 'e') {
			if(s.size() == 0) printf("Invalid\n");
			else {
				int k = s.size();
				if (k % 2 != 0) k = (k + 1) / 2;
				else k = k / 2;
				PeekMedian(k);
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值