题目链接:Median

做了整整一天T_T

尝试了各种方法:

首先看了解答,可以用multiset,但是发现java不支持;

然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大堆存放前半部分较小的元素,最小堆存放后半部分较大的元素,并且最大堆的所有元素小于最小堆的所有元素;保持最大堆最多比最小堆多一个元素。每次插入元素的时候都先插入到最大堆,如果发现最大堆比最小堆多了两个个,那么就从最大堆里面拿出最大的放到最小堆里面;如果发现最大堆里面新插入的元素破坏了最大堆所有元素小于最小堆所有元素的原则,就把最大堆的最大元素和最小堆的最小元素交换。

这种做法可以用java的priority queue实现,插入操作的时间复杂度也只有O(log(n));但是堆结构非常不利于查找操作,所有删除的时候就很麻烦了,时间复杂度会达到O(n),会超时。

接下来google了一下,发现简单的插入排序和二分结合在一起就可以了,非常开森的自己码了一个二分,码完了又超时,心都碎了,明明人家拿C++码的二分就不超时。

后来发现java的ArrayList自带的就有Collections.binarySearch(),于是又拿这个尝试,果然变快了(为什么要歧视我自己写的二分,why......),但是最后一个例子又超时。想起来以前的教训,用BufferedReader代替了Scanner,然后就过了......java真的好坑啊,一不小心就超时。

最后代码如下:

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.Buffer;
import java.util.*;
class Solution{
private static int numberOfItems = 0;
private static void add(int number,ArrayList<Integer> a){
int positon = Collections.binarySearch(a, number);
if(positon<0)
positon = -positon-1; a.add(positon,number);
numberOfItems++;
printMedia(a);
} public static void printMedia(ArrayList<Integer> a){
if(numberOfItems>0){
if(numberOfItems%2==0)
{
long num = (long)a.get(numberOfItems/2)+(long)a.get(numberOfItems/2-1);
if(num%2 == 0)
System.out.printf("%d\n",num/2);
else {
System.out.printf("%.1f\n", num/2.0);
}
}
else {
System.out.println(a.get(numberOfItems/2));
}
}
else {
System.out.println("Wrong!");
}
}
public static void remove(int value,ArrayList<Integer> a){
int position = Collections.binarySearch(a, value);
if(position < 0)
System.out.println("Wrong!");
else{
a.remove(position);
numberOfItems--;
printMedia(a);
}
}
public static void main( String args[] )throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine()); ArrayList<Integer> a = new ArrayList<Integer>();
for(int i = 0;i < N;i++){
String[] words = br.readLine().trim().split(" ");
if(words[0].equals("a")){
add(Integer.parseInt(words[1]), a);
}
else{
remove(Integer.parseInt(words[1]), a);
}
}
}
}

不过这么折腾确实学到不少东西,总结一下:

1.在java中可以用priority queue实现堆,默认最小堆,用Collections.reverseOrder可以建一个最大堆;

2.在java中ArrayList自带的有二分方法:Collections.binarySearch(),这个方法的时间复杂度是O(log(n)),不知道为什么比自己写的快,特别注意它的返回值:

Returns:
the index of the search key, if it is contained in the list;
otherwise, (-(insertion point) - 1).
The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size() if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.

即当没有找到元素时,它返回return = (-(insertion point) - 1),insertion point就是第一个比找不到的元素大的元素的索引,即这个元素可以插入的位置,那么我们解出insertion point = -return-1,就是插入元素的位置了。

3.BufferedReader还是比Scanner节省不少时间的,以后尽量用它。

【HackerRank】Median的更多相关文章

  1. 【HackerRank】Find the Median(Partition找到数组中位数)

    In the Quicksort challenges, you sorted an entire array. Sometimes, you just need specific informati ...

  2. 【leetcode】Median of Two Sorted Arrays

    题目简述: There are two sorted arrays A and B of size m and n respectively. Find the median of the two s ...

  3. 【leedcode】 Median of Two Sorted Arrays

    https://leetcode.com/problems/median-of-two-sorted-arrays/ There are two sorted arrays nums1 and num ...

  4. 【leetcode】Median of Two Sorted Arrays(hard)★!!

    There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted ...

  5. 【HackerRank】How Many Substrings?

    https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...

  6. 【HackerRank】Running Time of Quicksort

    题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...

  7. 【hackerrank】Week of Code 30

    Candy Replenishing Robot Find the Minimum Number 直接模拟 Melodious password dfs输出方案 Poles 题意:有多个仓库,只能从后 ...

  8. 【hackerrank】Week of Code 26

    在jxzz上发现的一个做题网站,每周都有训练题,题目质量……前三题比较水,后面好神啊,而且类型差不多,这周似乎是计数专题…… Army Game 然后给出n*m,问需要多少个小红点能全部占领 解法:乘 ...

  9. 【HDU5857】Median

    题意 给出一个长度为n的有序序列.给出m个询问,每个询问包括四个正整数l1,r1,l2,r2你用l1tor1的和l2tor2的元素来组成一个新的序列,然后找出这个序列的中位数. 分析 这是当时Spri ...

随机推荐

  1. AtCoder Regular Contest 070 D - No Need 想法:利用单调性二分+bitset优化

    /** 题目:D - No Need 链接:http://arc070.contest.atcoder.jp/tasks/arc070_b 题意:给出N个数,从中选出一个子集,若子集和大于等于K,则这 ...

  2. 第一百六十六节,jQuery,基础 DOM 和 CSS 操作,元素内容,元素属性,css和class,元素宽度高度、偏移、滚动条

    jQuery,基础 DOM 和 CSS 操作,元素内容,元素属性,css和class,元素宽度高度.偏移.滚动条 学习要点: 1.DOM 简介 2.设置元素及内容 3.元素属性操作 4.元素样式操作 ...

  3. Tomcat无法启动:Server Tomcat v8.5 Server at localhost failed to start

    Tomcat无法启动 项目状态 Maven项目:基础环境(依赖,基本配置文件)搭建完成,前端页面都导入,部署测试项目环境,出现该问题 问题情景: 1.弹窗提示Tomcat启动失败  2.Console ...

  4. 补装老版本的Java SE

    到oracle注册账号下载bin文件 在bin文件下载目录打开终端复制下面到命令 # chmod +x jdk-6u21-linux-i586.bin 注意版本不同,比如我这次下载到是6u45 chm ...

  5. Android输入法的显示与隐藏

    显示输入法: public void ShowSoftInput(View v) { // v 接受输入的控件 mInputMethodManager = (InputMethodManager) ( ...

  6. (转)免费天气预报接口API以及全国所有地区代码!!

    国家气象局提供的天气预报接口 接口地址: http://www.weather.com.cn/data/sk/101010100.html http://www.weather.com.cn/data ...

  7. jq cookie

    //$.cookie("xx");//读取xx的值 //$.cookie("xx","123");//设置xx的值为123 //$.cook ...

  8. django实现密码加密的注册(数据对象插入)

    在 django实现密码非加密的注册(数据对象插入)的基础上,我们对视图和注册页面进行了简单修改 视图 from django.shortcuts import render,redirect,ren ...

  9. php安装pcntl扩展

    1.下载一个同样版本的php(我的是php5.6.27,我下的是php5.6.28) [root@CentOS6 lamp]# wget http://hk1.php.net/get/php-5.6. ...

  10. Mac下最好用的文本编辑器

    友情提醒:图多杀猫. 曾经在Windows下一直用gVim.能够用键盘控制一切,操作起来是又快又爽,还支持一大堆插件.想怎么玩就怎么玩.后来转Mac后,也沿袭着之前的习惯.一直在用终端的Vim.偶尔会 ...