@@ -102,64 +102,47 @@ Then for every pair of cows, we just check the positions for every contest to se
102
102
103
103
104
104
``` cpp
105
- #include < iostream>
106
105
#include < fstream>
106
+ #include < iostream>
107
107
#include < vector>
108
108
109
-
110
109
int main () {
111
- std::ifstream infile("gymnastics.in");
112
- std::ofstream outfile("gymnastics.out");
113
-
114
-
115
- int K, N;
116
- infile >> K >> N;
117
-
118
-
119
- std::vector<std::vector<int>> positions(K, std::vector<int>(N + 1));
120
-
121
- for (int i = 0; i < K; ++i) {
122
- for (int j = 0 ; j < N ; ++ j ) {
123
- int cow;
124
- infile >> cow;
125
- positions[i ][cow ] = j;
126
- }
127
- }
128
-
129
-
130
- int consistent_pairs = 0;
131
-
132
-
133
- for (int i = 1; i <= N; ++i) {
134
- for (int j = i + 1; j <= N; ++j) {
135
- bool i_before_j = true;
136
- bool j_before_i = true;
137
- for (int s = 0; s < K; ++s) {
138
- if (positions[s ][i ] > positions[s ][j ]) {
139
- i_before_j = false;
140
- }
141
- if (positions[s ][j ] > positions[s ][i ]) {
142
- j_before_i = false;
143
- }
144
- }
145
- if (i_before_j || j_before_i) {
146
- consistent_pairs++;
147
- }
148
- }
149
- }
150
-
151
-
152
- outfile << consistent_pairs << std::endl ;
153
-
154
-
155
- infile .close ();
156
- outfile .close ();
157
- return 0 ;
110
+ std::ifstream infile("gymnastics.in");
111
+ std::ofstream outfile("gymnastics.out");
112
+
113
+ int K, N;
114
+ infile >> K >> N;
115
+
116
+ std::vector<std::vector<int>> positions(K, std::vector<int>(N + 1));
117
+
118
+ for (int i = 0; i < K; ++i) {
119
+ for (int j = 0 ; j < N ; ++ j ) {
120
+ int cow;
121
+ infile >> cow;
122
+ positions[i ][cow ] = j;
123
+ }
124
+ }
125
+
126
+ int consistent_pairs = 0;
127
+
128
+ for (int i = 1; i <= N; ++i) {
129
+ for (int j = i + 1; j <= N; ++j) {
130
+ bool i_before_j = true;
131
+ bool j_before_i = true;
132
+ for (int s = 0; s < K; ++s) {
133
+ if (positions[s ][i ] > positions[s ][j ]) { i_before_j = false; }
134
+ if (positions[s ][j ] > positions[s ][i ]) { j_before_i = false; }
135
+ }
136
+ if (i_before_j || j_before_i) { consistent_pairs++; }
137
+ }
138
+ }
139
+
140
+ outfile << consistent_pairs << std::endl ;
141
+
142
+ infile .close ();
143
+ outfile .close ();
144
+ return 0 ;
158
145
}
159
-
160
-
161
-
162
-
163
146
` ` `
164
147
165
148
@@ -171,55 +154,45 @@ int main() {
171
154
import java .util .* ;
172
155
173
156
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 <> ();
184
- for (int i = 0 ; i < K ; i ++ ) {
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 ];
192
- for (int rank = 0 ; rank < N ; rank ++ ) {
193
- pos [ranking [rank ]] = rank ;
194
- }
195
- positions .add (pos );
196
- }
197
-
198
- int consistentPairs = 0 ;
199
-
200
- for (int i = 1 ; i <= N ; i ++ ) {
201
- for (int j = i + 1 ; j <= N ; j ++ ) {
202
- boolean iBeforeJ = true ;
203
- boolean jBeforeI = true ;
204
- for (int s = 0 ; s < K ; s ++ ) {
205
- if (positions .get (s )[i ] > positions .get (s )[j ]) {
206
- iBeforeJ = false ;
207
- }
208
- if (positions .get (s )[j ] > positions .get (s )[i ]) {
209
- jBeforeI = false ;
210
- }
211
- }
212
- if (iBeforeJ || jBeforeI ) {
213
- consistentPairs ++ ;
214
- }
215
- }
216
- }
217
-
218
- System .out .println (consistentPairs );
219
- }
157
+ public static void main(String [] args ) {
158
+ Scanner scanner = new Scanner (System .in );
159
+ String inputData = scanner .useDelimiter (" \\ A" ).next ();
160
+ String [] data = inputData .split (" \\ s+" );
161
+
162
+ int K = Integer .parseInt (data [0 ]);
163
+ int N = Integer .parseInt (data [1 ]);
164
+ int index = 2 ;
165
+
166
+ List < int []> positions = new ArrayList <> ();
167
+ for (int i = 0 ; i < K ; i ++ ) {
168
+ int [] ranking = new int [N ];
169
+ for (int j = 0 ; j < N ; j ++ ) {
170
+ ranking [j ] = Integer .parseInt (data [index ++ ]);
171
+ }
172
+
173
+ // Precompute positions of each cow
174
+ int [] pos = new int [N + 1 ];
175
+ for (int rank = 0 ; rank < N ; rank ++ ) { pos [ranking [rank ]] = rank ; }
176
+ positions .add (pos );
177
+ }
178
+
179
+ int consistentPairs = 0 ;
180
+
181
+ for (int i = 1 ; i <= N ; i ++ ) {
182
+ for (int j = i + 1 ; j <= N ; j ++ ) {
183
+ boolean iBeforeJ = true ;
184
+ boolean jBeforeI = true ;
185
+ for (int s = 0 ; s < K ; s ++ ) {
186
+ if (positions .get (s )[i ] > positions .get (s )[j ]) { iBeforeJ = false ; }
187
+ if (positions .get (s )[j ] > positions .get (s )[i ]) { jBeforeI = false ; }
188
+ }
189
+ if (iBeforeJ || jBeforeI ) { consistentPairs ++ ; }
190
+ }
191
+ }
192
+
193
+ System .out .println (consistentPairs );
194
+ }
220
195
}
221
-
222
-
223
196
` ` `
224
197
225
198
@@ -229,7 +202,8 @@ public class Main {
229
202
230
203
` ` ` py
231
204
import sys
232
- sys .stdin = open (" gymnastics.in" ," r" )
205
+
206
+ sys .stdin = open (" gymnastics.in" , " r" )
233
207
sys .stdout = open (" gymnastics.out" , " w" )
234
208
input_data = sys .stdin .read ().split ()
235
209
@@ -241,30 +215,30 @@ index = 2
241
215
242
216
positions = []
243
217
for i in range (K ):
244
- ranking = list (map (int , input_data [index : index + N ]))
245
- index += N
246
-
247
- # Precompute positions of each cow
248
- pos = [0 ] * (N + 1 )
249
- for rank , cow in enumerate (ranking ):
250
- pos [cow ] = rank
251
- positions .append (pos )
218
+ ranking = list (map (int , input_data [index : index + N ]))
219
+ index += N
220
+
221
+ # Precompute positions of each cow
222
+ pos = [0 ] * (N + 1 )
223
+ for rank , cow in enumerate (ranking ):
224
+ pos [cow ] = rank
225
+ positions .append (pos )
252
226
253
227
254
228
consistent_pairs = 0
255
229
256
230
257
231
for i in range (1 , N + 1 ):
258
- for j in range (i + 1 , N + 1 ):
259
- i_before_j = True
260
- j_before_i = True
261
- for s in range (K ):
262
- if positions [s ][i ] > positions [s ][j ]:
263
- i_before_j = False
264
- if positions [s ][j ] > positions [s ][i ]:
265
- j_before_i = False
266
- if i_before_j or j_before_i :
267
- consistent_pairs += 1
232
+ for j in range (i + 1 , N + 1 ):
233
+ i_before_j = True
234
+ j_before_i = True
235
+ for s in range (K ):
236
+ if positions [s ][i ] > positions [s ][j ]:
237
+ i_before_j = False
238
+ if positions [s ][j ] > positions [s ][i ]:
239
+ j_before_i = False
240
+ if i_before_j or j_before_i :
241
+ consistent_pairs += 1
268
242
269
243
270
244
print (consistent_pairs )
0 commit comments