Submission #1125666

#TimeUsernameProblemLanguageResultExecution timeMemory
1125666OI_AccountNaan (JOI19_naan)C++20
29 / 100
1154 ms9112 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, len, mark[N + 10]; ll a[N + 10][N + 10]; ll sum[N + 10][N + 10]; vector<int> ans; void readInput() { cin >> n >> len; for (int i = 1; i <= n; i++) for (int j = 1; j <= len; 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(llabs(a), llabs(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(x) - pnt); return mKasr(sum[idx][mid] - sum[idx][x]) + here; } kasr calcNext(int idx) { kasr need = mKasr(sum[idx][len]) / mKasr(n); int l = getVal(pnt) - 1, r = len + 1; while (r - l > 1) { int mid = (l + r) >> 1; if (calcSum(idx, mid) < need) l = mid; else r = mid; } if (r == len + 1) return mKasr(-1); int x = getVal(pnt); kasr remain = (r == x? need: need - calcSum(idx, r - 1)); kasr last = (r == x? pnt: mKasr(r - 1)); //cout << idx << ": " << r << ' ' << calcSum(idx, 1).a << ' ' << calcSum(idx, 1).b << ' ' << (calcSum(idx, 1) < need) << endl; return last + remain / mKasr(a[idx][r]); } void add() { //cout << "add " << endl; int idx = 0; kasr newPnt; for (int i = 1; i <= n; i++) if (!mark[i]) { kasr tmp = calcNext(i); if (tmp.a != -1 && (idx == 0 || tmp < newPnt)) { idx = i; newPnt = tmp; } } if (!idx) return; 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() { if (ans.size() != n) { cout << -1 << flush; return; } 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...