Submission #699126

#TimeUsernameProblemLanguageResultExecution timeMemory
699126dooweyNaan (JOI19_naan)C++14
5 / 100
2 ms340 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = 2010;
ll V[N][N];

struct Frac{
    ll A;
    ll B;
}; // A / B

Frac add(Frac ai, Frac bi){
    return {(ai.A * 1ll * bi.B + bi.A * 1ll * ai.B), (ai.B * 1ll * bi.B)};
}

bool comp(Frac aa, Frac bb){
    bb.A = -bb.A;
    aa = add(aa, bb);
    if(aa.A >= 0){
        return true;
    }
    else{
        return false;
    }
}

int pip[N];
Frac need[N];
int ans[N];

int main(){
    fastIO;
    //freopen("in.txt", "r", stdin);
    int n, m;
    cin >> n >> m;
    for(int i = 1; i <= n; i ++ ){
        need[i] = {0, n};
        for(int j = 0; j < m; j ++ ){
            cin >> V[i][j];
            need[i].A += V[i][j];
        }
        pip[i] = -1;
    }
    Frac las = {0ll, 1ll};
    Frac cum;
    Frac en;
    int id;
    int gi;
    ll C;
    ll X;
    Frac ee;
    vector<int> ord;
    for(int pos = 1; pos < n; pos ++ ){
        Frac low = {m,1};
        gi = -1;
        for(int chk = 1; chk <= n; chk ++ ){
            if(pip[chk] == -1){
                cum = {0ll, 1ll};
                id = las.A / las.B;
                cum = add({id+1, 1}, {-las.A, las.B});
                cum.A *= V[chk][id];
                while(id < m){
                    if(comp(cum, need[chk])){
                        break;
                    }
                    id ++ ;
                    cum = add(cum, {V[chk][id], 1});
                }
                C = n * V[chk][id];
                en = add(cum, {-need[chk].A, need[chk].B});
                en.A *= C;
                en.B *= V[chk][id];
                X = en.A / en.B;
                ee = add({id + 1, 1}, {-X,C});
                if(comp(low, ee)){
                    low = ee;
                    gi = chk;
                }
            }
        }
        cout << low.A << " " << low.B << "\n";
        las = low;
        pip[gi] = pos;
        ans[gi] = pos;
    }
    for(int i = 1; i <= n; i ++ ){
        if(pip[i] == -1) ans[i] = n;
    }
    for(int i = 1; i <= n; i ++ ){
        cout << ans[i] << " ";
    }
    cout << "\n";
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...