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:

  1. 4 11 12 13 14
  2. 5 9 10 15 16 17

Sample Output:

  1. 13

题意:

给出两个已排序序列,求这两个序列合并后的中间数

思路:

开一个数组,在线处理第二个数组。 第一二个数组(下标从1开始)分别有n,m个元素,中间数在(n + m + 1) / 2的位置。所以只要从小到大数到(n + m + 1) / 2的位置就行了~ count计总个数 ,给第一个数组设置指针i,每次从第二个数组中读入temp,检查第一个数组中前几个数是不是比temp小,小就count+1并判断是否到数了中间数,到了就输出。 如果数完比temp小的数还没到中间数,count+1,检查temp是不是中间数,是就输出。循环上述过程。如果第二个数组读取完了,还没数到中间数,说明中间数在剩下的第一个数组中,就在剩下的数组中数到中间数位置即可

AC代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. int a[];
  5. int main()
  6. {
  7. int n,m;
  8. cin>>n;
  9. for(int i=;i<=n;i++)
  10. {
  11. cin>>a[i];
  12. }
  13. a[n + ] = 0x7fffffff;
  14. cin>>m;
  15. int mid=(n+m+)/;
  16. int k=,count=;
  17. for(int i=;i<=m;i++)
  18. {
  19. ll x;
  20. cin>>x;
  21. while(a[k]<x){
  22. count++;
  23. if(count==mid){
  24. cout<<a[k];
  25. }
  26. k++;
  27. }
  28. count++;
  29. if(count==mid){
  30. cout<<x;
  31. }
  32. }
  33. //如果第二个数组读取完了,还没数到中间数,
  34. //说明中间数在剩下的第一个数组中,就在剩下的数组中数到中间数位置即可
  35. while(count<=mid){
  36. count++;
  37. if(count==mid){
  38. cout<<a[k];
  39. }
  40. k++;
  41. }
  42. return ;
  43. }

PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*的更多相关文章

  1. 1029 Median (25 分)

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

  2. 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 ...

  3. PAT 甲级 1070 Mooncake (25 分)(结构体排序,贪心,简单)

    1070 Mooncake (25 分)   Mooncake is a Chinese bakery product traditionally eaten during the Mid-Autum ...

  4. PAT 甲级 1078 Hashing (25 分)(简单,平方二次探测)

    1078 Hashing (25 分)   The task of this problem is simple: insert a sequence of distinct positive int ...

  5. PAT 甲级 1032 Sharing (25 分)(结构体模拟链表,结构体的赋值是深拷贝)

    1032 Sharing (25 分)   To store English words, one method is to use linked lists and store a word let ...

  6. 【PAT甲级】1029 Median (25 分)

    题意: 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数. 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数.(重复一次) 输出两组数合并后的中位数.(200ms, ...

  7. PAT 1029 Median (25分) 有序数组合并与防坑指南

    题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...

  8. 【PAT】1029. Median (25)

    Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...

  9. PAT 甲级 1029 Median

    https://pintia.cn/problem-sets/994805342720868352/problems/994805466364755968 Given an increasing se ...

随机推荐

  1. Bert-util安装

    转载:https://blog.csdn.net/u013109501/article/details/91987180 https://blog.csdn.net/Vancl_Wang/articl ...

  2. BCB6 使用TZCompressionStream压缩

          最近由于项目需要涉及到解压第三方公司的数据,在此做一下记录环境部署和使用方法,免得以后忘记.    对方公司的数据是通过TCompressionStream 压缩之后,存到数据库中,采用的 ...

  3. 使用raw input 代替全局键盘钩子

    //关于raw input 请查看msdn https://msdn.microsoft.com/en-us/library/windows/desktop/ms645536%28v=vs.85%29 ...

  4. 【万能的DFS和BFS基础框架】-多刷题才是硬道理!

  5. pyqt pyside qcombobox disable wheel scrolling

    pyqt pyside qcombobox disable wheel scrolling import sys from PyQt5 import QtCore, QtWidgets import ...

  6. adb连接MUMU模拟器

    参考:http://mumu.163.com/2017/12/19/25241_730476.html?type=notice 通过adb就可以像操作linux一样来看看模拟器的文件什么的,难道刷机就 ...

  7. Python+request 分模块存放接口,多接口共用参数URL、headers的抽离,添加日志打印等《三》

    主要介绍内容如下: 1.分模块存放接口 2.多接口共用参数URL.headers的抽离为配置文件 3.添加日志打印 4.一个py文件运行所有所测的接口 如上介绍内容的作用: 1.分模块存放接口:方便多 ...

  8. windows builder里面的可伸缩面板

    使用 org.eclipse.wb.core.controls.flyout.FlyoutControlComposite.FlyoutControlComposite类 构造方法中传入的prefer ...

  9. 【JUC系列第三篇】-CAS算法详解

    作者 : 毕来生 微信: 878799579 1.CAS是什么? CAS是英文单词(Compare-And-Swap)的缩写,中文意思是:比较并替换.CAS需要有3个操作数:内存地址V,旧的预期值A, ...

  10. Spring Aop实例@Aspect、@Before、@AfterReturning@Around 注解方式配置(转)

    用过spring框架进行开发的人,多多少少会使用过它的AOP功能,都知道有@Before.@Around和@After等advice.最近,为了实现项目中的输出日志和权限控制这两个需求,我也使用到了A ...