PAT-A1029題解---解決內存超過限制


1029 Median (25 分)
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 Specification:
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 (≤2×10
​5
​​ ) 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 Specification:
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


題目大意:題意算是比較好理解的,就是讀入兩個數組,然後找出兩個數組合並之後的中間數,如果總個數爲偶數,則爲中間兩個的左邊的那個數。


題目思路:題意看上去就賊簡單,所以想都沒想,就直接開了個大數組讀入,找位置輸出就行了,所以一開始寫的代碼是這樣的。

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1000010;
int s[2*maxn];
int main(){
	int n,m;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&s[i]);
	}
	scanf("%d",&m);
	for(int i=n;i<n+m;i++){
		scanf("%d",&s[i]);
	}
	int num=n+m;
	sort(s,s+num);
	if(num%2==0){
		printf("%d",s[num/2-1]);
	}
	else
		printf("%d",s[(num-1)/2]);
	return 0;
} 

這樣寫絕大部分的測試點都可以通過,但是會有一個測試點失敗,超出題目內存的限制了,所以直接開大數組暴力,把這題當作簡單模擬寫是不行的。


後來呢,看了一眼算法筆記,說是用two points來做的,想偷個懶,就直接看了晴神的code,沒想到晴神這個也是卡在了內存超限點,不過two points的思想還是可以對內存空間進行優化的。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 1000001;
const int inf = 0x7fffffff;
int s1[maxn], s2[maxn];
int main() {
    int n, m;
    scanf("%d", &n);
    for(int i = 0; i < n; i++) {
        scanf("%d", &s1[i]);
    }
    scanf("%d", &m);
    for(int i = 0; i < m; i++) {
        scanf("%d", &s2[i]);
    }
    s1[n] = s2[m] = inf;
    int mid = (n + m - 1) / 2;
    int i = 0, j = 0, count = 0;
    while(count < mid) {
        if(s1[i] < s2[j]) {
            i++;
        } else {
            j++;
        }
        count++;
    }
    if(s1[i] < s2[j]) {
        printf("%d\n", s1[i]);
    } else {
        printf("%d\n", s2[j]);
    }
    return 0;
}

方法就是僅讀入s1,然後s2中的元素並不需要儲存,只需要進行另一個point的移動比較就可以了,用隊列的方法實現是最簡單的,但是C++的庫有點忘了,以後再填這個坑。


//AC code
#include<iostream>
using namespace std;
int k[200005];
int main(){
    int n,m,temp,count=0;
    cin>>n;
    for(int i=1;i<=n;i++)   
        scanf("%d",&k[i]);
    k[n+1]=0x7fffffff;
    cin>>m;
    int mid=(n+m+1)/2,i=1;
    for(int j=1;j<=m;j++){
        scanf("%d",&temp);
        while(k[i]<temp){ 		//找到s中第一個小於temp的 
            count++;
            if(count==mid)
				cout<<k[i];
            i++;
        }
        count++;
        if(count==mid) 
			cout<<temp;
    }
    while(i<=n){  				//如果讀完s2還不能達到中點 
        count++;
        if(count==mid) 
			cout<<k[i];
        i++;
    }
    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章