Skip to content

Commit 372c173

Browse files
authored
Update Precomp.mdx [java code]
1 parent e0ce3c8 commit 372c173

File tree

1 file changed

+30
-30
lines changed

1 file changed

+30
-30
lines changed

content/2_Bronze/Precomp.mdx

+30-30
Original file line numberDiff line numberDiff line change
@@ -168,54 +168,54 @@ int main() {
168168
169169
170170
```java
171-
import java.io.*;
172171
import java.util.*;
173172

174-
175-
public class Gymnastics {
176-
public static void main(String[] args) throws IOException {
177-
BufferedReader br = new BufferedReader(new FileReader("gymnastics.in"));
178-
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("gymnastics.out")));
179-
180-
String[] input_data = br.readLine().split(" ");
181-
int K = Integer.parseInt(input_data[0]);
182-
int N = Integer.parseInt(input_data[1]);
183-
184-
int[][] positions = new int[K][N + 1];
185-
173+
public class Main {
174+
public static void main(String[] args) {
175+
Scanner scanner = new Scanner(System.in);
176+
String inputData = scanner.useDelimiter("\\A").next();
177+
String[] data = inputData.split("\\s+");
178+
179+
int K = Integer.parseInt(data[0]);
180+
int N = Integer.parseInt(data[1]);
181+
int index = 2;
182+
183+
List<int[]> positions = new ArrayList<>();
186184
for (int i = 0; i < K; i++) {
187-
String[] ranking = br.readLine().split(" ");
185+
int[] ranking = new int[N];
186+
for (int j = 0; j < N; j++) {
187+
ranking[j] = Integer.parseInt(data[index++]);
188+
}
189+
190+
// Precompute positions of each cow
191+
int[] pos = new int[N + 1];
188192
for (int rank = 0; rank < N; rank++) {
189-
int cow = Integer.parseInt(ranking[rank]);
190-
positions[i][cow] = rank;
193+
pos[ranking[rank]] = rank;
191194
}
195+
positions.add(pos);
192196
}
193-
194-
int consistent_pairs = 0;
195197

198+
int consistentPairs = 0;
196199

197200
for (int i = 1; i <= N; i++) {
198201
for (int j = i + 1; j <= N; j++) {
199-
boolean i_before_j = true;
200-
boolean j_before_i = true;
202+
boolean iBeforeJ = true;
203+
boolean jBeforeI = true;
201204
for (int s = 0; s < K; s++) {
202-
if (positions[s][i] > positions[s][j]) {
203-
i_before_j = false;
205+
if (positions.get(s)[i] > positions.get(s)[j]) {
206+
iBeforeJ = false;
204207
}
205-
if (positions[s][j] > positions[s][i]) {
206-
j_before_i = false;
208+
if (positions.get(s)[j] > positions.get(s)[i]) {
209+
jBeforeI = false;
207210
}
208211
}
209-
if (i_before_j || j_before_i) {
210-
consistent_pairs++;
212+
if (iBeforeJ || jBeforeI) {
213+
consistentPairs++;
211214
}
212215
}
213216
}
214217

215-
216-
pw.println(consistent_pairs);
217-
br.close();
218-
pw.close();
218+
System.out.println(consistentPairs);
219219
}
220220
}
221221

0 commit comments

Comments
 (0)