【PAT】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
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.
Output
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
分析:寻找中位数。一开始用sort来进行排序后找出中位数,但是这样会有两组数据超时。所以只能进行逐一比较。
代码:
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
int n;
long t;
int i; scanf("%d",&n);
vector<long> vec1(n);
for(i=0; i<n; i++)
scanf("%ld",&vec1[i]); scanf("%d",&n);
vector<long> vec2(n);
for(i=0; i<n; i++)
scanf("%ld",&vec2[i]); int j,temp = 0;
long v;
vector<long> result; for(i=0,j=0; i<vec1.size() && j<vec2.size(); )
{
if(vec1[i] <= vec2[j]){
v = vec1[i];
i++;
}
else{
v = vec2[j];
j++;
}
result.push_back(v);
if(result.size() == ( vec1.size() + vec2.size() + 1)/2 ){
cout<<result[result.size() - 1]<<endl;
break;
}
} if(result.size() != ( vec1.size() + vec2.size() + 1)/2)
{
while(i<vec1.size())
{
result.push_back(vec1[i]);
if(result.size() == ( vec1.size() + vec2.size() + 1)/2 ){
cout<<result[result.size() - 1]<<endl;
break;
}
i++;
}
while(j<vec2.size())
{
result.push_back(vec2[j]);
if(result.size() == ( vec1.size() + vec2.size() + 1)/2 ){
cout<<result[result.size() - 1]<<endl;
break;
}
j++;
}
}
return 0;
}
【PAT】1029. Median (25)的更多相关文章
- 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 分)(思维题,找两个队列的中位数,没想到)*
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- 【PAT甲级】1029 Median (25 分)
题意: 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数. 输入一个正整数N(<=2e5),接着输入N个非递减序的长整数.(重复一次) 输出两组数合并后的中位数.(200ms, ...
- 【PAT】1032 Sharing (25)(25 分)
1032 Sharing (25)(25 分) To store English words, one method is to use linked lists and store a word l ...
- 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 分)
1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the m ...
- PAT 1029 Median (25分) 有序数组合并与防坑指南
题目 Given an increasing sequence S of N integers, the median is the number at the middle position. Fo ...
- 【PAT】B1075 链表元素分类(25 分)
这道题算有点难,心目中理想的难度. 不能前怕狼后怕虎,一会担心超时,一会又担心内存过大,直接撸 将三部分分别保存到vector 有意思的在于输出 分别输出第一个的add和num 中间输出nextadd ...
- 【LeetCode】4. Median of Two Sorted Arrays(思维)
[题意] 给两个有序数组,寻找两个数组组成后的中位数,要求时间复杂度为O(log(n+m)). [题解] 感觉这道题想法非常妙!! 假定原数组为a,b,数组长度为lena,lenb. 那么中位数一定是 ...
随机推荐
- ajax轮询
oa.comet = function (id) { if (oa.id == 0) oa.id = id; $.ajax({ url: '/comet.asy?id=' + ...
- Yii笔记---redirect重定向
Yii的redirect方法在CControler与CHttpRequest之中都有被定义,CController中的redirect调用了CHttpRequest中的redirect方法.我们平常调 ...
- C#开源框架(整理)
http://www.cnblogs.com/chinanetwind/p/3715809.html http://www.cnblogs.com/chinanetwind/p/3715813.htm ...
- Intent相关
Intent是什么? 翻译为:意图,目的(名词) 其实根本没必要管它是什么,看看它能做什么就好了. 不过后来我知道了,它就是个机制----通信机制-----android的许多组件间的交流要依赖它. ...
- 本人为项目组制定的一份页面优化指南(easyui页面优化方案)
#本人为项目组制定的一份页面优化指南(easyui页面优化方案) ##背景 这是一篇我之前为项目组制定的页面优化指南,主要是面向表单页面,典型的像[注册用户](https://passport.cnb ...
- phonegap,Cordova 使用html5标签
某些安卓手机的webview使用location.href="tel:123456"不能调到打电话的界面,可以用下面的解决办法: config.xml文件最后加上一行: <a ...
- 第二百一十八天 how can I 坚持
真的是自以为是吗?或许是我想太多. 今天下雪了,2015年入冬以来的第一场雪,好冷. 又是一周. 睡觉吧,明天老贾生日. 没啥了,中午有点肚子疼,冬天了要注意.
- jxse2.6在jdk8下,JxtaMulticastSocket存在的问题
JxtaMulticastSocket覆写了java.net.MulticastSocket的bind方法: @Override public void bind(SocketAddress addr ...
- Linux下的grep搜索命令详解(一)
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
- Working with Other Node Types
[Working with Other Node Types] [Shape Nodes Draw Path-Based Shapes] The SKShapeNode class draws a s ...