PAT甲级——A1029 Median

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 (≤) 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

这道题,我要说道说道;这道题和考究内存的使用,如果你的代码会去保存输入的两组数据,我相信,测试会提示“超内存”的错误;所以,就保存一组数据,与另一组数据进行比较就行,不用保存;一般简单的int数组远比任何容器都省内存!!!
 1 #include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int res[200005], m, n, i, j, a, index = 0;
 6     cin >> m;
 7     for (i = 1; i <= m; ++i)
 8         cin >> res[i];
 9     cin >> n;
10     for (i = 1,j=1; i <= n; ++i)
11     {
12         cin >> a;
13         while (j <= m && a > res[j])
14         {
15             ++index;
16             if (index == (m + n + 1) / 2)
17             {
18                 cout << res[j] << endl;
19                 return 0;
20             }
21             ++j;
22         }
23         ++index;
24         if (index == (m + n + 1) / 2)
25         {
26             cout << a << endl;
27             return 0;
28         }
29     }
30     while(j <= m)
31     {
32         ++index;
33         if (index == (m + n + 1) / 2)
34         {
35             cout << res[j] << endl;
36             return 0;
37         }
38         ++j;
39     }
40     return 0;
41 }

原文地址:https://www.cnblogs.com/zzw1024/p/11219687.html

时间: 2024-10-08 14:20:49

PAT甲级——A1029 Median的相关文章

PAT 甲级 1029 Median

https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968 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 S

PAT 甲级 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 sequence

PAT甲级1005 Spell It Right

题目:PAT甲级 1005 题解:水题.看到题目的第一时间就在想一位一位的mod,最后一加一转换就完事了.结果看到了N最大为10的100的次方,吓得我赶紧放弃这个想法... 发现碰到这种情况用字符串十分好用,这道题应该考察的就是这一点.大致思路就是把数字的每一位放到字符串中,然后通过ASCII码得到每一位的相加结果num,然后把num一位一位的放到stack中,使用stack是因为它先进先出的特性,最后输出就行了. 代码: 1 #include<cstdio> 2 #include<qu

PAT甲级考前整理

终于在考前,刷完PAT甲级130道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种境界.PAT甲级题目总的说卡题目的比较多,卡测试点的比较少,有些题目还会有题意混淆,这点就不吐槽了吧.静下心来耍这130道题,其实磨练的是一种态度与手感,养成的是一种习惯.热爱AC没有错!! 130道题目主要的考点: 1.排序:快速排序,直接插入排序,希尔排序,分治排序,堆排序. 2.图论:拓扑排序.最短路径.深度搜索.广度搜索. 3.树:树的遍历.完全二叉树.AVL. 4.其他:并查集,模拟,哈希.背包.

PAT甲级考试题库1001 A+B Format 代码实现及相关知识学习

准备参加九年九月份的PAT甲级证书考试,对网站上的题目进行总结分析: 1001题 A+B Format (20 分) Calculate a+b and output the sum in standard format -- that is, the digits must be separated into groups of three by commas (unless there are less than four digits). 计算a+b的值并以一定格式输出其和sum(数字需要

PAT甲级专题|最短路

PAT甲级最短路 主要算法:dijkstra 求最短最长路.dfs图论搜索. 1018,dijkstra记录路径 + dfs搜索路径最值 25分,错误点暂时找不出.. 如果只用dijkstra没法做,只能得20分 #include<bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 510; int cmax,n,ter,m; int caps[maxn]; int g[maxn][m

【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全部出手的利润.输出最大利润. trick: 测试点2可能包含M不为整数的数据.(尽管题面说明M是正整数,可是根据从前PAT甲级题目的经验,有可能不是整数.....) 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using names

PAT甲级题分类汇编——理论

理论这一类,是让我觉得特别尴尬的题,纯粹是为了考数据结构而考数据结构.看那Author一栏清一色的某老师,就知道教数据结构的老师的思路就是和别人不一样. 题号 标题 分数 大意 Author 1051 Pop Sequence 25 判断一个序列是否是pop序列 CHEN, Yue 1052 Linked List Sorting 25 链表排序 CHEN, Yue 1057 Stack 30 一个有中位数功能的stack CHEN, Yue 1074 Reversing Linked List

PAT甲级第二次真题练习

1005 Spell It Right (20)(20 分)提问 Given a non-negative integer N, your task is to compute the sum of all the digits of N, and output every digit of the sum in English. Input Specification: Each input file contains one test case. Each case occupies one