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;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ll INF=1e9+7;
const int LIM=2e3+7;
ll T[LIM][LIM], sum[LIM], odw[LIM], n, l;
bool mniejszy(pair<pair<ll,ll>,ll>a, pair<pair<ll,ll>,ll>b) {
ll x=a.st.st*b.st.nd, y=b.st.st*a.st.nd;
if(x==y) return a.nd<b.nd;
return x<y;
}
pair<pair<ll,ll>,ll>nxt(pair<ll,ll>x) {
pair<pair<ll,ll>,ll>ans={{INF, 1}, INF};
rep(i, n) if(!odw[i]) {
ll p=x.st/x.nd, q=x.st-p*x.nd;
ll a=(T[i][p]*q+x.nd-1)/x.nd;
ll c=T[i][p]-a;
if(c>=sum[i]) {
a+=sum[i];
pair<pair<ll,ll>,ll>t={{a+p*T[i][p], T[i][p]}, i};
if(mniejszy(t, ans)) ans=t;
continue;
}
ll d=p+1;
while(c+T[i][d]<sum[i]) {
c+=T[i][d];
++d;
}
ll e=sum[i]-c;
pair<pair<ll,ll>,ll>t={{e+d*T[i][d], T[i][d]}, i};
if(mniejszy(t, ans)) ans=t;
}
return ans;
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> l;
rep(i, n) {
rep(j, l) {
cin >> T[i][j];
sum[i]+=T[i][j];
T[i][j]*=n;
}
}
vector<ll>P;
pair<ll,ll>akt={0, 1};
rep(i, n-1) {
pair<pair<ll,ll>,ll>x=nxt(akt);
P.pb(x.nd);
odw[x.nd]=1;
akt=x.st;
cout << akt.st << " " << akt.nd << '\n';
}
rep(i, n) if(!odw[i]) P.pb(i);
for(auto i : P) cout << i+1 << " ";
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... |