Cod sursa(job #1781522)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 16 octombrie 2016 22:37:45
Problema Lapte Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.25 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("lapte.in");
ofstream fout("lapte.out");

const int NMax = 105;

pair < int, int > A[NMax], B[NMax], fi[NMax];
int C[NMax], D[NMax];
int aux[NMax];
int timeUsed[NMax];

inline bool cmp(const pair < int ,int > &a, const pair < int, int > &b){

    if(a.first != b.first) return a.first < b.first;
    return B[a.second] > B[b.second];

}

inline void compute(int &a, pair < int, int > v[], const int &n, const int &m, const int &up){

    for(int i = 1; i <= n; i++) {

        if(a >= m) return;

        int time = up - timeUsed[v[i].second];
        int need = m - a;

        if(need <= time / v[i].first){

            a += need;
            timeUsed[v[i].second] += need * v[i].first;

        } else {

            a += time / v[i].first;
            timeUsed[v[i].second] += (time / v[i].first) * v[i].first;

        }

    }

}

inline bool good(const int &n, const int &m, const int &up){

    int a, b;
    a = b = 0;
    for(int i = 1; i <= n; i++) timeUsed[i] = 0;

    compute(a, A, n, m, up);
    compute(b, B, n, m, up);

    if(a >= m && b >= m) return true;
    return false;

}

int main(){

    ios::sync_with_stdio();
    fin.tie(NULL);

    int n, m;
    fin >> n >> m;

    for(int i = 1; i <= n; i++){

        int a, b;
        fin >> a >> b;

        fi[i] = {a, b};
        A[i] = {a, i};
        B[i] = {b, i};
    }

    sort(A + 1, A + n + 1, cmp);
    sort(B + 1, B + n + 1);

    int lo = 1;
    int hi = NMax * NMax;
    int mid, ans;
    while(lo <= hi){

        mid = (lo + hi) / 2;
        if(good(n, m, mid) == true){

            ans = mid;
            hi = mid - 1;

        } else {

            lo = mid + 1;

        }
    }

    fout << ans << "\n";

    lo = 0;
    for(int i = 1; i <= n; i++) timeUsed[i] = 0;
    compute(lo, A, n, m, ans);
    for(int i = 1; i <= n; i++) C[i] = timeUsed[i] / fi[i].first;
    for(int i = 1; i <= n; i++) aux[i] = timeUsed[i];
    lo = 0;
    compute(lo, B, n, m, ans);
    for(int i = 1; i <= n; i++) D[i] = (timeUsed[i] - aux[i]) / fi[i].second;
    for(int i = 1; i <= n; i++){

        fout << C[i] << " " << D[i] << "\n";

    }
    return 0;
}