-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinimizeAbsoluteDifferenceBetter.java
150 lines (137 loc) · 3.82 KB
/
MinimizeAbsoluteDifferenceBetter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
#backtracking #todo
*/
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
class MinimizeAbsoluteDifferenceBetter {
public static void rec(int[] index, int size, int[] a, bool mark[], int[] result) {
if (size == 4) {
// todo
// a[index[0]] / a[index[1]] - a[index[2]] / a[index[3]]
// fraction_index < fraction_result: update
}
for (int i = 0; i < 5; i++) {
if (!mark[i]) {
mark[i] = true;
index[size] = i;
// todo
// rec(index, size + 1, a, mark, result)
index[size] = -1;
mark[i] = false;
}
}
}
public static void calculate(
HashMap<Integer, ArrayList<Integer>> map,
int[] orgArr,
int len,
int[] result,
ArrayList<Fraction> resultFraction,
int[] visitedArr) {
// success
if (len == 4) {
resultFraction.add(new Fraction(result[0], result[1], result[2], result[3]));
return;
}
// try
for (int i = 0; i < visitedArr.length; i++) {
if (visitedArr[i] == 0) {
visitedArr[i] = 1;
result[len] = i;
for (int x : map.get(i)) {
if (orgArr[x] < orgArr[i]) continue;
if (visitedArr[x] == 0) {
result[len + 1] = x;
visitedArr[x] = 1;
calculate(map, orgArr, len + 2, result, resultFraction, visitedArr);
result[len + 1] = -1;
visitedArr[x] = 0;
}
}
result[len] = -1;
visitedArr[i] = 0;
}
}
}
public static void main(String[] args) {
// get input
Scanner sc = new Scanner(System.in);
int[] orgArr = new int[5];
for (int i = 0; i < orgArr.length; i++) {
int number = sc.nextInt();
orgArr[i] = number;
}
// calculate
HashMap<Integer, ArrayList<Integer>> map = new HashMap<>();
for (int i = 0; i < orgArr.length; i++) {
map.put(i, new ArrayList<Integer>());
for (int j = 0; j < orgArr.length; j++) {
if (i != j && orgArr[i] <= orgArr[j]) {
map.get(i).add(j);
}
}
}
int[] arr = new int[4];
int[] visitedArr = new int[orgArr.length];
ArrayList<Fraction> resultFraction = new ArrayList<>();
calculate(map, orgArr, 0, arr, resultFraction, visitedArr);
// result
Fraction.arr = orgArr;
Collections.sort(resultFraction);
Fraction ans = resultFraction.get(0);
Fraction fraction;
for (int i = 1; i < resultFraction.size(); i++) {
fraction = resultFraction.get(i);
if (ans.compareTo(fraction) == 0) {
if (Fraction.compareOrder(ans, fraction) > 0) {
ans = fraction;
}
} else {
break;
}
}
System.out.println(ans);
}
}
class Fraction implements Comparable<Fraction> {
public static int compareOrder(Fraction f1, Fraction f2) {
if (f1.a != f2.a) return f1.a - f2.a;
if (f1.b != f2.b) return f1.b - f2.b;
if (f1.c != f2.c) return f1.c - f2.c;
return f1.d - f2.d;
}
int a, b, c, d;
static int[] arr = null;
public Fraction(int a, int b, int c, int d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
public int compareTo(Fraction other) {
long v1 =
(long) Math.abs(arr[this.a] * arr[this.d] - arr[this.c] * arr[this.b])
* arr[other.b]
* arr[other.d];
long v2 =
(long) Math.abs(arr[other.a] * arr[other.d] - arr[other.c] * arr[other.b])
* arr[this.b]
* arr[this.d];
if (v1 > v2) {
return 1;
} else if (v1 < v2) {
return -1;
}
return 0;
}
public String toString() {
return (a + " " + b + " " + c + " " + d);
// if (a < c || (a == c && b < d)) {
// return (a + " " + b + " " + c + " " + d);
// } else {
// return (c + " " + d + " " + a + " " + b);
// }
}
}