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×105) 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 分)的更多相关文章
- PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- 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 (25分) 有序数组合并与防坑指南
题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...
- 【PAT甲级】1029 Median (25 分)
题意: 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数. 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数.(重复一次) 输出两组数合并后的中位数.(200ms, ...
- 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】1029. Median (25)
Given an increasing sequence S of N integers, the median is the number at the middle position. For e ...
- A1029 Median (25 分)
一.技术总结 最开始的想法是直接用一个vector容器,装下所有的元素,然后再使用sort()函数排序一下,再取出中值,岂不完美可是失败了,不知道是容器问题还是什么问题,就是编译没有报错,最后总是感觉 ...
- 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 ...
- 1029. Median (25)
分析: 考察归并排序,用简单的快排会超时. #include <iostream> #include <stdio.h> #include <algorithm> ...
随机推荐
- 十二、SpringBoot 优雅的集成Spring Security
前言 至于什么是Spring security ,主要两个作用,用户认证和授权.即我们常说的,用户只有登录了才能进行其他操作,没有登录的话就重定向到登录界面.有的用户有权限执行某一操作,而有的用户不能 ...
- C++——运行时类型识别RTTI
1.实现方式 typeid运算符,返回表达式的类型 dynamic_cast运算符,基类的指针或引用安全地转换成派生类的指针或引用 2.适用于:使用基类的指针或引用执行派生类的操作,且该操作不是虚函数 ...
- HDU3450_Counting Sequences
题意: 让你从所给的序列中找到他的子序列,使他们相邻之间差距不超过d,问有多少个转移的子序列 这题第一眼大概就知道是状态转移,sum[i]表示以前i个中有多少个,那么sum[i+1]比sum[i] 多 ...
- oracle数据库 唯一约束的创建与删除
1.创建索引: alter table TVEHICLE add constraint CHECK_ONLY unique (CNUMBERPLATE, CVIN, CPLATETYPE, DWQCH ...
- Linux部署web项目
一.软件1.putty2.WinSCP 二.调试1.linux 下 apache启动.停止.重启命令基本的操作方法:本文假设你的apahce安装目录为/usr/local/apache2,这些方法适合 ...
- 36-python基础-python3-字典与列表的区别
(1)不像列表,字典中的表项是不排序的.名为 spam 的列表中,第一个表项是 spam[0].但字典中没有“第一个”表项. (2)确定两个列表是否相同时,表项的顺序必须相同,但在字典确定两个字典是否 ...
- window下eclipse搭建hadoop环境
1 生成插件jar 1.1 安装java,ant运行环境 1.2 下载hadoop-2.5.0.tar.gz并解压到指定目录 1.3 下载hadoop2x-eclipse-plugin-master. ...
- InnoDB B树 锁
InnoDB B树 叶子=>主键+数记录非叶子=>主键1+主键3...主键4 事务和行锁 索引项加锁 相等条件来访问更新数据,避免使用范围条件 (1)InnoDB的行销是基于索引实现的,如 ...
- 挂载时出现mount: RPC: Unable to receive; errno = Connection refused错误的解决方法
当我们在做NFS开发板下挂载时,经常会出现mount: RPC: Unable to receive; errno = Connection refused的错误,连接被拒绝了,到底是什么原因呢? 这 ...
- JSONObject.toJSONString(map)
Map<Integer, List<Integer>> map = new LinkedHashMap<>(); map.put(1,ddzCard.getOneS ...