现在的位置: 首页 > 综合 > 正文

PAT 1029. Median (25)

2017年11月17日 ⁄ 综合 ⁄ 共 3136字 ⁄ 字号 评论关闭

http://pat.zju.edu.cn/contests/pat-a-practise/1029

Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1={11, 12, 13, 14} is 12, and the median of S2={9, 10, 15, 16, 17} is 15. The median of two sequences is defined to be the median
of the nondecreasing sequence which contains all the elements of both sequences. For example, the median of S1 and S2 is 13.

Given two increasing sequences of integers, you are asked to find their median.

Input

Each input file contains one test case. Each case occupies 2 lines, each gives the information of a sequence. For each sequence, the first positive integer N (<=1000000) is the size of that sequence. Then N integers follow, separated by a space. It is guaranteed
that all the integers are in the range of long int.

Output

For each test case you should output the median of the two given sequences in a line.

Sample Input

4 11 12 13 14
5 9 10 15 16 17

Sample Output

13

直接合并数组然后sort会超时,这里考虑用归并排序,复杂度为O(N)。。。。

但是这里还有两个坑:1、输入用cin会超时,只能用scanf   2、注意空间问题,如果使用三个vector存序列的话,内存会超限,可以考虑用队列,进队一个删除一个,或者就不建新的队列,直接定义标号查找

题目不难,但是烂坑真特么多。。调了好久才从坑里爬出来→_→

超时代码: 

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int main(){
	int n1, n2;
	long x;
	vector<long> v1, v2, v3;
	scanf("%d", &n1);
	for (int i = 0; i < n1; i++){
		cin >> x;
		v1.push_back(x);
	}
	scanf("%d", &n2);
	for (int i = 0; i < n2; i++){
		cin >> x;
		v2.push_back(x);
	}
	int i = 0, j = 0;
	while(i < n1 && j < n2){
		if (v1[i] <= v2[j]){
			v3.push_back(v1[i]);
			i++;
		}
		else if(v1[i] > v2[j]){
			v3.push_back(v2[j]);
			j++;
		}
	}
	if (i == n1){
		for (; j < n2; j++)
			v3.push_back(v2[j]);
	}
	else if (j == n2){
		for (; i < n1; i++)
			v3.push_back(v1[i]);
	}

	cout << v3[(n1 + n2 - 1) / 2] << endl;
	return 0;
}

内存超限代码:

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int main(){
	int n1, n2;
	long x;
	vector<long> v1, v2, v3;
	scanf("%d", &n1);
	for (int i = 0; i < n1; i++){
		scanf("%ld", &x);
		v1.push_back(x);
	}
	scanf("%d", &n2);
	for (int i = 0; i < n2; i++){
		scanf("%ld", &x);
		v2.push_back(x);
	}
	int i = 0, j = 0;
	while (i < n1 && j < n2){
		if (v1[i] <= v2[j]){
			v3.push_back(v1[i]);
			i++;
		}
		else if (v1[i] > v2[j]){
			v3.push_back(v2[j]);
			j++;
		}
	}
	if (i == n1){
		for (; j < n2; j++)
			v3.push_back(v2[j]);
	}
	else if (j == n2){
		for (; i < n1; i++)
			v3.push_back(v1[i]);
	}

	cout << v3[(n1 + n2 - 1) / 2] << endl;
	return 0;
}

使用deque的AC代码:

#include <iostream>
#include <cstdio>
#include <deque>
using namespace std;

int main(){
	int n1, n2, i;
	deque<long> s1, s2, s;
	long tmp;
	scanf("%d", &n1);
	for (i = 0; i < n1; i++){
		scanf("%ld", &tmp);
		s1.push_back(tmp);
	}
	scanf("%d", &n2);
	for (i = 0; i < n2; i++){
		scanf("%ld", &tmp);
		s2.push_back(tmp);
	}
	while (!s1.empty() && !s2.empty()){
		if (s1.front() <= s2.front()){
			s.push_back(s1.front());
			s1.pop_front();
		}
		else{
			s.push_back(s2.front());
			s2.pop_front();
		}
	}
	if (s1.empty()){
		while (!s2.empty()){
			s.push_back(s2.front());
			s2.pop_front();
		}
	}
	else if (s2.empty()){
		while (!s1.empty()){
			s.push_back(s1.front());
			s1.pop_front();
		}
	}
	cout << s[(s.size() - 1) / 2] << endl;
	return 0;
}

直接用标号查找的AC代码:

#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
int main(){
	int n1, n2;
	long x;
	vector<long> v1, v2;
	scanf("%d", &n1);
	for (int i = 0; i < n1; i++){
		scanf("%ld", &x);
		v1.push_back(x);
	}
	scanf("%d", &n2);
	for (int i = 0; i < n2; i++){
		scanf("%ld", &x);
		v2.push_back(x);
	}
	int i = 0, j = 0, index = 0, k = (n1 + n2 - 1) / 2;
	while (i < n1 && j < n2){
		if (v1[i] <= v2[j]){
			if (index== k){
				printf("%ld\n", v1[i]);
				break;
			}
			index++;
			i++;
		}
		else if (v1[i] > v2[j]){
			if (index == k){
				printf("%ld\n", v2[j]);
				break;
			}
			index++;
			j++;
		}
	}
	if (i == n1)
		printf("%ld\n", v2[k+j-index]);
	else if (j == n2)
		printf("%ld\n", v1[k+i-index]);
	return 0;
}


抱歉!评论已关闭.