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 遍历目录下面的所有文件及子文件夹

    //遍历文件夹下面所有文件及子文件夹 function read_all_dir ( $dir ){ $result = array(); $handle = opendir($dir);//读资源 ...

  2. ms13_055 metasploit

    111 def get_payload(t) 112 if t['Rop'] == :msvcrt 113 print_status("Using msvcrt ROP") 114 ...

  3. Find a Way (双bfs)

    Pass a year learning in Hangzhou, yifenfei arrival hometown Ningbo at finally. Leave Ningbo one year ...

  4. 运维01 VMware与Centos系统安装

    VMware与Centos系统安装   今日任务 1.Linux发行版的选择 2.vmware创建一个虚拟机(centos) 3.安装配置centos7 4.xshell配置连接虚拟机(centos) ...

  5. elemeng-ui中el-select的默认选择项问题

    直接绑定将option中的value值绑定给v-model <template> <div> <el-select v-model="query"&g ...

  6. vue开发请求本地模拟数据的配置方法(转)

    VUE开发请求本地数据的配置,早期的vue-lic下面有dev-server.js和dev-client.js两文件,请求本地数据在dev-server.js里配置,最新的vue-webpack-te ...

  7. 单机zookeeper部署伪集群

    1.zookeeper介绍 ZooKeeper 是一个为分布式应用所设计的分布的.开源的协调服务.分布式的应用可以建立在同步.配置管理.分组和命名等服务的更高级别的实现的基础之上. ZooKeeper ...

  8. 利用docker搭建WordPress

    步骤一 创建mysql的容器 步骤二 创建wordpress的容器并链接mysql容器的数据库 创建mysql的容器 docker run -d --name mysql -v mysql-data: ...

  9. 通过一条很慢的SQL梳理下SQL优化基础

  10. 用Emacs进行Java开发

    用Emacs进行Java开发 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} 用 ...