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. sqrt函数实现(神奇的算法)

    我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...

  2. 个人博客开发之 全局配置文件settings设置

    项目源码下载:http://download.vhosts.cn # -*- coding: utf-8 -*- """ Django settings for cpyb ...

  3. dropdown多选下拉框

    写好了一个dropdown多选框.直接粘下面代码就能用 效果展示: temp2.jsp <%@page import="com.util.LabelCacheManager" ...

  4. plsql programming 10 日期和时间戳

    年 月 日 时 分 秒 时区 用小时表示的相对于 UTC 的时差 用分钟表示的相对于 UTC 的时差 date 存储日期和时间, 不带时区, 精确到秒 timestamp 存储日期和时间, 不带时区, ...

  5. select option 不可以选

    <select> <option>Volvo</option> <option>Saab</option> <option disab ...

  6. references non-existing project XXX, launch configuration问题的解决办法

    Go to Project->properties In properties window's left pane select "Run/Debug Settings". ...

  7. Python使用MySQL数据库的

    然而,2016年开始,我从Python2切换到了Python3,Python2已经基本不再使用,MySQLdb驱动从2014年1月停止了维护.所以,打算重新再来写这篇博客. Python2 ---&g ...

  8. 1. 写出一个能创建多级目录的 PHP 函数(新浪网技术部)

    function create_dir($path,$mode){ if (is_dir($path)){ echo "该目录已经存在"; }else{ if(mkdir($pat ...

  9. hibernate 懒加载图解

  10. 源码探究Java_HashMap

    1. HashMap 定义,抽取HashMap类中主要变量,如下 public class HashMap<K,V> extends AbstractMap<K,V> impl ...