#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Frac {
ll num = 0, den = 1;
Frac() = default;
Frac(ll a, ll b) : num(a), den(b) {}
bool operator<(const Frac &oth) const {
ll a = num / den, b = oth.num / oth.den;
return pair{a, oth.den * (num - den * a)} < pair{b, den * (oth.num - oth.den * b)};
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, L;
cin >> n >> L;
vector v(n, vector<int>(L));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < L; ++j) {
cin >> v[i][j];
}
}
vector p(n, vector<Frac>(n));
for (int i = 0; i < n; ++i) {
ll sum = accumulate(v[i].begin(), v[i].end(), 0LL);
ll pref = 0;
Frac last{};
for (int j = 0, k = 0; j < n; ++j) {
while (k < L && pref * n < sum * (j + 1)) {
pref += v[i][k++];
}
if (Frac(k, 1) < last) {
last.num += sum;
} else {
last = Frac(1LL * n * v[i][k - 1] * (k - 1) + sum * (j + 1) - n * (pref - v[i][k - 1]), n * v[i][k - 1]);
}
p[i][j] = last;
}
}
vector<bool> used(n);
vector<int> ord;
for (int j = 0; j < n; ++j) {
Frac best(1e9, 1);
int b = -1;
for (int i = 0; i < n; ++i) {
if (!used[i] && p[i][j] < best) {
best = p[i][j];
b = i;
}
}
if (j + 1 < n) {
cout << best.num << " " << best.den << '\n';
}
ord.push_back(b);
used[b] = true;
}
for (int x : ord) {
cout << x + 1 << ' ';
}
cout << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
440 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
328 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
324 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
336 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
336 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
440 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
324 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
328 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
324 KB |
Output is correct |
28 |
Correct |
1 ms |
332 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
336 KB |
Output is correct |
31 |
Correct |
1 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
336 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
1 ms |
340 KB |
Output is correct |
40 |
Correct |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
340 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
26 ms |
7876 KB |
Output is correct |
44 |
Correct |
164 ms |
46244 KB |
Output is correct |
45 |
Correct |
105 ms |
20296 KB |
Output is correct |
46 |
Correct |
16 ms |
2360 KB |
Output is correct |
47 |
Correct |
139 ms |
29132 KB |
Output is correct |
48 |
Correct |
112 ms |
60764 KB |
Output is correct |
49 |
Correct |
41 ms |
15116 KB |
Output is correct |
50 |
Correct |
204 ms |
76780 KB |
Output is correct |
51 |
Correct |
113 ms |
27948 KB |
Output is correct |
52 |
Correct |
212 ms |
66112 KB |
Output is correct |
53 |
Correct |
170 ms |
58572 KB |
Output is correct |
54 |
Correct |
1 ms |
340 KB |
Output is correct |
55 |
Correct |
44 ms |
32208 KB |
Output is correct |
56 |
Correct |
141 ms |
39496 KB |
Output is correct |
57 |
Correct |
112 ms |
33052 KB |
Output is correct |
58 |
Correct |
148 ms |
57360 KB |
Output is correct |
59 |
Correct |
134 ms |
32328 KB |
Output is correct |
60 |
Correct |
383 ms |
100580 KB |
Output is correct |
61 |
Correct |
364 ms |
100596 KB |
Output is correct |
62 |
Correct |
399 ms |
100320 KB |
Output is correct |
63 |
Correct |
372 ms |
100832 KB |
Output is correct |
64 |
Correct |
402 ms |
100908 KB |
Output is correct |
65 |
Correct |
317 ms |
87620 KB |
Output is correct |
66 |
Correct |
293 ms |
87464 KB |
Output is correct |
67 |
Correct |
293 ms |
87328 KB |
Output is correct |
68 |
Correct |
155 ms |
42772 KB |
Output is correct |
69 |
Correct |
168 ms |
40668 KB |
Output is correct |
70 |
Correct |
153 ms |
52764 KB |
Output is correct |
71 |
Correct |
247 ms |
63052 KB |
Output is correct |