1029. Median (25)
来源:互联网 发布:网络执法官绿色破解版 编辑:程序博客网 时间:2024/06/02 01:16
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 Input4 11 12 13 145 9 10 15 16 17Sample Output
13
提交代码
题意:给定两个有序数组,求合并后的中位数
抱着试一试的心态,排了个序,居然过了.
#include <cstdio>#include <algorithm>using namespace std;int ary[2000010];int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%d", ary + i); int m; scanf("%d", &m); for (int i = n; i < n + m; ++i) scanf("%d", ary + i); sort(ary, ary + n + m); printf("%d\n", ary[(n + m - 1) / 2]); return 0;}
#include <cstdio>#include <queue>using namespace std;int main() { queue<int> q1, q2; int n, m, tmp; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &tmp); q1.push(tmp); } scanf("%d", &m); for (int i = 0; i < m; ++i) { scanf("%d", &tmp); q2.push(tmp); } int x; for (int cnt = 0; cnt <= (n + m - 1) / 2; ++cnt) { if ((!q1.empty() && !q2.empty() && q1.front() < q2.front()) || (q2.empty())) { x = q1.front(); q1.pop(); } else { x = q2.front(); q2.pop(); } } printf("%d\n", x); 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)
- Android开源特效框架
- ecshop增加点击购买直接进入购物车
- mysql innodb ibdata 数据文件误删除恢复过程
- UIWindow
- ViewGroup和View的事件分发机制
- 1029. Median (25)
- Git之如何解决Error:pathspec '/layout/radar_chart.xml' did not match any file(s) known to
- Nginx和lvs在负载均衡方面的对比
- swift按钮通过代码添加点击事件时出现unrecognized selector sent to instance 0x136ea5010错误
- C语言开发总结(十七)
- Spring 配置文件中Bean 属性id和name的区别
- 10款最好用的MySQL数据库客户端图形界面管理工具
- Page Object Model (Selenium, Python)
- angular 小试