【HackerRank】 The Full Counting Sort
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的更多相关文章
- 【Atcoer】ARC088 E - Papple Sort
[题目]E - Papple Sort [题意]给定长度为n的小写字母串,只能交换相邻字母,求形成回文串的最小步数.n<=2*10^5. [算法]数学 [题解]官方题解 本题题解中有以下重要的思 ...
- 【HackerRank】Insertion Sort Advanced Analysis(归并排序求数列逆序数对)
Insertion Sort is a simple sorting technique which was covered in previous challenges. Sometimes, ar ...
- 【转】 std list/vector sort 排序
[转自]http://blog.csdn.net/marising/article/details/4567531 网上江湖郎中和蒙古大夫很多,因此,此类帖子也很多.关于排序,我还真没研究过,看了江湖 ...
- 【HackerRank】Running Time of Quicksort
题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...
- 【转载】双调排序Bitonic Sort,适合并行计算的排序算法
双调排序是data-independent的排序, 即比较顺序与数据无关的排序方法, 特别适合做并行计算,例如用GPU.fpga来计算. 1.双调序列 在了解双调排序算法之前,我们先来看看什么是双调序 ...
- 【HackerRank】How Many Substrings?
https://www.hackerrank.com/challenges/how-many-substrings/problem 题解 似乎是被毒瘤澜澜放弃做T3的一道题(因为ASDFZ有很多人做过 ...
- 【HDOJ】4358 Boring counting
基本思路是将树形结构转线性结构,因为查询的是从任意结点到叶子结点的路径.从而将每个查询转换成区间,表示从该结点到叶子结点的路径.离线做,按照右边界升序排序.利用树状数组区间修改.树状数组表示有K个数据 ...
- 【转】linux中的sort命令
转自:http://www.cnblogs.com/51linux/archive/2012/05/23/2515299.html sort是在Linux里非常常用的一个命令,管排序的,集中精力,五分 ...
- 【转载】C#中自定义Sort的排序规则IComparable接口
C#中的List集合在排序的时候,如果不使用Lambda表达式进行排序的话,一般调用Sort()方法进行排序,如果希望Sort()方法排序后的结果跟我们预想的效果一致或者按照我们自定义的规则排序,则需 ...
随机推荐
- UVA 1640 The Counting Problem UVA1640 求[a,b]或者[b,a]区间内0~9在里面各个数的数位上出现的总次数。
/** 题目:UVA 1640 The Counting Problem UVA1640 链接:https://vjudge.net/problem/UVA-1640 题意:求[a,b]或者[b,a] ...
- SecurCRT 远程linux 输入中文及 oracle 查询出文号问题
一. 首先确认你的linux是否设置了支持中文 cat /etc/sysconfig/i18n 其中: LANG 变量是 language 的简称, 这个变量时决定系统的默认语言, 即系统菜单, 程序 ...
- 第一百九十八节,jQuery EasyUI,ProgressBar(进度条)组件
jQuery EasyUI,ProgressBar(进度条)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 ProgressBar(进度条) ...
- C++调用Fortran程序----动态链接方式
参考http://yxbwuhee.blog.sohu.com/143577510.html 一.C++动态调用Fortran DLL (1)创建FORTRAN DLL工程,生成forsubs.dll ...
- poj 2594(可相交的最小路径覆盖)
题目链接:http://poj.org/problem?id=2594 思路:本来求最小路径覆盖是不能相交的,那么对于那些本来就可达的点怎么处理,我们可以求一次传递闭包,相当于是加边,这样我们就可以来 ...
- iOS View生成--->清晰的图片
核心代码 view生成图片方法 UIGraphicsBeginImageContext(view.bounds.size); [view.layer renderInContext:UIGraphic ...
- 第二篇:尽可能使用 const
前言 const 关键字是常量修辞符,如果要告知编译器某个变量在程序中不会发生改变,则可将其声明为 const. 但,对 const 关键字的认识不能仅仅停留在这一层 - 它提供了很多更强大的功能. ...
- 转:: 刺鸟:用python来开发webgame服务端(5)
来源:http://ciniao.me/article.php?id=19 --------------- 刺鸟原创文章,转载请注明出处 在前面的文章中,我们已经开始了不少逻辑功能的开发,在这期 ...
- Android自定义Button字体颜色和背景颜色
http://blog.csdn.net/breeze666/article/details/7747649
- PHP连接MySQL数据库操作
(原文链接:http://www.cnblogs.com/csensix/archive/2012/05/23/2515494.html) 方法一:普通方法(面向过程) 首先,先做出如下假设(也适用与 ...