1029. Median (25)
来源:互联网 发布:重庆时尚频道直播软件 编辑:程序博客网 时间:2024/06/06 05:31
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 Input4 11 12 13 145 9 10 15 16 17Sample Output
13
给定的两个序列是有序的,直接用典型的归并套路
#include <iostream>#include <vector>#include <iomanip>using namespace std;int main(){ios::sync_with_stdio(false);long long n;cin>>n;vector<long long> vec1(n);for(int i=0;i<n;i++)cin>>vec1[i];cin>>n;vector<long long> vec2(n);for(int i=0;i<n;i++)cin>>vec2[i];vector<long long> vec;int i=0,j=0;while(i<vec1.size()&&j<vec2.size()){if(vec1[i] < vec2[j])vec.push_back(vec1[i++]);if(vec1[i] >= vec2[j])vec.push_back(vec2[j++]);}while(i<vec1.size())vec.push_back(vec1[i++]);while(j<vec2.size())vec.push_back(vec2[j++]);cout<<vec[(vec.size()-1)/2]<<endl;system("pause");return 0;}
- 1029. Median (25)
- 1029. Median (25)-PAT
- 【PAT】1029. Median (25)
- 1029. Median (25)
- 1029. Median (25)
- PAT 1029. Median (25)
- 1029. Median (25)
- PAT 1029. Median (25)
- PAT 1029. Median (25)
- 1029. Median (25)
- 1029. Median (25)
- 1029. Median (25)
- 1029. Median (25)
- 1029. Median (25)
- 1029. Median (25)
- 1029. Median (25)
- pat 1029. Median (25)
- 1029. Median (25)
- Test
- 第1章 python 打印输出实战
- socket编程 -- 网络字节序、IP地址转换、sockaddr数据结构
- ubuntu14.04 ruby on rails
- 1046. Shortest Distance (20)
- 1029. Median (25)
- POJ 2642 The Brick Stops Here(01背包问题)
- 安卓错误集锦
- poj1654 Area
- sql server 如何批量update数据
- poj 2709 贪心暴搜
- nefuoj-1036:2月29日
- POJ 2643 Election(map)
- hdu 3047 Zjnu Stadium 带权并查集