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 分)(思维题,找两个队列的中位数,没想到)*的更多相关文章

  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. Navicate 12 for mysql

    先下载安装好 navicat工具,在下载下面的工具 链接:https://pan.baidu.com/s/1Y-IOrbnhvhlS6Y3lpABLQg密码: nktc 选其中的一个 请根据自己安装N ...

  2. Week08_day01 (Hive实现按照指定格式输出每七天的消费平均数)

    Hive实现按照指定格式输出每七天的消费平均数 数据准备 2018/6/1,10 2018/6/2,11 2018/6/3,11 2018/6/4,12 2018/6/5,14 2018/6/6,15 ...

  3. 性能一 Exploring Mobile vs. Desktop OpenGL Performance

    opengl insight Exploring Mobile vs. DesktopOpenGL Performance Jon McCaffrey 前面那些内容以前看过 应该写在谋篇帖子里了  F ...

  4. 设置 IntelliJ IDEA 主题

    一.选择File选项====>Settings====>Appearance====>Theme====>三选一切换主题

  5. Warning: (1260, 'Row xxx was cut by GROUP_CONCAT()')

    MySql数据库查询时,使用group_concat报错“Row XXX was cut by GROUP_CONCAT()”,查了下是因为group_concat有个最大长度的限制,超过最大长度就会 ...

  6. mysql查看当前所有数据库中的表大小和元信息information_schema

    查看所有mysql数据库表和索引大小 mysql查看当前所有的数据库和索引大小 ,),' mb') as data_size, concat(,),'mb') as index_size from i ...

  7. DisplayModeProvider完成移动开发自动视图解析

    MVC中新建视图命名:XXX.cshtml.XXX.mobile.cshtml:用手机访问会自动到xxx.mobile.cshtml 一.原理 MVC中是通过DisplayModeProvider实现 ...

  8. Maven+Docker 部署

    Maven+Docker 部署 安装jdk8镜像 docker pull openjdk:8-jdk-alpine maven插件推送方式 修改/etc/docker/daemon.json文件,加入 ...

  9. 001_FreeRTOS中断配置

    (一)优先级看:022_STM32中断优先级分组解析 (二)FreeRTOS的中断配置中没有处理亚优先级(响应优先级)的情况,所以只能配置成组4,16个都为抢占优先级 (三)此宏用来设置 MCU 使用 ...

  10. sql 存储过程例子和学习demo

    -------------------------------------------------------------------------- ------------------------- ...