【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. 那么中位数一定是 ...
随机推荐
- Delphi 提示在Delphi的IDE中,按Ctrl+Shift+G键可以为一个接口生成一个新的GUID。
对于Object Pascal语言来说,最近一段时间最有意义的改进就是从Delphi3开始支持接口(interface),接口定义了能够与一个对象进行交互操作的一组过程和函数.对一个接口进行定义包含两 ...
- hdu 5253 最小生成树
赤裸裸最小生成树,没啥说的,我用kruskal过的 /* * Author : ben */ #include <cstdio> #include <cstdlib> #inc ...
- RSA加密(跨平台通用的)
/// <summary> /// RSA加密 /// </summary> /// <param name="strPublickey">&l ...
- Python 统计文本中单词的个数
1.读文件,通过正则匹配 def statisticWord(): line_number = 0 words_dict = {} with open (r'D:\test\test.txt',enc ...
- ASP.NET MVC 常用内置验证特性 简介
1.[Required] : 必须输入 [Required(ErrorMessage = "请输入用户名")] 2.[StringLength] : 限制字符串长度 [String ...
- bzoj3714: [PA2014]Kuglarz
[PA2014]KuglarzTime Limit: 20 Sec Memory Limit: 128 MBSubmit: 553 Solved: 317[Submit][Status][Discus ...
- Orion Network Performance Monitor 软件在网络管理中的应用
Orion Network Performance Monitor 软件在网络管理中的应用 Orion Network Performance Monitor是完全的带宽性能和故障管理软件 ...
- linux du命令: 显示文件、目录大小
介绍:du命令用于显示指定文件(夹)在磁盘中所占的空间信息.假如指定的文件参数实际上是一个目录,就要计算该目录下的所有文件.假如 没有提供文件参数,执行du命令,显示当前目录内的文件占用空间信息. 语 ...
- HD1000A + B Problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s) ...
- [原创]Devexpress XtraReports 系列 4 创建多栏报表
昨天我们完成了 [原创]Devexpress XtraReports 系列 3 创建主从报表 今天我们继续学习新的一种报表模式:多栏报表.(Demo源码,数据库最后附上) 或许很多人会问什么是多栏报表 ...