首页 > 其他分享 >PAT Advanced 1029 Median(25)

PAT Advanced 1029 Median(25)

时间:2022-08-28 22:57:03浏览次数:63  
标签:integers PAT sequence int 1029 Median median sequences n1

题目描述:

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

算法描述:归并排序

题目大意:

给出两个序列,求两序列的中间值

#include<iostream>
#include<algorithm>
using namespace std;

int n1, n2, a[1000010];

int main()
{
    cin >> n1;
    for(int i = 0 ; i < n1 ; i ++)  scanf("%d", &a[i]);
    cin >> n2;
    for(int i = 0 ; i < n2 ; i ++)  scanf("%d", &a[n1 + i]);
    sort(a, a + n1 + n2);
    cout << a[(n1 + n2 - 1) / 2];
}

标签:integers,PAT,sequence,int,1029,Median,median,sequences,n1
From: https://www.cnblogs.com/yztozju/p/16634349.html

相关文章

  • Navicat_Keygen_Patch_v5.6
    navicatdownload: Navicat :https://download.navicat.com.cn/download/navicat150_premium_cs_x64.exekeygendownload:  Navicat_Keygen_Patch_v5.6  无法下载-......
  • Canvas的context.beginPath()很重要
    在开发一个小画板,用到canvas,做了一个油漆桶工具,每次填色,都会填到几个不同的图案里,而我想要的只是一个一个的填色,点哪个给哪个填色,找了半天,原因原来是画图时没......
  • NC50940 Running Median
    题目原题地址:RunningMedian题目编号:NC50940题目类型:对顶堆时间限制:C/C++5秒,其他语言10秒空间限制:C/C++65536K,其他语言131072K1.题目大意多组数据,每组有标号、......
  • [React] Compound Pattern
    Source:https://javascriptpatterns.vercel.app/patterns/react-patterns/compound-pattern Acompoundcompoenntusagelookslikethis:importReactfrom"react......
  • 问题记录_IDEA启动报错:Failed to create JVM. JVM Path
    问题记录_IDEA启动报错:FailedtocreateJVM.JVMPath  起因下午写代码的时候感觉IDEA有点卡,不应该啊,我16G咋回卡呢,分配的内存也不小,于是又去加大内存分配,结果IDE......
  • [Javascript] Prototype Pattern
    Source:https://javascriptpatterns.vercel.app/patterns/design-patterns/prototype-pattern Ifyouusefactorypatterntocreateobject:constcreateDog=(nam......
  • [Javascript] Factory pattern vs Class instance
    InJavaScript,thefactorypatternisn'tmuchmorethanafunctionthatreturnsanobjectwithoutusingthe new keyword. ES6arrowfunctions allowustocr......
  • [Javascript] Singleton Pattern
    Source:https://javascriptpatterns.vercel.app/patterns/design-patterns/singleton-patternWiththeSingletonPattern,werestricttheinstantiationofcertainc......
  • @DataJpaTest 进行测试的坑
    @DataJpaTest 这个注解主要用来在Spring项目中测试JPA数据源。默认情况下,带有 @DataJpaTest 注解的测试使用嵌入式内存数据库。因此 @DataJpaTest 这个注解还是......
  • webpack中path.join()和path.resolve()区别
    constpath=require('path')path模块提供了用于处理文件和目录的路径的实用工具,使用时引入即可。 1.__dirname和__filename的区别__dirname,是一个成员,用来动态获取......