@@ -168,54 +168,54 @@ int main() {
168
168
169
169
170
170
` ` ` java
171
- import java .io .* ;
172
171
import java .util .* ;
173
172
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 <> ();
186
184
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 ];
188
192
for (int rank = 0 ; rank < N ; rank ++ ) {
189
- int cow = Integer .parseInt (ranking [rank ]);
190
- positions [i ][cow ] = rank ;
193
+ pos [ranking [rank ]] = rank ;
191
194
}
195
+ positions .add (pos );
192
196
}
193
-
194
- int consistent_pairs = 0 ;
195
197
198
+ int consistentPairs = 0 ;
196
199
197
200
for (int i = 1 ; i <= N ; i ++ ) {
198
201
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 ;
201
204
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 ;
204
207
}
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 ;
207
210
}
208
211
}
209
- if (i_before_j || j_before_i ) {
210
- consistent_pairs ++ ;
212
+ if (iBeforeJ || jBeforeI ) {
213
+ consistentPairs ++ ;
211
214
}
212
215
}
213
216
}
214
217
215
-
216
- pw .println (consistent_pairs );
217
- br .close ();
218
- pw .close ();
218
+ System .out .println (consistentPairs );
219
219
}
220
220
}
221
221
0 commit comments