#include <bits/stdc++.h>
struct Frac {
__int128_t n, d;
Frac(__int128_t _n, __int128_t _d) {
n = _n, d = _d;
__int128_t g = std::__gcd(n, d); n /= g, d /= g;
if (d < 0) n *= -1, d *= -1;
}
Frac(__int128_t _n) : Frac(_n, 1) {}
Frac() : Frac(0) {}
friend Frac abs(Frac F) { return Frac(abs(F.n), F.d); }
friend bool operator < (const Frac& l, const Frac& r) { return l.n * r.d < r.n * l.d; }
friend bool operator <= (const Frac& l, const Frac& r) { return l.n * r.d <= r.n * l.d; }
friend bool operator > (const Frac& l, const Frac& r) { return l.n * r.d > r.n * l.d; }
friend bool operator >= (const Frac& l, const Frac& r) { return l.n * r.d >= r.n * l.d; }
friend bool operator == (const Frac& l, const Frac& r) { return l.n == r.n && l.d == r.d; }
friend bool operator != (const Frac& l, const Frac& r) { return !(l == r); }
Frac operator - () const { return Frac(-n, d); }
friend Frac operator + (const Frac& l, const Frac& r) { return Frac(l.n * r.d + r.n * l.d, l.d * r.d); }
friend Frac operator - (const Frac& l, const Frac& r) { return Frac(l.n * r.d - r.n * l.d, l.d * r.d); }
friend Frac operator * (const Frac& l, const Frac& r) { return Frac(l.n * r.n, l.d * r.d); }
friend Frac operator * (const Frac& l, int r) { return l * Frac(r, 1); }
friend Frac operator * (int r, const Frac& l) { return l * r; }
friend Frac operator / (const Frac& l, const Frac& r) { return l * Frac(r.d, r.n); }
friend Frac operator / (const Frac& l, const int& r) { return l / Frac(r, 1); }
friend Frac operator / (const int& l, const Frac& r) { return Frac(l, 1) / r; }
friend Frac& operator += (Frac& l, const Frac& r) { return l = l + r; }
friend Frac& operator -= (Frac& l, const Frac& r) { return l = l - r; }
template <class T> friend Frac& operator *= (Frac& l, const T& r) { return l = l * r; }
template <class T> friend Frac& operator /= (Frac& l, const T& r) { return l = l / r; }
};
int main() {
using namespace std;
typedef __int128_t ll;
ios_base::sync_with_stdio(0);
int N, L;
cin >> N >> L;
vector<vector<ll>> V(N, vector<ll>(L));
for (int i = 0; i < N; i++)
for (int j = 0; j < L; j++) {
long long x; cin >> x;
V[i][j] = x;
}
vector<vector<Frac>> split(N, vector<Frac>(N + 1));
for (int i = 0; i < N; i++) {
Frac need = 0;
for (int j = 0; j < L; j++)
need += V[i][j];
need /= N;
split[i][0] = 0;
Frac have = 0;
int it = 0;
Frac at = 0;
for (int j = 1; j <= N; j++) {
while (have < need) {
Frac add = (1 - at) * V[i][it];
if (have + add <= need) {
have += add;
at = 0;
it++;
} else {
Frac go = (need - have) / V[i][it];
have = need;
at += go;
}
}
split[i][j] = it + at;
have = 0;
}
}
const int INF = 1e9;
vector<Frac> splits(N);
vector<int> ord(N);
vector<bool> used(N);
for (int i = 1; i <= N - 1; i++) {
pair<Frac, int> best = {INF, INF};
for (int j = 0; j < N; j++)
if (!used[j])
best = min(best, {split[j][i], j});
splits[i - 1] = best.first;
ord[i - 1] = best.second;
used[best.second] = true;
}
for (int i = 0; i < N; i++) {
if (!used[i])
ord.back() = i;
}
for (int i = 1; i < N - 1; i++)
splits[i] = max(splits[i], splits[i - 1]);
for (int i = 0; i < N - 1; i++) {
assert(splits[i - 1] <= splits[i]);
}
for (int i = 0; i < N - 1; i++)
cout << (long long) splits[i].n << " " << (long long) splits[i].d << '\n';
for (int i = 0; i < N; i++)
cout << ord[i] + 1 << " ";
cout << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
2 ms |
492 KB |
Output is correct |
4 |
Correct |
3 ms |
492 KB |
Output is correct |
5 |
Correct |
2 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
3 ms |
492 KB |
Output is correct |
10 |
Correct |
3 ms |
492 KB |
Output is correct |
11 |
Correct |
2 ms |
492 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
2 ms |
492 KB |
Output is correct |
14 |
Correct |
2 ms |
492 KB |
Output is correct |
15 |
Correct |
2 ms |
492 KB |
Output is correct |
16 |
Correct |
2 ms |
492 KB |
Output is correct |
17 |
Correct |
2 ms |
492 KB |
Output is correct |
18 |
Correct |
3 ms |
492 KB |
Output is correct |
19 |
Correct |
3 ms |
576 KB |
Output is correct |
20 |
Correct |
2 ms |
492 KB |
Output is correct |
21 |
Correct |
3 ms |
492 KB |
Output is correct |
22 |
Correct |
3 ms |
492 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
2 ms |
492 KB |
Output is correct |
25 |
Correct |
2 ms |
492 KB |
Output is correct |
26 |
Correct |
1 ms |
364 KB |
Output is correct |
27 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
2 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
2 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
512 KB |
Output is correct |
18 |
Correct |
2 ms |
492 KB |
Output is correct |
19 |
Correct |
3 ms |
492 KB |
Output is correct |
20 |
Correct |
2 ms |
492 KB |
Output is correct |
21 |
Correct |
1 ms |
364 KB |
Output is correct |
22 |
Correct |
2 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
3 ms |
492 KB |
Output is correct |
25 |
Correct |
3 ms |
492 KB |
Output is correct |
26 |
Correct |
2 ms |
492 KB |
Output is correct |
27 |
Correct |
1 ms |
364 KB |
Output is correct |
28 |
Correct |
2 ms |
492 KB |
Output is correct |
29 |
Correct |
2 ms |
492 KB |
Output is correct |
30 |
Correct |
2 ms |
492 KB |
Output is correct |
31 |
Correct |
2 ms |
492 KB |
Output is correct |
32 |
Correct |
2 ms |
492 KB |
Output is correct |
33 |
Correct |
3 ms |
492 KB |
Output is correct |
34 |
Correct |
3 ms |
576 KB |
Output is correct |
35 |
Correct |
2 ms |
492 KB |
Output is correct |
36 |
Correct |
3 ms |
492 KB |
Output is correct |
37 |
Correct |
3 ms |
492 KB |
Output is correct |
38 |
Correct |
1 ms |
364 KB |
Output is correct |
39 |
Correct |
2 ms |
492 KB |
Output is correct |
40 |
Correct |
2 ms |
492 KB |
Output is correct |
41 |
Correct |
1 ms |
364 KB |
Output is correct |
42 |
Correct |
2 ms |
492 KB |
Output is correct |
43 |
Correct |
306 ms |
14572 KB |
Output is correct |
44 |
Correct |
1948 ms |
85912 KB |
Output is correct |
45 |
Correct |
684 ms |
35564 KB |
Output is correct |
46 |
Correct |
52 ms |
3948 KB |
Output is correct |
47 |
Correct |
1247 ms |
52972 KB |
Output is correct |
48 |
Correct |
2871 ms |
124068 KB |
Output is correct |
49 |
Correct |
591 ms |
31852 KB |
Output is correct |
50 |
Correct |
3433 ms |
162276 KB |
Output is correct |
51 |
Correct |
1239 ms |
61548 KB |
Output is correct |
52 |
Correct |
3134 ms |
143468 KB |
Output is correct |
53 |
Correct |
2624 ms |
125080 KB |
Output is correct |
54 |
Correct |
2 ms |
492 KB |
Output is correct |
55 |
Correct |
1311 ms |
64688 KB |
Output is correct |
56 |
Correct |
1714 ms |
87276 KB |
Output is correct |
57 |
Correct |
1316 ms |
72556 KB |
Output is correct |
58 |
Correct |
2365 ms |
122220 KB |
Output is correct |
59 |
Correct |
1372 ms |
72864 KB |
Output is correct |
60 |
Correct |
3203 ms |
188524 KB |
Output is correct |
61 |
Correct |
3317 ms |
188396 KB |
Output is correct |
62 |
Correct |
3911 ms |
209284 KB |
Output is correct |
63 |
Correct |
3724 ms |
210768 KB |
Output is correct |
64 |
Correct |
3353 ms |
210620 KB |
Output is correct |
65 |
Correct |
3879 ms |
197228 KB |
Output is correct |
66 |
Correct |
3697 ms |
197256 KB |
Output is correct |
67 |
Correct |
3230 ms |
197228 KB |
Output is correct |
68 |
Correct |
1553 ms |
92116 KB |
Output is correct |
69 |
Correct |
1429 ms |
90268 KB |
Output is correct |
70 |
Correct |
1926 ms |
112504 KB |
Output is correct |
71 |
Correct |
2276 ms |
138968 KB |
Output is correct |