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 ll = long long;
using pll = pair<ll, ll>;
#define X first
#define Y second
const pll INF = {1e9, 1};
int N, L, V[2009][2009], A[2009];
pll D[2009][2009];
bool operator <(pll P, pll Q) {
return 1LL * P.X * Q.Y < 1LL * Q.X * P.Y;
}
pll f(ll a, ll b) {
ll gcd = __gcd(a, b);
return {a / gcd, b / gcd};
}
pll R(int i, pll st) {
if(st >= INF) return INF;
ll div = st.X / st.Y, mod = st.X % st.Y, lft = st.Y - mod;
ll a, b;
// lft / st.Y * V[i][div] >= A[i] / N
if(1LL * lft * V[i][div] * N >= 1LL * A[i] * st.Y) {
// ((a / b) - (st.X / st.Y)) * V[i][div] == A[i] / N
// ((a / b) - (st.X / st.Y)) * V[i][div] * N == A[i]
// (a / b) * V[i][div] * N - (st.X / st.Y) * V[i][div] * N == A[i]
// (a / b) * V[i][div] * N * st.Y - st.X * V[i][div] * N == A[i] * st.Y
// (a / b) * V[i][div] * N * st.Y = st.X * V[i][div] * N + A[i] * st.Y
a = 1LL * st.X * V[i][div] * N + 1LL * A[i] * st.Y;
b = 1LL * V[i][div] * N * st.Y;
return f(a, b);
}
else {
ll sa, sb;
sa = 1LL * lft * V[i][div]; sb = st.Y;
tie(sa, sb) = f(sa, sb);
for(int j=div+1; j<L; j++) {
// sa / sb + V[i][j] >= A[i] / N
if(1LL * sa * N + 1LL * V[i][j] * sb * N >= 1LL * A[i] * sb) {
// (a / b) * V[i][j] + (sa / sb) == A[i] / N
// a/b * V[i][j] * sb * N + sa * N == A[i] * sb
// a/b * V[i][j] * sb * N == A[i] * sb - sa * N;
b = 1LL * V[i][j] * sb * N;
a = 1LL * b * j + 1LL * A[i] * sb - 1LL * sa * N;
return f(a, b);
}
sa += 1LL * sb * V[i][j];
tie(sa, sb) = f(sa, sb);
}
}
return INF;
}
bool chk[2009];
int main() {
scanf("%d%d",&N,&L);
for(int i=1; i<=N; i++) {
for(int j=0; j<L; j++) {
scanf("%d",&V[i][j]);
A[i] += V[i][j];
}
pll now = {0, 1};
for(int j=0; j<N; j++) {
now = R(i, now);
D[i][j] = now;
}
}
vector<pll> pos; vector<int> P;
for(int i=0; i<N; i++) {
pll mn = INF; int mni = -1;
for(int j=1; j<=N; j++) if(!chk[j]) {
if(D[j][i] < mn) {
mn = D[j][i];
mni = j;
}
}
pos.push_back(mn);
P.push_back(mni);
chk[mni] = 1;
}
for(int i=0; i<N-1; i++) printf("%lld %lld\n", pos[i].X, pos[i].Y);
for(auto& it: P) printf("%d ", it);
return 0;
}
Compilation message (stderr)
naan.cpp: In function 'int main()':
naan.cpp:60:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&N,&L);
~~~~~^~~~~~~~~~~~~~
naan.cpp:63:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&V[i][j]);
~~~~~^~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |