Submission #1125661

#TimeUsernameProblemLanguageResultExecution timeMemory
1125661OI_AccountNaan (JOI19_naan)C++20
0 / 100
1 ms584 KiB
#include <bits/stdc++.h>
using namespace std;

#define big __int128

typedef long long ll;

const int N = 2000;
const ll INF = 1'000'000'000;

ll gcd(ll a, ll b) {
    return b? gcd(b, a % b): a;
}

int n, l, mark[N + 10];
ll a[N + 10][N + 10];
ll sum[N + 10][N + 10];
vector<int> ans;

void readInput() {
    cin >> n >> l;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= l; j++) {
            cin >> a[i][j];
            sum[i][j] = sum[i][j - 1] + a[i][j];
        }
}

struct kasr {
    ll a, b;
    void cmp() {
        ll g = gcd(a, b);
        a /= g;
        b /= g;
        if (b > INF) {
            big res = (big) ((big) a * (big) INF + (big) (b - 1)) / (big) b;
            a = res;
            b = INF;
        }
    }
};

kasr pnt;
kasr mKasr(ll x, ll y) {
    kasr res;
    res.a = x;
    res.b = y;
    res.cmp();
    return res;
}

kasr mKasr(ll x) {
    kasr res;
    res.a = x;
    res.b = 1;
    res.cmp();
    return res;
}  

ll getVal(kasr x) {
    return x.a / x.b + 1;
}

kasr operator+(const kasr &x, const kasr &y) {
    return mKasr(x.a * y.b + y.a * x.b, x.b * y.b);
}

kasr operator*(const kasr &x, const kasr &y) {
    return mKasr(x.a * y.a, x.b * y.b);
}

kasr operator/(const kasr &x, const kasr &y) {
    return mKasr(x.a * y.b, x.b * y.a);
}

kasr operator-(const kasr &x, const kasr &y) {
    return mKasr(x.a * y.b - y.a * x.b, x.b * y.b);
}

bool operator<(const kasr &x, const kasr &y) {
    return x.a * y.b < y.a * x.b;
}

vector<kasr> res;

kasr calcSum(int idx, int mid) {
    int x = getVal(pnt);
    kasr here = mKasr(a[idx][x]) * (mKasr(mid) - pnt);
    return mKasr(sum[idx][mid] - sum[idx][x]) + here;
}

kasr calcNext(int idx) {
    kasr need = mKasr(sum[idx][l]) / mKasr(n);
    int l = getVal(pnt) - 1, r = l + 1;
    while (r - l > 1) {
        int mid = (l + r) >> 1;
        if (calcSum(idx, mid) < need)
            l = mid;
        else
            r = mid;
    }
    int x = getVal(pnt);
    kasr remain = (r == x? need: need - calcSum(idx, r - 1));
    kasr last = (r == x? pnt: mKasr(r - 1));
    return last + remain / mKasr(a[idx][r]);
}

void add() {
    int idx = 0;
    kasr newPnt;
    for (int i = 1; i <= n; i++)
        if (!mark[i]) {
            kasr tmp = calcNext(i);
            if (idx == 0 || tmp < newPnt) {
                idx = i;
                newPnt = tmp;
            }
        }
    mark[idx] = true;
    ans.push_back(idx);
    res.push_back(newPnt);
    pnt = newPnt;
}

void solve() {
    pnt = mKasr(0, 1);
    for (int i = 1; i <= n; i++)
        add();
}

void writeOutput() {
    for (int i = 0; i < n - 1; i++)
        cout << res[i].a << ' ' << res[i].b << '\n';
    for (auto id: ans)
        cout << id << ' ';
    cout.flush();
}

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    readInput();
    solve();
    writeOutput();
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...