1. 程式人生 > >pta--1029 Median(25 分)(思維&&解決記憶體超限)*

pta--1029 Median(25 分)(思維&&解決記憶體超限)*

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

【分析】題意挺好理解的吧,就是輸出兩個序列合併後的中位數。注意兩個序列都是有序的。一開始直接用sort了,但是最後一個測試點說記憶體超限。然後把int都改成了long long,,,,然後,最後4個點都是記憶體超限。好吧,方法問題了~

分析:開一個數組,線上處理第二個陣列。 第一二個數組(下標從1開始)分別有n,m個元素,中間數在(n + m + 1) / 2的位置。所以只要從小到大數到(n + m + 1) / 2的位置就行了~ count計總個數 ,給第一個陣列設定指標i,每次從第二個陣列中讀入temp,檢查第一個陣列中前幾個數是不是比temp小,小就count+1並判斷是否到數了中間數,到了就輸出。 如果數完比temp小的數還沒到中間數,count+1,檢查temp是不是中間數,是就輸出。迴圈上述過程。如果第二個陣列讀取完了,還沒數到中間數,說明中間數在剩下的第一個陣列中,就在剩下的陣列中數到中間數位置即可

【程式碼】

#include<iostream>
#include<cstdio>
using namespace std;
int k[200005];
int main()
{
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
		scanf("%d",&k[i]);
	int m;
	scanf("%d",&m);
	int temp,cnt=0,i=1;
	k[n+1]=0x7fffffff;//long int的最大值
	int midpos=(n+m+1)/2;
	for(int j=1;j<=m;j++)
	{
		scanf("%d",&temp);
		while(k[i]<temp)
		{
			cnt++;
			if(cnt==midpos)cout<<k[i];
			i++;
		}
		cnt++;
		if(cnt==midpos)cout<<temp;
	}
	while(i<=n)
	{
		cnt++;
		if(cnt==midpos)cout<<k[i];
		i++;
	}
	return 0;
}

相關推薦

pta--1029 Median25 思維&&解決記憶體*

1029 Median(25 分) Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median

PAT甲級 1029 Median25 AC 解決記憶體

1029 Median(25 分) Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the

CCF2015.3 第一題:影象旋轉(java) 80記憶體

CCF2015.3 第一題:影象旋轉(java) 問題描述   旋轉是影象處理的基本操作,在這個問題中,你需要將一個影象逆時針旋轉90度。   計算機中的影象表示可以用一個矩陣來表示,為了旋轉一個影象,只需要將對應的矩陣旋轉即可。 輸入格式   輸入的第一行包含兩個整數n, m,分別表示

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

【笨方法學PAT】【未完成】1029 Median 25

一、題目 Given a set of N (>1) positive integers, you are supposed to partition them into two disjoint sets A​1​​ and A​2​

1029 Median 25 【邊輸入邊輸出】

1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median

PTA 樹的同構25

其中 content turn 不同 是否 std itl ng- class 7-1 樹的同構(25 分) 給定兩棵樹T1和T2。如果T1可以通過若幹次左右孩子互換就變成T2,則我們稱兩棵樹是“同構”的。例如圖1給出的兩棵樹就是同構的,因為我們把其中一棵樹的結點A、

PTA 人以群25

-o fontsize diff 討論 can tro lis 絕對值 -i 7-2 人以群分(25 分) 社交網絡中我們給每個人定義了一個“活躍度”,現希望根據這個指標把人群分為兩大類,即外向型(outgoing,即活躍度高的)和內向型(introverted,即活

PTA 旅遊規劃25

title 收費 () 輸出 格式 分開 i++ namespace push 7-10 旅遊規劃(25 分) 有了一張自駕旅遊路線圖,你會知道城市間的高速公路長度、以及該公路要收取的過路費。現在需要你寫一個程序,幫助前來咨詢的遊客找一條出發地和目的地之間的最短路徑。

PTA乙級刷題日誌---100525繼續(3n+1)猜想

卡拉茲(Callatz)猜想已經在1001中給出了描述。在這個題目裡,情況稍微有些複雜。 當我們驗證卡拉茲猜想的時候,為了避免重複計算,可以記錄下遞推過程中遇到的每一個數。例如對 n=3 進行驗證的時候,我們需要計算 3、5、8、4、2、1,則當我們對 n=5、8、

PTA活用遞推類 1093 Count PAT's 25 ,1101 Quick Sort 25

思路 對於一類涉及序列的問題, 假如序列中每一位的值都可以通過“左右關係”計算得到,可以考慮所謂的“左右兩側的結果”是否可以通過遞推的預處理來得到,後面的計算就不用反覆求解。 ——演算法筆記 A1093 處理的是PAT三個字母之間的前後關係, 計數只與‘A’左右

PTA 1012 The Best Rank 25

1012 The Best Rank (25 分) To evaluate the performance of our first year CS majored students, we consider their grades of three cour

PTA 中國大學MOOC-陳越、何欽銘-資料結構-2018秋 05-樹8 File Transfer 25 並查集

We have a network of computers and a list of bi-directional connections. Each of these connections allows a file transfer from one compute

PTA團體程式設計天梯賽 L2-027 名人堂與代金券 25

L2-027 名人堂與代金券 (25 分) 原題連結 對於在中國大學MOOC(http://www.icourse163.org/ )學習“資料結構”課程的學生,想要獲得一張合格證書,總評成績必須達到 60 分及以上,並且有另加福利:總評分在 [G, 100]

PTA 01-複雜度2 Maximum Subsequence Sum 25

01-複雜度2 Maximum Subsequence Sum (25 分) Given a sequence of K integers { N​1​​, N​2​​, …, N​K​​ }. A continuous subsequence is defin

PTA 資料結構與演算法題目集中文7-37 模擬EXCEL排序25 排序

Excel可以對一組紀錄按任意指定列排序。現請編寫程式實現類似功能。 輸入格式: 輸入的第一行包含兩個正整數N(≤10​5​​) 和C,其中N是紀錄的條數,C是指定排序的列號。之後有 N行,每行包含一條學生紀錄。每條學生紀錄由學號(6位數字,保證沒有重複的學號)、姓名(不

PTA 7-21排序 PAT排名彙總25 25程式碼

排序, 每個考點排序,最後整體排序 結構體比較方便 (注:部落格作為交流使用,切勿抄襲應付作業) #include<bits/stdc++.h> using namespace std;

PTA 資料結構與演算法題目集中文 7-35 城市間緊急救援25 迪傑斯特拉演算法

7-35 城市間緊急救援(25 分) 作為一個城市的應急救援隊伍的負責人,你有一張特殊的全國地圖。在地圖上顯示有多個分散的城市和一些連線城市的快速道路。每個城市的救援隊數量和每一條連線兩個城市的快速道路長度都標在地圖上。當其他城市有緊急求助電話給你的時候,你的任務是帶領你的

PTA 資料結構與演算法題目集中文7-38 尋找大富翁25 快排或堆排序

胡潤研究院的調查顯示,截至2017年底,中國個人資產超過1億元的高淨值人群達15萬人。假設給出N個人的個人資產值,請快速找出資產排前M位的大富翁。 輸入格式: 輸入首先給出兩個正整數N(≤10​6​​)和M(≤10),其中N為總人數,M為需要找出的大富翁數;接下來一行給出

PTA 6-2 二叉樹的遍歷25 25程式碼 陣列實現層次遍歷

前三個 中序 先序 後序遍歷直接遞迴就可以了 最後一個層次遍歷 可以把每一層 用陣列存起來,容易實現 (注: 部落格作為交流使用,請勿抄襲應付作業) /* 你的程式碼將被嵌在這裡 */ void