#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
struct Fraction {
long long a = 0, b = 1;
bool operator<(const Fraction &other) const & {
return a * other.b < other.a * b;
}
bool operator<=(const Fraction &other) const & {
return !(other < *this);
}
Fraction operator*(const Fraction &other) const & {
long long A = a * other.a;
long long B = b * other.b;
long long G = __gcd(A, B);
return {A / G, B / G};
}
Fraction operator+(const Fraction &other) const & {
long long B = b * other.b;
long long A = a * other.b + other.a * b;
long long G = abs(__gcd(A, B));
return {A / G, B / G};
}
Fraction operator-(Fraction other) {
other.a *= -1;
return (*this) + other;
}
};
int n, m;
cin >> n >> m;
vector<vector<int>> v(n, vector<int>(m));
vector<vector<int>> f(n, vector<int>(m + 1, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int x;
cin >> x;
v[i][j] = x;
f[i][j + 1] = f[i][j] + x;
}
}
vector<Fraction> key;
vector<int> perm;
vector<bool> chosen(n, false);
Fraction cur = {0, 1};
vector<int> ptr(n, 0); // possible cut for i is in [ptr[i], ptr[i] + 1)
for (int t = 0; t < n - 1; t++) {
vector<pair<Fraction, int>> get = [&]() -> vector<pair<Fraction, int>> {
vector<pair<Fraction, int>> res;
for (int i = 0; i < n; i++) {
if (chosen[i]) continue;
Fraction nxt = [&]() -> Fraction {
auto F = [&](Fraction x) -> Fraction {
int j = x.a / x.b;
if (j < m) return Fraction{f[i][j], 1} + Fraction{v[i][j], 1} * Fraction{x.a % x.b, x.b};
return Fraction{f[i][m], 1};
};
Fraction req = F(Fraction{m, 1}) * Fraction{1, n};
while (true) {
if (F(Fraction{ptr[i] + 1, 1}) - F(cur) <= req) { // then is not enough
ptr[i]++;
continue;
}
Fraction rem = req + F(cur) - F(Fraction{ptr[i], 1});
return Fraction{ptr[i], 1} + rem * Fraction{1, v[i][ptr[i]]};
}
}();
res.push_back({nxt, i});
}
return res;
}();
auto [nxt, i] = *min_element(get.begin(), get.end());
key.push_back(nxt);
perm.push_back(i);
cur = nxt;
chosen[i] = true;
}
for (int i = 0; i < n; i++) {
if (!chosen[i]) perm.push_back(i);
}
for (Fraction frac : key) {
cout << frac.a << ' ' << frac.b << '\n';
}
for (int i : perm) {
cout << i + 1 << ' ';
}
cout << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
360 KB |
Output is correct |
4 |
Correct |
0 ms |
532 KB |
Output is correct |
5 |
Correct |
0 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
360 KB |
Output is correct |
7 |
Correct |
0 ms |
360 KB |
Output is correct |
8 |
Correct |
1 ms |
360 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
588 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
360 KB |
Output is correct |
3 |
Correct |
1 ms |
360 KB |
Output is correct |
4 |
Correct |
0 ms |
532 KB |
Output is correct |
5 |
Correct |
0 ms |
360 KB |
Output is correct |
6 |
Correct |
0 ms |
360 KB |
Output is correct |
7 |
Correct |
0 ms |
360 KB |
Output is correct |
8 |
Correct |
1 ms |
360 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
588 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
600 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Execution timed out |
4011 ms |
3420 KB |
Time limit exceeded |
44 |
Halted |
0 ms |
0 KB |
- |