PAT 甲级 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 (≤) 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
题意:
给出两个已排序序列,求这两个序列合并后的中间数
思路:
开一个数组,在线处理第二个数组。 第一二个数组(下标从1开始)分别有n,m个元素,中间数在(n + m + 1) / 2的位置。所以只要从小到大数到(n + m + 1) / 2的位置就行了~ count计总个数 ,给第一个数组设置指针i,每次从第二个数组中读入temp,检查第一个数组中前几个数是不是比temp小,小就count+1并判断是否到数了中间数,到了就输出。 如果数完比temp小的数还没到中间数,count+1,检查temp是不是中间数,是就输出。循环上述过程。如果第二个数组读取完了,还没数到中间数,说明中间数在剩下的第一个数组中,就在剩下的数组中数到中间数位置即可
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[];
int main()
{
int n,m;
cin>>n;
for(int i=;i<=n;i++)
{
cin>>a[i];
}
a[n + ] = 0x7fffffff;
cin>>m;
int mid=(n+m+)/;
int k=,count=;
for(int i=;i<=m;i++)
{
ll x;
cin>>x;
while(a[k]<x){
count++;
if(count==mid){
cout<<a[k];
}
k++;
}
count++;
if(count==mid){
cout<<x;
}
}
//如果第二个数组读取完了,还没数到中间数,
//说明中间数在剩下的第一个数组中,就在剩下的数组中数到中间数位置即可
while(count<=mid){
count++;
if(count==mid){
cout<<a[k];
}
k++;
}
return ;
}
PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*的更多相关文章
- 1029 Median (25 分)
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- PAT甲 1029. Median (25) 2016-09-09 23:11 27人阅读 评论(0) 收藏
1029. Median (25) 时间限制 1000 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given an incr ...
- PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)
1070 Mooncake (25 分) Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...
- PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)
1078 Hashing (25 分) The task of this problem is simple: insert a sequence of distinct positive int ...
- PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)
1032 Sharing (25 分) To store English words, one method is to use linked lists and store a word let ...
- 【PAT甲级】1029 Median (25 分)
题意: 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数. 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数.(重复一次) 输出两组数合并后的中位数.(200ms, ...
- PAT 1029 Median (25分) 有序数组合并与防坑指南
题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...
- 【PAT】1029. Median (25)
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- PAT 甲级 1029 Median
https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968 Given an increasing se ...
随机推荐
- java之数据结构与算法
1.了解基本数据结构及特点 如,有哪些二叉树,各有什么特点 树二叉搜索树 每个节点都包含一个值,每个节点至多有两棵子树,左孩子小于自己,右孩子大于自己,时间复杂度是O(log(n)),随着不断插入节点 ...
- Java基础 static限定符的使用 以及【 static实现的 singleton(单例)设计模式】
static实现的 singleton(单例)设计模式 /** static实现的 singleton设计模式 , 使得一个类只能够创建一个static对象 */ 模板设计结构: package Co ...
- mysql 5.7 版本 You must reset your password using ALTER USER statement before executing this statement报错处理
https://blog.csdn.net/muziljx/article/details/81541896 MySQL版本5.7.6版本开始的用户可以使用如下命令: mysql> ALTER ...
- JVM之Java运行时数据区(线程隔离区)
来源 JVM会在会在执行Java程序过程中把所管理的内存划分为若干区域,主要包括程序计数器(Program Counter Register),虚拟机栈(VM Stack),本地方法栈(Native ...
- 从客户端中检测到有潜在危险的 request.form值
这里只说ASP.NET MVC的解决方法,ASP.NET已经不碰了. 给控制器加上[ValidateInput(false)]特性即可忽略含有HTML标记的内容. [HttpPost] [Valida ...
- MySQL之InnoDB索引面试学习笔记
写在前面 想要做好后台开发,终究是绕不过索引这一关的.先问自己一个问题,InnoDB为什么选择B+树作为默认索引结构.本文主要参考MySQL索引背后的数据结构及算法原理和剖析Mysql的InnoDB索 ...
- 2019牛客多校D move——乱搞&&思维题
题意 给定 $n$ 个物品,体积分别为 $v_i$,现有 $K$ 的容积一样的箱子,按如下策略装入物品:每次选取尽可能大的装入(较大的不能装入时可以向小的找),依次装入箱子. 分析 首先,不具有严格的 ...
- WeakReference 弱引用
弱引用是使用WeakReference类创建的.因为对象可能在任意时刻被回收,所以在引用该对象前必须确认它存在. class MainEntryPoint { static void Main() { ...
- [2019HDU多校第四场][HDU 6617][D. Enveloping Convex]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6617 题目大意:给出一凸包\(P\),求最小的与\(P\)相似且对应边平行的多边形,使得题目给出的\( ...
- Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) A. Forgetting Things
链接: https://codeforces.com/contest/1247/problem/A 题意: Kolya is very absent-minded. Today his math te ...