二分法找中位数
来源:互联网 发布:微信公众平台seo 编辑:程序博客网 时间:2024/05/29 11:27
/*Name: 中位数median Author: 巧若拙 Description: 描述给定n(n为奇数且小于1000)个整数,整数的范围在0-m(0<m<2^31)之间,请使用二分法求这n个整数的中位数。所谓中位数,是指将这n个数排序之后,排在正中间的数。输入第一行是两个整数n和m。第二行是用空格隔开的n个整数 输出一个中位数 样例输入5 103 7 2 5 4 样例输出4*/#include<iostream> #include<cstdio> #include<cstdlib> using namespace std; const int MAXN = 1000; int A[MAXN]; int main() { int n, m; cin >> n >> m; for (int i=0; i<n; i++) cin >> A[i]; int mid, count, left = 0, right = m; while (left <= right) { mid = (left + right) / 2; count = 0; for (int i=0; i<n; i++) {if (A[i] >= mid)//记录不小于mid的整数数量count++; } if (count > n/2) left = mid + 1; else right = mid - 1; // cout << mid << " " << left << " " << right << " " << count << " " << n/2 << endl; } cout << right << endl; return 0; }
阅读全文
0 0
- 二分法找中位数
- POJ - 3579(二分法找中位数)
- 找中位数
- 找中位数
- Median of Two Sorted Arrays(找两个排序数组的中位数,二分法)
- 海量数据找中位数
- Sicily 4224. 找中位数
- 海量数据找中位数
- 双数组找中位数
- 大数据量找中位数
- 大根堆 小根堆 找中位数
- 海量数据找中位数
- 无序数组找中位数
- 海量数据找中位数
- 面试常见问题-找中位数
- 二分法差找
- 二分法找数
- 10G个数找中位数
- A标签实现文件下载以及INPUT隐藏边框样式
- Algorithm 头文件中的算法
- POJ
- Android抽象布局——include、merge 、ViewStub
- 批处理去除指定字符前所有的0
- 二分法找中位数
- Go游戏服务器开发的一些思考(十三):behavior3go的一些坑(备忘)
- L1-009. N个数求和
- 设计模式之迭代器模式
- Xilinx ZYNQ 7000+Vivado2015.2系列(二)之奇数分频和逻辑分析仪(ILA)的使用
- 如果一个网站后台能有机会接触到用户的明文密码,那就是流氓行为!
- PHP面试总结(3)-- Linux
- The content of element type "struts" must match "(package|include|bean|constant)*".
- 数据库那点事(Mysql)-完