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

注意点:1.要考虑到m个数字已经输入完但是k还没到count的情况
    2.必须用cin.tie(0),否则超时。
#include<bits/stdc++.h>
using namespace std; const int maxn = ; int a[maxn]; int n,m; int main(){ ios::sync_with_stdio(false);
cin.tie(); cin>>n; int i,j; for(i=;i<n;i++)
cin>>a[i]; cin>>m; int count=; bool flag=false; count=(m+n+)/; int k=; int b; int ans; i=; for(j=;j<m&&k<count;j++){
cin>>b; if(a[i]<=b){
while(i<n&&a[i]<b){
k++;
if(k==count)
{
ans=a[i];
break;
} i++;
} } k++;
if(k==count)
{
ans=b;
break;
} } while(k<count&&i<n){
ans=a[i];
i++;
k++;
} // cout<<count<<endl;
cout<<ans<<endl; }
 

1029 Median (25 分)的更多相关文章

  1. PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

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

  2. 1029 Median (25分)

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

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

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

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

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

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

  6. 【PAT】1029. Median (25)

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

  7. A1029 Median (25 分)

    一.技术总结 最开始的想法是直接用一个vector容器,装下所有的元素,然后再使用sort()函数排序一下,再取出中值,岂不完美可是失败了,不知道是容器问题还是什么问题,就是编译没有报错,最后总是感觉 ...

  8. PAT Advanced 1029 Median (25) [two pointers]

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

  9. 1029. Median (25)

    分析: 考察归并排序,用简单的快排会超时. #include <iostream> #include <stdio.h> #include <algorithm> ...

随机推荐

  1. PHP生成PDF完美支持中文,解决TCPDF乱码

    PHP生成PDF完美支持中文,解决TCPDF乱码 2011-09-26 09:04 418人阅读 评论(0) 收藏 举报 phpfontsheaderttfxhtml文档 PHP生成PDF完美支持中文 ...

  2. python re模块常用的正则表达式

    '.'     默认匹配除\n之外的任意一个字符,若指定flag DOTALL,则匹配任意字符,包括换行 '^'     匹配字符开头,若指定flags MULTILINE,这种也可以匹配上(r&qu ...

  3. 个人笔记 - MATLAB

    1.教程 2.基本知识 2.1 帮助文档设置成中文:链接1 2.2 多行注释: 链接1 2.3 MATLAB基本数据类型: 链接1  链接2 2.4 matlab中的 ndims(a).length( ...

  4. selenium 3 下载 + Java使用

    一.下载Selenium 3 的相关包和工具 Selenium 3 下载的官网地址为http://www.seleniumhq.org/download/.当然,需要翻墙才能登陆selenium的官网 ...

  5. Apache 2.4.12 64位+Tomcat-8.0.32-windows-x64负载集群方案

    上次搞了Apache 2.2的集群方案,但是现在自己的机器和客户的服务器一般都是64位的,而且tomcat已经到8了.重新做Apache 2.4.12 64位+Tomcat-8.0.32-window ...

  6. Kotlin 和 Flutter 对于开发者究竟意味着什么?

    更多阿里P7架构进阶学习视频:阿里P7Android架构进阶学习视频回放近些年来,编程语言流行度的变化其实不大,在 TIOBE 编程语言排行榜上,Java.C.C++ 固若金山,也就只有 Python ...

  7. python 3和python 2 的不同之 f - strings

    python3.6版本及以上版本才能使用 f "{}{}{}" f-string 格式化输出

  8. android html布局界面

  9. LeetCode Arrary Easy 35. Search Insert Position 题解

    Description Given a sorted array and a target value, return the index if the target is found. If not ...

  10. Keepalived高可用服务

    Keepalived高可用服务 避免负载均衡服务出现单点问题 高可用服务原理 Keepalived的工作原理: Keepalived高可用对之间是通过VRRP通信的,因此,我从 VRRP开始了解起: ...