POJ Brush-Up 系列:2299。超快速分类
最编程
2024-07-09 11:35:09
...
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1434484
POJ 刷题系列:2299. Ultra-QuickSort
传送门:2299. Ultra-QuickSort
题意:
给出长度为n的序列,每次只能交换相邻的两个元素,问至少要交换几次才使得该序列为递增序列。
思路:
实际上是让你求解逆序对的个数,求逆序对的个数可以采用归并排序。曾写过一篇关于算法细节系列(27):时间复杂度为何还能优化?今天又重新复习了一遍,有了一些新的理解。除了分治能够把解决问题的深度限定在logn\log n内之外,此处还有一个优化的关键,当给定了两个子数组a,b时,因为a和b均已排好序,此时在数组b中找出比数组a小的元素,可以采用【统一加】的原则,比如数组a中有元素:
a = [3, 4, 5, 6]
b = [1, 2, 3, 4]
对于数组a中的3而言,b中存在1和2,所以count += 2
此时,对于数组a中的4而言,b中存在1,2和3,count += 3
因为1,2对于a中的3而言一定是逆序对,那么对于a中的4更是如此,所以我们完全可以设置一个全局指针j,在上一轮的基础上继续比较,详见代码。
代码如下:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Map;
import java.util.StringTokenizer;
public class Main{
String INPUT = "./data/judge/201801/P2299.txt";
public static void main(String[] args) throws IOException {
new Main().run();
}
int N;
long count;
long[] num;
void merge(int s, int e) {
if (e - s <= 1) return;
int m = (s + e) / 2;
merge(s, m);
merge(m, e);
for (int i = s, j = m; i < m; ++i) {
while (j < e && num[i] > num[j]) j ++;
count += j - m;
}
mergeSort(s, e);
}
void mergeSort(int s, int e) {
int m = (s + e) / 2;
long[] aux = new long[e - s];
int i = s;
int j = m;
int k = 0;
while (i < m && j < e) {
if (num[i] < num[j]) aux[k++] = num[i++];
else aux[k++] = num[j++];
}
while (i < m) aux[k++] = num[i++];
while (j < e) aux[k++] = num[j++];
for (int t = 0, l = s; l < e; ++l) num[l] = aux[t++];
}
void read() {
while (true) {
N = ni();
if (N == 0) break;
num = new long[N];
for (int i = 0; i < N; ++i) {
num[i] = ni();
}
count = 0;
merge(0, N);
out.println(count);
}
}
FastScanner in;
PrintWriter out;
void run() throws IOException {
boolean oj;
try {
oj = ! System.getProperty("user.dir").equals("F:\\oxygen_workspace\\Algorithm");
} catch (Exception e) {
oj = System.getProperty("ONLINE_JUDGE") != null;
}
InputStream is = oj ? System.in : new FileInputStream(new File(INPUT));
in = new FastScanner(is);
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
read();
out.flush();
if (!oj){
System.out.println("[" + (System.currentTimeMillis() - s) + "ms]");
}
}
public boolean more(){
return in.hasNext();
}
public int ni(){
return in.nextInt();
}
public long nl(){
return in.nextLong();
}
public double nd(){
return in.nextDouble();
}
public String ns(){
return in.nextString();
}
public char nc(){
return in.nextChar();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
boolean hasNext;
public FastScanner(InputStream is) throws IOException {
br = new BufferedReader(new InputStreamReader(is));
hasNext = true;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
hasNext = false;
return "##";
}
}
return st.nextToken();
}
String next = null;
public boolean hasNext(){
next = nextToken();
return hasNext;
}
public int nextInt() {
if (next == null){
hasNext();
}
String more = next;
next = null;
return Integer.parseInt(more);
}
public long nextLong() {
if (next == null){
hasNext();
}
String more = next;
next = null;
return Long.parseLong(more);
}
public double nextDouble() {
if (next == null){
hasNext();
}
String more = next;
next = null;
return Double.parseDouble(more);
}
public String nextString(){
if (next == null){
hasNext();
}
String more = next;
next = null;
return more;
}
public char nextChar(){
if (next == null){
hasNext();
}
String more = next;
next = null;
return more.charAt(0);
}
}
static class D{
public static void pp(int[][] board, int row, int col) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
sb.append(board[i][j] + (j + 1 == col ? "\n" : " "));
}
}
System.out.println(sb.toString());
}
public static void pp(char[][] board, int row, int col) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
sb.append(board[i][j] + (j + 1 == col ? "\n" : " "));
}
}
System.out.println(sb.toString());
}
}
static class ArrayUtils {
public static void fill(int[][] f, int value) {
for (int i = 0; i < f.length; ++i) {
Arrays.fill(f[i], value);
}
}
public static void fill(int[][][] f, int value) {
for (int i = 0; i < f.length; ++i) {
fill(f[i], value);
}
}
public static void fill(int[][][][] f, int value) {
for (int i = 0; i < f.length; ++i) {
fill(f[i], value);
}
}
}
static class Num{
public static <K> void inc(Map<K, Integer> mem, K k) {
if (!mem.containsKey(k)) mem.put(k, 0);
mem.put(k, mem.get(k) + 1);
}
}
}