This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
using nagai = __int128;
using ll=long long;
#define sz(x) int((x).size())
const int N=2020;
int n,m;
nagai matr[N][N];
nagai pref[N][N];
struct frac{
nagai a,b;
frac(){}
frac(nagai a,nagai b):a(a),b(b){
}
frac(nagai x):a(x),b(1){}
frac operator *(frac o){
return frac(a*o.a,b*o.b);
}
frac operator +(frac o){
return frac(a*o.b+o.a*b,b*o.b);
}
frac operator -(frac o){
return frac(a*o.b-o.a*b,b*o.b);
}
bool operator <=(frac o){
return a*o.b<=b*o.a;
}
bool operator <(frac o){
return a*o.b<b*o.a;
}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m;
for(int i=0;i<n;++i)
for(int j=0;j<m;++j){
ll x;
cin>>x;
matr[i][j]=x;
pref[i][j+1]=pref[i][j]+matr[i][j];
}
frac cur(0);
vector<frac>fr;
vector<int>ord;
vector<bool>used(n);
vector<int>ptr(n);
for(int i=0;i<n;++i){
frac mn(1e9);
int mni=-1;
for(int j=0;j<n;++j){
if(used[j])continue;
while(ptr[j]<m&&frac(pref[j][ptr[j]])<=frac(pref[j][m])*frac(i+1,n))
++ptr[j];
int L=ptr[j]-1;
frac rest(0);
rest=frac(pref[j][m])*frac(i+1,n)-frac(pref[j][L]);
if(rest.a)
rest=rest*frac(1,matr[j][L]);
rest=rest+frac(L);
if(rest<mn)
mn=rest,mni=j;
}
cur=mn;
fr.push_back(cur);
ord.push_back(mni);
used[mni]=true;
}
for(int i=0;i<n-1;++i){
cout<<(ll)fr[i].a<<' '<<(ll)fr[i].b<<'\n';
}
for(int i=0;i<n;++i)
cout<<ord[i]+1<<' ';
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... |