#include <bits/stdc++.h>
using namespace std;
#define ll long long
// returns true if fraction a < fraction b
bool better(pair<ll, ll> a, pair<ll, ll> b) {
return __int128(a.first)*b.second<__int128(a.second)*b.first;
}
pair<ll,ll> reduce(pair<ll, ll>a) {
ll g = __gcd(a.first, a.second);
return make_pair(a.first/g,a.second/g);
}
int main() {
int n, l; cin >> n >> l;
ll V[n][l];
ll sum[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < l; j++) cin >> V[i][j];
sum[i] = 0; for (int j = 0; j < l; j++) sum[i] += V[i][j];
}
pair<ll, ll> split[n][n];
for (int i = 0; i < n; i++) {
int cur = 0;
ll want = 0;
for (int j = 1; j < n; j++) {
want += sum[i];
while (cur < l && V[i][cur]*n <= want) {
want -= V[i][cur]*n;
cur++;
}
split[i][j] = reduce(make_pair((ll)cur*V[i][cur]*n+want, V[i][cur]*n));
//cout << "split[" << i << "][" << j << "] = " << split[i][j].first << "/" << split[i][j].second << endl;
}
}
bool taken[n]; for (int i = 0; i < n; i++) taken[i] = false;
vector<int> permutation;
for (int i = 1; i < n; i++) {
int best = -1;
for (int j = 0; j < n; j++) {
if (taken[j]) continue;
if (best == -1) {
best = j;
} else {
if (better(split[j][i], split[best][i])) best = j;
}
}
taken[best] = true;
cout << split[best][i].first << " " << split[best][i].second << endl;
permutation.push_back(best+1);
}
for (int j = 0; j < n; j++) if (!taken[j]) permutation.push_back(j+1);
for (int x : permutation) cout << x << " ";
cout << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
384 KB |
Output is correct |
12 |
Correct |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
6 ms |
384 KB |
Output is correct |
14 |
Correct |
6 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
7 ms |
384 KB |
Output is correct |
6 |
Correct |
6 ms |
384 KB |
Output is correct |
7 |
Correct |
6 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
7 ms |
384 KB |
Output is correct |
10 |
Correct |
7 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
256 KB |
Output is correct |
13 |
Correct |
6 ms |
384 KB |
Output is correct |
14 |
Correct |
7 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
8 ms |
384 KB |
Output is correct |
17 |
Correct |
7 ms |
384 KB |
Output is correct |
18 |
Correct |
7 ms |
384 KB |
Output is correct |
19 |
Correct |
7 ms |
384 KB |
Output is correct |
20 |
Correct |
6 ms |
384 KB |
Output is correct |
21 |
Correct |
7 ms |
384 KB |
Output is correct |
22 |
Correct |
7 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
256 KB |
Output is correct |
24 |
Correct |
6 ms |
384 KB |
Output is correct |
25 |
Correct |
6 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
6 ms |
384 KB |
Output is correct |
4 |
Correct |
6 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
384 KB |
Output is correct |
12 |
Correct |
6 ms |
384 KB |
Output is correct |
13 |
Correct |
6 ms |
384 KB |
Output is correct |
14 |
Correct |
6 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
6 ms |
384 KB |
Output is correct |
18 |
Correct |
6 ms |
384 KB |
Output is correct |
19 |
Correct |
6 ms |
384 KB |
Output is correct |
20 |
Correct |
7 ms |
384 KB |
Output is correct |
21 |
Correct |
6 ms |
384 KB |
Output is correct |
22 |
Correct |
6 ms |
384 KB |
Output is correct |
23 |
Correct |
5 ms |
384 KB |
Output is correct |
24 |
Correct |
7 ms |
384 KB |
Output is correct |
25 |
Correct |
7 ms |
384 KB |
Output is correct |
26 |
Correct |
6 ms |
384 KB |
Output is correct |
27 |
Correct |
5 ms |
256 KB |
Output is correct |
28 |
Correct |
6 ms |
384 KB |
Output is correct |
29 |
Correct |
7 ms |
384 KB |
Output is correct |
30 |
Correct |
6 ms |
384 KB |
Output is correct |
31 |
Correct |
8 ms |
384 KB |
Output is correct |
32 |
Correct |
7 ms |
384 KB |
Output is correct |
33 |
Correct |
7 ms |
384 KB |
Output is correct |
34 |
Correct |
7 ms |
384 KB |
Output is correct |
35 |
Correct |
6 ms |
384 KB |
Output is correct |
36 |
Correct |
7 ms |
384 KB |
Output is correct |
37 |
Correct |
7 ms |
384 KB |
Output is correct |
38 |
Correct |
5 ms |
256 KB |
Output is correct |
39 |
Correct |
6 ms |
384 KB |
Output is correct |
40 |
Correct |
6 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
42 |
Correct |
6 ms |
384 KB |
Output is correct |
43 |
Correct |
157 ms |
7416 KB |
Output is correct |
44 |
Correct |
943 ms |
43000 KB |
Output is correct |
45 |
Correct |
588 ms |
17912 KB |
Output is correct |
46 |
Correct |
84 ms |
2044 KB |
Output is correct |
47 |
Correct |
695 ms |
26488 KB |
Output is correct |
48 |
Correct |
670 ms |
62200 KB |
Output is correct |
49 |
Correct |
202 ms |
15992 KB |
Output is correct |
50 |
Correct |
1072 ms |
81272 KB |
Output is correct |
51 |
Correct |
506 ms |
30968 KB |
Output is correct |
52 |
Correct |
1068 ms |
71804 KB |
Output is correct |
53 |
Correct |
854 ms |
62584 KB |
Output is correct |
54 |
Correct |
6 ms |
384 KB |
Output is correct |
55 |
Correct |
268 ms |
32376 KB |
Output is correct |
56 |
Correct |
652 ms |
43732 KB |
Output is correct |
57 |
Correct |
515 ms |
36344 KB |
Output is correct |
58 |
Correct |
758 ms |
61088 KB |
Output is correct |
59 |
Correct |
581 ms |
36472 KB |
Output is correct |
60 |
Correct |
1906 ms |
94308 KB |
Output is correct |
61 |
Correct |
1924 ms |
116216 KB |
Output is correct |
62 |
Correct |
1998 ms |
115672 KB |
Output is correct |
63 |
Correct |
1968 ms |
116420 KB |
Output is correct |
64 |
Correct |
1911 ms |
116400 KB |
Output is correct |
65 |
Correct |
1366 ms |
102904 KB |
Output is correct |
66 |
Correct |
1355 ms |
103160 KB |
Output is correct |
67 |
Correct |
1274 ms |
102976 KB |
Output is correct |
68 |
Correct |
658 ms |
49196 KB |
Output is correct |
69 |
Correct |
767 ms |
49144 KB |
Output is correct |
70 |
Correct |
743 ms |
59512 KB |
Output is correct |
71 |
Correct |
1142 ms |
75000 KB |
Output is correct |