【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. 那么中位数一定是 ...
随机推荐
- delphi7如何实现 科学计数的转换。 比如我输入2,触发之后会转换成2.000000E+00.求赐教
uses SysUtils; function StrToExp(s: string): string;var f: Extended;begin f := StrToFloat(s); Result ...
- 【HTTP】GET和POST的区别
[HTTP]GET和POST的区别 HTTP定义了与服务器交互的不同方法,最基本的方法有四种:GET.POST.PUT.DELETE. URL全程是资源描述符,用于描述网上的资源.以上四种方式就是 ...
- VC++2010配置使用MySQL5.6
0.前提 安装后的文件概览 编译器: VC++2010 MySQL版本:MySQL5.6.19 for win64 Connector版本:connector c++ 1.1.3 在VS2010 ...
- Cocos2d-android (05) 渐变动画(颜色,淡入淡出。。。)
淡入淡出.颜色渐变及动作重复执行 import org.cocos2d.actions.base.CCRepeatForever; import org.cocos2d.actions.interva ...
- eclipse手动导入dtd文件
1.在eclipse的工具栏上按照“Window->Preferences->XML->XML Catalog”依次点下去,然后在右侧选中 User Specified Entrie ...
- 我的格斗梦——张龙海(R.J)谈游戏动画师职业(转)
编者按:他是一个生在东北,祖藉却是韩国的年轻人.从小生性好动的他觉得上课 学习十分枯燥,所以高中没毕业便辍学在家.但他仍是一个喜欢动漫.游戏的年轻人,因为热爱所以他用父母给的钱开始了求学之路,在之后的 ...
- Building nginx from Sources(从源代码安装nginx)
Building nginx from Sources(从源代码安装nginx) The build is configured using the configure command. 安装用配置 ...
- 前端架构:Angular与requirejs集成实践
这几天angular与requirejs.browserify的集成弄的博主头好晕,今天终于成功集成了requirejs,现写些心得体会在这里. 核心思想:angular加载时有一定的顺序,必须依次加 ...
- DouNet学习_收发邮件
一.收发邮件 --->第一步:发邮件首先要有发送者的邮箱地址和登录的密码才能发送 这些都写在APP里 不要写死 --->第二步:发邮件就要有网络,要添加net.Mail命名空间 要发送的 ...
- js运动 缓冲运动
<!DOCTYPE HTML> <HTML> <meta http-equiv="Content-Type" content="text/h ...