#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 2005, INF = 1e18;
int BIG = 1e9;
int n, m;
int a[maxn][maxn], sum[maxn][maxn], req[maxn];
bool vis[maxn];
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
BIG = BIG / n * n;
for (int i=1;i<=n;i++) {
int total = 0;
for (int j=1;j<=m;j++) {
cin >> a[i][j];
sum[i][j] = sum[i][j-1] + a[i][j];
total += a[i][j];
}
req[i] = total * BIG / n;
}
int pos = 0;
int ans[n+1];
for (int i=1;i<=n;i++) {
pair<int,int> mn = {INF, -1};
int nxt = (pos + BIG - 1)/BIG, left = nxt * BIG - pos;
for (int j=1;j<=n;j++) if (!vis[j]) {
int l = pos+1, r = BIG * m;
while (l < r) {
int mid = (l+r)/2;
int val;
if (mid/BIG != pos/BIG) val = left * a[j][pos/BIG + 1] + (sum[j][mid/BIG] - sum[j][nxt]) * BIG + (mid%BIG) * a[j][mid/BIG + 1];
else val = (mid - pos) * a[j][pos/BIG + 1];
// cout << ".\n";
// cout << j << " " << mid << " " << val << endl;
// cout << left * a[j][pos/BIG + 1] << " " << (sum[j][mid/BIG] - sum[j][nxt]) * BIG << " " << (mid%BIG) * a[j][mid/BIG + 1] << endl;
if (val >= req[j]) r = mid;
else l = mid+1;
}
// int val;
// if (l/BIG != pos/BIG) val = left * a[j][pos/BIG + 1] + (sum[j][l/BIG] - sum[j][nxt]) * BIG + (l%BIG) * a[j][l/BIG + 1];
// else val = (l - pos) * a[j][pos/BIG + 1];
// assert(val >= req[j]);
// return 0;
mn = min(make_pair(l, j), mn);
// cout << j << " " << l << endl;
}
ans[i] = mn.second;
vis[mn.second] = true;
pos = mn.first;
// cout << pos << " " << BIG << "\n";
if (i < n) cout << pos << " " << BIG << "\n";
}
for (int i=1;i<=n;i++) cout << ans[i] << " "; cout << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |