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;
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define bas(x) #x << ": " << x
#define prarr(x, n) cout << #x << ": "; for (int qsd = 0; qsd < n; qsd++) cout << x[qsd] << " "; cout << endl;
#define prarrv(x) cout << #x << ": "; for (int qsd = 0; qsd < (int)x.size(); qsd++) cout << x[qsd] << " "; cout << endl;
#define inside sl<=l%&&r<=sr
#define outside sr<l||r<sl
typedef long long ll;
int n, m;
ll mat[2002][2002];
ll presum[2002][2002];
pair<ll, ll> sum(pair<ll, ll> a, pair<ll, ll> b){
ll pay = a.first*b.second + a.second*b.first;
ll payda = a.second*b.second;
ll gcd = __gcd(pay, payda);
pay /= gcd;
payda /= gcd;
/*while (payda > 1000000000){
pay++;
gcd = __gcd(pay, payda);
pay /= gcd;
payda /= gcd;
}*/
return mp(pay, payda);
}
pair<ll, ll> get(int i, ll pay, ll payda){
//cout << "get(" << i << ", " << pay << ", " << payda << ")" << endl;
/*double ans = presum[i][(int)(pay/payda)-1];
ans += mat[i][(int)(pay/payda)]*((pay/payda) - (int)(pay/payda));
return ans;*/
return sum(mp(presum[i][(pay/payda)-1], 1), mp((pay%payda)*mat[i][pay/payda], payda));
}
pair<ll, ll> getmin(int i, pair<ll, ll> puan){
//cout << "getmin(" << i << ", " << puan.first << ", " << puan.second << ")" << endl;
if (presum[i][m-1] < (double)puan.first/(double)puan.second) return mp(-1, -1);
int l = -1;
int r = m-1;
while (l < r){
int m = (l+r+1)/2;
if (presum[i][m] > (double)puan.first/(double)puan.second) r = m-1;
else l = m;
}
//cout << bas(l) << endl;
if (l != -1 && presum[i][l] == (double)puan.first/(double)puan.second) return mp(l+1, 1);
puan = sum(puan, mp((l == -1) ? 0 : -presum[i][l], 1));
// mat[i][l+1]*oran = puan;
return sum(mp(l+1, 1), mp(puan.first, puan.second*mat[i][l+1]));
}
int main(){
cin.tie(0); ios::sync_with_stdio(false);
cin >> n >> m;
for (int i = 0; i < n; i++){
ll cur = 0;
for (int j = 0; j < m; j++){
cin >> mat[i][j];
cur += mat[i][j];
presum[i][j] = cur;
}
}
/*for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cout << mat[i][j] << " ";
}
cout << endl;
}
cout << endl;
for (int i = 0; i < n; i++){
for (int j = 0; j < m; j++){
cout << presum[i][j] << " ";
}
cout << endl;
}
cout << endl;*/
vector<pair<ll, ll> > ans;
vector<int> p;
bitset<2002> vis;
vis.reset();
ll cpay = 0;
ll cpayda = 1;
for (int bol = 0; bol < n; bol++){
//cout << bas(bol) << endl;
ll minpay = 10000000;
ll minpayda = 1;
int ci = 0;
for (int i = 0; i < n; i++){
if (vis[i]) continue;
//cout << bas(i) << endl;
pair<ll, ll> suan = get(i, cpay, cpayda);
//cout << "suan: " << suan.first << ", " << suan.second << endl;
pair<ll, ll> minoran = getmin(i, sum(suan, mp(presum[i][m-1], n)));
//cout << bas(minoran.first) << ", " << bas(minoran.second) << endl;
if (minoran.first == -1){
cout << -1 << endl;
exit(0);
}
if ((double)minpay/(double)minpayda >(double) minoran.first/(double)minoran.second){
minpay = minoran.first;
minpayda = minoran.second;
ci = i;
}
}
cpay = minpay;
cpayda = minpayda;
ans.pb(mp(cpay, cpayda));
p.pb(ci+1);
vis[ci] = 1;
//cout << cpay << " " << cpayda << " -> " << ci << endl;
}
for (int i = 0; i < n-1; i++) cout << ans[i].first << " " << ans[i].second << endl;
for (int i = 0; i < n; i++) cout << p[i] << " ";
cout << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |