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( ...
随机推荐
- 修改/etc/profile文件
通常情况下,/etc/profile文件是只读的,直接用vi或gedit打开修改后是无法保存的.要修改profile,需要取得root权限,(使用gedit编辑)应该如下: $sudo gedit / ...
- oracle函数--trunc
作用:截取 语法:trunc(date,[fmt]) TRUNC函数,ORA-01898 精度说明符过多 TRUNC(SYSDATE)即可默认当前日期(年月日),---写到这一步就好了 TRUNC ...
- html5移动端Meta设置
1. 强制让文档的宽度与设备的宽度保持1:1,并且文档最大的宽度比例是1.0,且不允许用户点击屏幕放大浏览. <meta name="viewport" content= ...
- Windows环境下Android Studio v1.0安装教程
Windows环境下Android Studio v1.0安装教程 准备工具 JDK安装包. 要求:JDK 7以及以上版本. Android Studio安装文件. Windows: exe(包含SD ...
- Use getopt() & getopt_long() to Parse Arguments
Today I came across a function [getopt] by accident. It is very useful to parse command-line argumen ...
- DPM检测模型 训练自己的数据集 读取接口修改
(转载请注明作者和出处 楼燚(yì)航的blog :http://www.cnblogs.com/louyihang-loves-baiyan/ 未经允许请勿用于商业用途) 本文主要是针对上一篇基于D ...
- 使用ZeroNet搭建P2P全球网站
软件 ZeroNet是一个利用比特币加密和BT技术提供不受审查的网络与通信的BT平台,ZeroNet网络功能已经得到完整的种子的支持和加密连接,保证用户通信和文件共享的安全.使用ZeroNet,你可以 ...
- 第四章 Hibernate入门
1.构建了一个Student实体类 public class Student { private Integer id; //name private String name; //age priva ...
- C#复习(学生信息输入)
在控制台程序中使用结构体.集合,完成下列要求项目要求:一.连续输入5个学生的信息,每个学生都有以下4个内容:1.序号 - 根据输入的顺序自动生成,不需要手动填写,如输入第一个学生的序号是1,第二个是2 ...
- Oracle Delete inner的方式,级联删除子表的数据方式。
例子1: from table2 b where a.id=b.id) 例子2: rebatepolicy表是主表,rebatepolicyitems是从表,从表有主表的主键,现在对于主表一些条件的数 ...