In this challenge you need to print the data that accompanies each integer in a list. In addition, if two strings have the same integers, you need to print the strings in their original order. Hence, your sorting algorithm should be stable, i.e. the original order should be maintained for equal elements.

Insertion Sort and the simple version of QuickSort were stable, but the faster in-place version of Quicksort was not (since it scrambled around elements while sorting).

In cases where you care about the original order, it is important to use a stable sorting algorithm. In this challenge, you will use counting sort to sort a list while keeping the order of the strings (with same accompanying integer) preserved.

Challenge
In the previous challenge, you created a "helper array" that contains
information about the starting position of each element in a sorted
array. Can you use this array to help you create a sorted array of the
original list?

Hint: You can go through the original array to access the
strings. You can then use your helper array to help determine where to
place those strings in the sorted array. Be careful about being one off.

Details and a Twist
You will be given a list that contains both integers and strings. Can
you print the strings in order of their accompanying integers? If the
integers for two strings are equal, ensure that they are print in the
order they appeared in the original list.

The Twist - Your clients just called with an update. They
don't want you to print the first half of the original array. Instead,
they want you to print a dash for any element from the first half. So
you can modify your counting sort algorithm to sort the second half of
the array only.

Input Format
n - the size of the list ar.
n lines follow, each containing an integer x, and a string, s.

Output Format
Print the strings in their correct order.

Constraints
1 <= n <= 1000000
n is even
1 <= length(s) <= 10
0 <= x < 100 , x ∈ ar
The characters in every string s is in lowercase.


题解:

给出一串(整数,字符串)序列,要求根据整数的大小排序这些元组,然后把开始位于数组后半部分的单词按照排序后的单词输出,位于前半部分的单词则用“-”代替。

一个例子是:

Sample Input

20
0 ab
6 cd
0 ef
6 gh
4 ij
0 ab
6 cd
0 ef
6 gh
0 ij
4 that
3 be
0 to
1 be
5 question
1 or
2 not
4 is
2 to
4 the Sample Output - - - - - to be or not to be - that is the question - - - -

这里有两个信息十分重要,一个是整数对应的字符串和整数在原数组中的位置。

所以用两个map:

HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();
HashMap<Integer, ArrayList<Integer>> index_Map = new HashMap<Integer, ArrayList<Integer>>();

一个用来记录元组中第一个整数在原数组中的索引,一个用来记录元组中第一个整数对应的字符串。比如在map中,0对应的list是<ab,ef,ab,ef,ij,to>,在index_Map中,0对应的list是<0,2,5,7,9,12>。

这样就可以在得到两个hashmap后直接输出结果了。比如先根据两个hashmap输出0对应的串,第一个0对应的串在原数组中的索引是0,属于前半部分,所以它用"-"代替,0对应的最后一个串“to“在原数组中的索引是12,属于后半部分,所以它应该输出;依次再处理1,2,3...对应的串,就得到结果了。

代码如下:

 import java.io.*;
import java.util.*; public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int s = in.nextInt();
HashMap<Integer, ArrayList<String>> map = new HashMap<Integer, ArrayList<String>>();
HashMap<Integer, ArrayList<Integer>> index_Map = new HashMap<Integer, ArrayList<Integer>>();
int[] count = new int[100];
for(int i=0;i<s;i++){
int key = in.nextInt();
count[key]++;
String value = in.next();
if(!map.containsKey(key)){
map.put(key, new ArrayList<String>());
index_Map.put(key, new ArrayList<Integer>());
}
index_Map.get(key).add(i);
map.get(key).add(value);
} int mid = s/2;
StringBuffer sb = new StringBuffer();
for(int i = 0;i < 100;i++ )
{
if(map.containsKey(i)){
for(int j = 0;j < map.get(i).size();j++){
int index = index_Map.get(i).get(j);
String string = map.get(i).get(j);
if(index < mid)
sb.append("-").append(" ");
else
sb.append(string).append(" ");
}
}
}
System.out.println(sb.toString());
}
}

【HackerRank】 The Full Counting Sort的更多相关文章

  1. 【Atcoer】ARC088 E - Papple Sort

    [题目]E - Papple Sort [题意]给定长度为n的小写字母串,只能交换相邻字母,求形成回文串的最小步数.n<=2*10^5. [算法]数学 [题解]官方题解 本题题解中有以下重要的思 ...

  2. 【HackerRank】Insertion Sort Advanced Analysis(归并排序求数列逆序数对)

    Insertion Sort is a simple sorting technique which was covered in previous challenges. Sometimes, ar ...

  3. 【转】 std list/vector sort 排序

    [转自]http://blog.csdn.net/marising/article/details/4567531 网上江湖郎中和蒙古大夫很多,因此,此类帖子也很多.关于排序,我还真没研究过,看了江湖 ...

  4. 【HackerRank】Running Time of Quicksort

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

  5. 【转载】双调排序Bitonic Sort,适合并行计算的排序算法

    双调排序是data-independent的排序, 即比较顺序与数据无关的排序方法, 特别适合做并行计算,例如用GPU.fpga来计算. 1.双调序列 在了解双调排序算法之前,我们先来看看什么是双调序 ...

  6. 【HackerRank】How Many Substrings?

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

  7. 【HDOJ】4358 Boring counting

    基本思路是将树形结构转线性结构,因为查询的是从任意结点到叶子结点的路径.从而将每个查询转换成区间,表示从该结点到叶子结点的路径.离线做,按照右边界升序排序.利用树状数组区间修改.树状数组表示有K个数据 ...

  8. 【转】linux中的sort命令

    转自:http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分 ...

  9. 【转载】C#中自定义Sort的排序规则IComparable接口

    C#中的List集合在排序的时候,如果不使用Lambda表达式进行排序的话,一般调用Sort()方法进行排序,如果希望Sort()方法排序后的结果跟我们预想的效果一致或者按照我们自定义的规则排序,则需 ...

随机推荐

  1. python第三周文件处理和函数-----下

    #默认参数的值是在一开始定义的时候就传给了函数, # 在后来的修改中不会被修改. #默认参数的值必须放到位置形参参数的最后面 #默认参数使用的场景是一个参数不经常变得场景,所以参数一般是不可变类型.字 ...

  2. Discuz! X 插件开发手册

      文件命名规范 Discuz! 按照如下的规范对程序和模板进行命名,请在设计插件时尽量遵循此命名规范: 可以直接通过浏览器访问的普通程序文件,以 .php 后缀命名. 被普通程序文件引用的程序文件, ...

  3. Easyui Form增加myLoad方法,使其支持二级数据对象

    $.extend($.fn.form.methods, { myLoad : function (jq, param) { return jq.each(function () { load(this ...

  4. Spring4 MVC+Hibernate4 Many-to-many连接表+MySQL+Maven实例

    这篇文章显示 Hibernate 的多对多实例,在 Spring MVC CRUD Web应用程序中连接表.我们将同时讨论管理多对多关系在视图和后端. 我们将使用应用程序的Web界面创建,更新,删除和 ...

  5. response.sendRedirect 的功能是地址重定向(页面跳转)

    response.sendRedirect 的功能是地址重定向(页面跳转) 1.response.sendredirect(url); 新的页面并不能处理旧页面的pagecontext(request ...

  6. go http的三种实现---3

    package main //效率最高的一个方法 import ( "fmt" "io" "log" "net/http" ...

  7. jQuery-PHP跨域请求数据

    jQuery: //获取域名 function getDomain(url){ var a = document.createElement('a'); a.href = url; url=a.hos ...

  8. ffmpeg参数使用说明1

    1.     ffmpeg.exe -i F:闪客之家闪客之歌.mp3 -ab 56 -ar 22050 -b 500 -r 15 -s 320x240 f:11.flv 2.     ffmpeg ...

  9. fecha的使用

    项目中时间的处理是无法避免的,时间的处理方式有很多,这里介绍一下fecha的使用 fecha是一个日期格式化和解析的js库,它提供了强大的日期处理功能,功能强大且只有2k大小.安装方式简单,只需要 n ...

  10. Linux安装配置

    虚拟机配置Linux镜像文件 配置网路 防火墙 一.虚拟机配置Linux镜像文件 1.将下载好的Linux镜像文件载入进来 2.启动虚拟机,安装 3.此步为是否检测linux系统,我们选择" ...