Last active
July 17, 2016 12:21
-
-
Save liuxueyang/8c05b3620a5f5ff36c05aeeda588e985 to your computer and use it in GitHub Desktop.
Java离散化,根据类的某个成员排序数组
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.util.Arrays; | |
import java.util.Comparator; | |
import java.util.Scanner; | |
public class Main { | |
public static void main(String[] args) throws FileNotFoundException { | |
int t; | |
Scanner cin; | |
FileInputStream fin; | |
fin = new FileInputStream("/Users/liu/Hack/algorithm/spoj/INVCNT/src/input.txt"); | |
cin = new Scanner(fin); | |
// cin = new Scanner(System.in); | |
t = cin.nextInt(); | |
while (t-- != 0) { | |
int n; | |
Point[] a; | |
n = cin.nextInt(); | |
a = new Point[n]; | |
for (int i = 0; i < n; ++i) { | |
a[i] = new Point(); | |
a[i].value = cin.nextInt(); | |
a[i].pos = i + 1; | |
} | |
Arrays.sort(a); | |
// compress y | |
for (int i = 0; i < n; ++i) { | |
a[i].compressValue = i + 1; | |
} | |
Arrays.sort(a, Point.PosComparator); | |
} | |
} | |
static class Point implements Comparable { | |
int value; | |
int pos; | |
int compressValue; | |
public static Comparator<Point> PosComparator = new Comparator<Point>() { | |
public int compare(Point o1, Point o2) { | |
return o1.pos - o2.pos; | |
} | |
}; | |
@Override | |
public int compareTo(Object o) { | |
return this.value - ((Point) o).value; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment