The median of multi ascending array
Given 17 arrays,every array is ascending.The task is to get the median of all these numbers.
0 1 2 3 4 this is an array with 5 elements,the median is (5/2).
0 1 2 3 4 5 this is an array with 6 elements,the median is (6/2).
If we index an the elements from 0,the median is the element's count/2.
Input format
sort them:
36 elements(from a[0] to a[35]).The median is the a[18](the 19th)= 54.
The method above is O(n).
The method below is O(lgn).The keythought in the below method is alike with trisection.
#include<iostream>
#include<math.h>
using namespace std;
][];
];
int n;
int main(){
;
cin>>n;
;i<n;i++){
cin>>len[i];
l+=len[i];
;j<len[i];j++)cin>>a[i][j];
}
);
/*
If forward l-(l/2) steps,the minimum value is the ans.
*/
int ans;
while(rem){
int s=ceil(1.0*rem/n);
;
;
//At least,there is someone forward ceil(rem/n) steps.who should do this?
;i<n;i++){
if(len[i]){
,len[i]-s);
if(a[i][j]>mi){
mi=a[i][j];
ii=i;
}
}
}
,len[ii]-s);
rem-=(len[ii]-j);
len[ii]=j;
ans=min(ans,a[ii][j]);//I made a mistake here,should use 'min' to update ans.
}
cout<<ans<<endl;
;
}
The median of multi ascending array的更多相关文章
- ZOJ2481 Unique Ascending Array 2017-04-18 23:08 33人阅读 评论(0) 收藏
Unique Ascending Array Time Limit: 2 Seconds Memory Limit: 65536 KB Given an array of integers ...
- Median of Two Sorted Array leetcode java
题目: There are two sorted arrays A and B of size m and n respectively. Find the median of the two sor ...
- LeetCode 4 Median of Two Sorted Array
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...
- PHP中的Array
PHP中的数组是一个有序映射(1对1的关系 key->value).Array是一个综合体:可表示数组.字典.集合等. key可以是int或string.value可以是任意类型. key如下情 ...
- codeforces 166C Median - from lanshui_Yang
C. Median time limit per test 2 seconds memory limit per test 256 megabytes input standard input out ...
- Kotlin实现LeetCode算法题之Median of Two Sorted Arrays
题目Median of Two Sorted Arrays(难度Hard) 方案1,数组合并&排序调用Java方法 import java.util.* class Solution { fu ...
- [leetcode]4. Median of Two Sorted Arrays俩有序数组的中位数
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...
- 215. Kth Largest Element in an Array(QuickSort)
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- 九章lintcode作业题
1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr( ...
随机推荐
- Makefile关键字
@ makefile会把将要执行的命令行在命令执行前输出到屏幕上,使用@可以避免显示出命令本身 若@ echo 正在编译-则$make显示正在编译- 若echo 正在编译-则$make显示: echo ...
- hdu 4960 Another OCD Patient(dp)
Another OCD Patient Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Ot ...
- 理解 OpenStack + Ceph (7): Ceph 的基本操作和常见故障排除方法
本系列文章会深入研究 Ceph 以及 Ceph 和 OpenStack 的集成: (1)安装和部署 (2)Ceph RBD 接口和工具 (3)Ceph 物理和逻辑结构 (4)Ceph 的基础数据结构 ...
- 30 algorithm questions study
April 26, 2015 Spent over a few months to go over 30 questions about algorithm starting from January ...
- 地铁 Dijkstra(优先队列优化) 湖南省第12届省赛
传送门:地铁 思路:拆点,最短路:拆点比较复杂,所以对边进行最短路,spfa会tle,所以改用Dijkstra(优先队列优化) 模板 /******************************** ...
- POJ1014Dividing[多重背包可行性]
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 68769 Accepted: 17955 Descri ...
- AC日记——求10000以内n的阶乘 openjudge 1.6 14
14:求10000以内n的阶乘 总时间限制: 5000ms 内存限制: 655360kB 描述 求10000以内n的阶乘. 输入 只有一行输入,整数n(0<=n<=10000). 输出 ...
- javascript高级程序设计 读书笔记1
第二章 在HTML中使用JS 加载JS有三种:行内,head头部和外部链接JS 最好使用外部链接<script src="example.js" ></sc ...
- QuickFIX/N入门:(三)如何配置QuickFIX/N
Acceptor或者Initiator能够为您维护尽可能多的FIX会话,因而FIX会话标识的唯一性非常重要.在QuickFIX/N中,一个FIX会话的唯一标识是由:BeginString(FIX版本号 ...
- java 22 - 8 多线程之线程生命周期图解