제출 #524388

#제출 시각아이디문제언어결과실행 시간메모리
524388fatemetmhrNaan (JOI19_naan)C++17
29 / 100
266 ms9068 KiB
// ~Be name khoda~ // #include<bits/stdc++.h> using namespace std; typedef long long ll; #define pb push_back #define mp make_pair #define all(x) x.begin(), x.end() #define fi first #define se second #define cl clear #define endll '\n' const int maxn = 1e6 + 10; const int maxn5 = 2e3 + 10; const int maxnt = 1.2e6 + 10; const int maxn3 = 1e3 + 10; const int mod = 1e9 + 7; const ll inf = 2e18; struct kasr{ __int128 s, m; kasr(__int128 a, __int128 b){ while(a > 1e18 || b > 1e18){ a /= 2; b /= 2; } __int128 g = __gcd(a, b); s = a / g; m = b / g; } kasr(){ s = 0; m = 1; } inline ll ciel(){ __int128 f = s / m; if(s * f != m) f++; return f; } inline void recalc(){ while(m > 1e9){ s /= 2; m /= 2; } } }; inline kasr operator -(ll a, kasr b){ a *= b.m; return kasr(a - b.s, b.m); } inline kasr operator -(kasr a, kasr b){ __int128 mm = a.m * b.m / __gcd(a.m, b.m); return kasr(a.s * (mm / a.m) - b.s * (mm / b.m), mm); } inline kasr operator +(kasr a, kasr b){ __int128 mm = a.m * b.m / __gcd(a.m, b.m); return kasr(a.s * (mm / a.m) + b.s * (mm / b.m), mm); } inline bool operator <=(kasr a, kasr b){ return a.s * b.m <= b.s * a.m; } inline bool operator >=(kasr a, kasr b){ return a.s * b.m >= b.s * a.m; } inline bool operator <(kasr a, kasr b){ return a.s * b.m < b.s * a.m; } inline bool operator >(kasr a, kasr b){ return a.s * b.m > b.s * a.m; } int n, l, per[maxn5]; ll a[maxn5][maxn5], ps[maxn5][maxn5]; kasr need[maxn5]; kasr out[maxn5]; bool mark[maxn5]; inline ll get_val(int l, int r, int i){ if(l >= r) return 0; return ps[i][r] - ps[i][l]; } inline kasr get(int i, kasr last){ kasr ret (0, 1); kasr ask = need[i]; int id = last.ciel(); if(kasr(id, 1) > last){ kasr rem = id - last; kasr sup = kasr(ask.s, ask.m * a[i][id]); if(sup >= rem){ last = kasr(id, 1); ask = ask - kasr(rem.s * a[i][id], rem.m); } else{ return last + sup; } } //cout << i << ' ' << last << ' ' << id << ' ' << ask << endl; int lo = id, hi = l + 1; while(hi - lo > 1){ int mid = (lo + hi) >> 1; if(kasr(get_val(id, mid, i), 1) <= ask) lo = mid; else hi = mid; } //cout << lo << ' ' << ask << endl; last = kasr(lo, 1); ask = ask - kasr(get_val(id, lo, i), 1); kasr sup = kasr(ask.s, ask.m * a[i][lo + 1]); //cout << last << ' ' << sup << ' ' << ask << endl; return last + sup; } int main() { ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); cin >> n >> l; //cout << "rgit " << n << ' ' << l << endl; for(int i = 0; i < n; i++) for(int j = 1; j <= l; j++){ cin >> a[i][j]; //cout << "aha " << i << ' ' << j << endl; need[i].s += a[i][j]; ps[i][j] = a[i][j] + (j > 0 ? ps[i][j - 1] : 0); } //return 0; for(int i = 0; i < n; i++){ //chh[i] = need[i]; //cout << "* " << i << ' ' << need[i] << endl; need[i].m = n; //cout << need[i] << endl; } kasr last(0, 1); for(int i = 0; i < n - 1; i++){ int v = 0; kasr have(l + 5, 1); for(int j = 0; j < n; j++) if(!mark[j]){ kasr cur = get(j, last); //cout << i << ' ' << j << ' ' << cur << ' ' << last << endl; if(cur <= have){ v = j; have = cur; } } per[i] = v; last = have; out[i] = last; mark[v] = true; } for(int i = 0; i < n; i++) if(!mark[i]) per[n - 1] = i; for(int i = 0; i < n - 1; i++){ out[i].recalc(); cout << ll(out[i].s) << ' ' << ll(out[i].m) << '\n'; } for(int i = 0; i < n; i++) cout << per[i] + 1 << ' '; cout << endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...