POJ 2299 Ultra-QuickSort
来源:互联网 发布:usb控制软件破解版 编辑:程序博客网 时间:2024/06/06 00:48
Ultra-QuickSort
Time Limit: 7000MS Memory Limit: 65536KTotal Submissions: 48348 Accepted: 17652
Description
In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence
9 1 0 5 4 ,
Ultra-QuickSort produces the output
0 1 4 5 9 .
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
Ultra-QuickSort produces the output
Your task is to determine how many swap operations Ultra-QuickSort needs to perform in order to sort a given input sequence.
Input
The input contains several test cases. Every test case begins with a line that contains a single integer n < 500,000 -- the length of the input sequence. Each of the the following n lines contains a single integer 0 ≤ a[i] ≤ 999,999,999, the i-th input sequence element. Input is terminated by a sequence of length n = 0. This sequence must not be processed.
Output
For every input sequence, your program prints a single line containing an integer number op, the minimum number of swap operations necessary to sort the given input sequence.
Sample Input
59105431230
Sample Output
60
因为看的是《算法》的第二章排序的前几节,所以专门挑的排序的题来做的。这是一个求逆序对数的题,题意是给你多个序列,每个序列前都有他的序列的长度。是这样的:5(序列长度) 9 1 0 5 4(序列中的值) 3(序列长度) 1 2 3(序列中的值) …… 0(表示结束)。刚开始想直接暴力计算逆序对数,但担心超时,于是网上找了下别人的思路。原来归并排序可以求逆序对数,而且非常的优美和巧妙。中间有一行注释了“This is the core.”,没错,那行就是关键。
import java.util.Scanner;/** * Created by 小粤 on 2015/8/3. */public class Main{ private static int[] aux = new int[500000]; private static int[] numbers = new int[500000]; private static long counter = 0; // This counter must be long. private static boolean less(int a, int b) { return a - b < 0; } private static void sort(int lo, int hi) { if (lo >= hi) { return; } int mid = (lo + hi) / 2; sort(lo, mid); sort(mid + 1, hi); merge(lo, mid, hi); } private static void merge(int lo, int mid, int hi) { for (int k = lo; k <= hi; k++) { aux[k] = numbers[k]; } int i = lo, j = mid + 1; for (int k = lo; k <= hi; k++) { if (i > mid) { numbers[k] = aux[j++]; } else if (j > hi) { numbers[k] = aux[i++]; } else if (less(aux[j], aux[i])) { numbers[k] = aux[j++]; counter += mid - i + 1; // This is the core. } else { numbers[k] = aux[i++]; } } } public static void main(String[] args) { int lengthOfNumbers = 0; Scanner scanner = new Scanner(System.in); while (true) { lengthOfNumbers = scanner.nextInt(); if (lengthOfNumbers != 0) { for (int i = 0; i < lengthOfNumbers; i++) { numbers[i] = scanner.nextInt(); } counter = 0; sort(0, lengthOfNumbers - 1); System.out.println(counter); } else { break; } } }}
0 0
- POJ 2299 Ultra-QuickSort
- poj 2299 Ultra-QuickSort
- poj 2299 Ultra-QuickSort
- poj 2299 Ultra-QuickSort
- poj 2299 Ultra-QuickSort
- poj 2299Ultra-quicksort
- POJ-2299 Ultra-QuickSort
- POJ 2299 Ultra-QuickSort
- POJ 2299 Ultra-QuickSort
- poj 2299 Ultra-QuickSort
- POJ 2299 Ultra-QuickSort
- POJ 2299 Ultra-QuickSort
- POJ 2299 Ultra-QuickSort
- POJ 2299 - Ultra-QuickSort
- POJ 2299 Ultra-QuickSort
- POJ 2299 Ultra-QuickSort
- POJ 2299 Ultra-QuickSort
- POJ 2299 Ultra-QuickSort
- Http网络传递参数中文乱码问题
- SLAM: SLAM的发展历程
- day17
- hdu1024 N! (数组模拟乘法过程)
- 九度OJ 题目1029:魔咒词典
- POJ 2299 Ultra-QuickSort
- hdu 1325 水题
- 病毒传播
- 解决ERROR 1201 (HY000): Could not initialize master info structure
- LocalBroadcastManager源码
- Linux - Bash shell的功能;内建命令type
- 读《数学之美》第三章 统计语言模型
- The constructor SimpleAdapter(new View.OnClickListener(){},...)is undefined的问题及解决
- HDOJ 1222 Wolf and Rabbit(数学解题技巧)