#include <bits/stdc++.h>
#define int long long
#define __int128_t ll
using namespace std;
typedef array<int, 2> fr;
const int MAX = 2001;
const int INF = 0x3f3f3f3f3f3f3f3f;
int V[MAX][MAX], S[MAX];
fr D[MAX][MAX];
bool chk[MAX];
int gcd(int X, int Y) { return Y ? gcd(Y, X % Y) : X; }
fr add(fr A, fr B) {
int X = gcd(A[1], B[1]);
return {A[0] * B[1] / X + B[0] * A[1] / X, A[1] * B[1] / X};
}
bool comp(fr A, fr B) { return A[0] * B[1] <= A[1] * B[0]; }
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int N, L, S, X, Y;
fr K;
vector<int> P;
cin >> N >> L;
for (int i = 1; i <= N; i++) {
S = X = Y = 0;
for (int j = 1; j <= L; j++)
cin >> V[i][j], S += V[i][j];
for (int j = 1; j <= N - 1; j++) {
while (S * j >= N * (Y + V[i][X + 1]))
Y += V[i][++X];
D[i][j] = {S * j - N * Y + X * N * V[i][X + 1], N * V[i][X + 1]};
cout << i << ' ' << j << ' ' << D[i][j][0] << ' ' << D[i][j][1] << '\n';
}
D[i][N] = {0, 0};
}
for (int i = 1; i <= N; i++) {
K = {MAX, 1}, X = -1;
for (int j = 1; j <= N; j++) {
if (chk[j])
continue;
if (comp(D[j][i], K))
K = D[j][i], X = j;
}
if (i < N)
cout << K[0] << ' ' << K[1] << '\n';
P.push_back(X), Y = K[1], chk[X] = true;
}
for (int i : P)
cout << i << ' ';
cout << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |