Median (25)

时间限制 1000 ms 内存限制 65536 KB 代码长度限制 100 KB 判断程序 Standard (来自 小小)
题目描述
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.

输入描述:
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 (<=1000000) 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.

输出描述:
For each test case you should output the median of the two given sequences in a line.

输入例子:
4 11 12 13 14
5 9 10 15 16 17

输出例子:
13

题解:求解中位数,用二指针的思想,设i,j 扫一遍,便可得到。中间位置为(n+m-1)/2

#include <bits/stdc++.h>
#include <stdlib.h>
using namespace std;
#define vi vector<int>
#define pii pair<int,int>
#define x first
#define y second
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define SZ(x) x.size()
#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=b-1;i>=a;i--)
#define pi acos(-1)
#define mod 1000000007
#define inf 1000000007
#define ll long long
#define DBG(x) cerr<<(#x)<<"="<<x<<"\n";
#define N 200010
template <class U,class T> void Max(U &x, T y){if(x<y)x=y;}
template <class U,class T> void Min(U &x, T y){if(x>y)x=y;}
template <class T> void add(int &a,T b){a=(a+b)%mod;}
const int maxn=1000010;
const int INF=0x7ffffff;
int main()
{int n,m,a[maxn],b[maxn],ans;cin>>n;rep(i,0,n) cin>>a[i];cin>>m;rep(i,0,n) cin>>b[i];//a[n]=b[m]=inf;ans=(n+m-1)/2;int i=0,j=0,k=0;while(k<ans){k++;if(a[i]>b[j]){j++;}else{i++;}}if(a[i]<b[j])cout<<a[i]<<endl;else cout<<b[j]<<endl;}

Median (25)相关推荐

  1. 1029.Median (25)

    1029.Median (25) pat-al-1029 2017-02-23 利用哨兵 数组太大不能放main里 /*** pat-al-1029* 2017-02-23* C version* A ...

  2. 【19行代码AC,简洁】1029 Median (25 分)

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given an increasing sequence S of N integers, the median is the n ...

  3. r语言 bsda包_使用R语言creditmodel包进行Vintage分析或留存率分析

    1 什么是vintage分析? Vintage分析(账龄分析法)被广泛应用于信用卡及信贷行业,这个概念起源于葡萄酒,即不同年份出产的葡萄酒的品质有差异,那么不同时期开户或者放款的资产质量也有差异,其核 ...

  4. PAT甲级题目翻译+答案 AcWing(基础算法与数据结构)

    1029 Median (25 分) 题意 : median中位数 给两个升序的序列,要求它们两个合并后的中位数 思路 : 双指针 语法 : long int #include <iostrea ...

  5. 【最新合集】PAT甲级最优题解(题解+解析+代码)

    以下每道题均是笔者多方对比后, 思考整理得到的最优代码,欢迎交流! 共同成长哇.可以和博主比拼一下谁刷的更快~ 欢迎收藏.欢迎来玩儿 PAT题解目录 题号 标题 题解 分类 使用算法 1001 A+B ...

  6. 【OBS】vs2019 + QT5.15.2 : obs-studio-27.2.4 configure和vs工程生成

    要自己来的工作还挺多. 下载的官方 发布版本, 但是https://github.91chi.fun/https://github.com/obsproject/libdshowcapture.git ...

  7. 【PAT甲级】A1001-A1050刷题记录

    文章目录 A1001 A+B Format (20 分) 0.25 ★(一元多项式加法) A1002 A+B for Polynomials (25 分) 0.21 (单源最短路Dijkstra+边权 ...

  8. 刷PAT甲级的各题思路、细节以及遇到的问题记录

    1001 A+B Format (20分) 因为一定会用到字符串,而string非常好用,但是用的时候一定要注意不能越界访问,否则会在运行时出现abort() has been called. 100 ...

  9. PAT题型分类 记录汇总

    这篇博客记录了我在跟着<算法笔记>以及习题册<算法笔记 上机实践指南>刷了PAT的题目之后的一些解题方法的总结与心得. 第三章 入门模拟 1. 简单模拟 一般解题思路 这一小节 ...

最新文章

  1. Linux基础知识——常用shell命令介绍(一)
  2. AgileEAS.NET SOA 中间件2013第四季度发布部分功能开源预告
  3. 数据库系统概论:第四章 数据库安全性
  4. php读写分离数据不能同步,thinkphp 下数据库读写分离代码剖析
  5. 【Linux】一步一步学Linux——man命令(有问题找男人)(13)
  6. ReactJS入门之生命周期
  7. 关于全局缓存的一种简单实现方法
  8. Failure to find com.oracle:ojdbc6:jar:11.2.0.1.0
  9. CLIP再创辉煌!西南交大MSRA提出CLIP4Clip,进行端到端的视频文本检索!
  10. 机智云获取树莓派传来的数据_哪些数据对云来说太冒险了?
  11. ice(Internet Communications Engine) window 安装与配置
  12. python aiml_Python AIML搭建聊天机器人实例
  13. Adobe Photoshop Pro CC 2019及类似软件注册
  14. uni-app开发环境配置
  15. 【淘宝装修】PS DW 介绍 教程 代码(终极篇)
  16. linux mbr efi 分区吗,几个概念:MBR,GPT,EFI分区,混合分区表,BootCamp
  17. 启动Maven项目 死活报404 配置文件都没问题
  18. Java常用类--java.lang.StringBuilder
  19. 计算机显示屏显示超出屏幕大小,如果计算机提示显示器显示超出范围,该怎么办?...
  20. 知识管理文档协同不一定要用语雀和石墨,用它效果更好

热门文章

  1. Scrum在大型游戏团队中的应用
  2. 复旦大学硕士盲审 计算机学院,上海市硕士论文盲审 复旦大学论文抽检、盲审工作的通知...
  3. win10环境下c语言打开文件失败,cfile fopen fopen_s win10下打开文件失败
  4. 【HTTP协议其实很简单】03.自己写一个微型静态Web服务器
  5. 计算机进制算法在线,二进制转十进制和十六进制在线计算器
  6. html如何添加时钟效果,HTML5实现时钟效果
  7. 卸载landesk的方法
  8. 微信开发之服务号设置
  9. Excel 2003文档的密码忘了怎么办
  10. java实验1_《Java程序设计》实验1