Cod sursa(job #2765738)

Utilizator DragosC1Dragos DragosC1 Data 29 iulie 2021 18:33:45
Problema Lapte Scor 90
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.64 kb
#include <fstream>
#include <algorithm>
using namespace std;
 
int n, L;
pair<pair<int, int>, int> lapte[1001];
 
void read() {
    int i;
    ifstream f("lapte.in");
    f >> n >> L;
    for (i = 1; i <= n; i++) {
        f >> lapte[i].first.first >> lapte[i].first.second;
        lapte[i].second = i;
    }
    f.close();
}
 
bool csort(pair<pair<int, int>, int> a, pair<pair<int, int>, int> b) {
    if (a.first.second > b.first.second)
        return 1;
    else if (a.first.second == b.first.second && a.first.first < b.first.first)
        return 1;
    return 0;
}
 
int rez;
 
pair<int, int> sol[1001];
 
bool ok(int x) {
    int i, S = L, aux, S2 = 0;
    for (i = 1; i <= n; i++) {
        aux = min(S, x / lapte[i].first.first);
        S -= aux;
        sol[i].first = aux;
        sol[i].second = (x - aux * lapte[i].first.first) / lapte[i].first.second;
        S2 += sol[i].second;
    }
    if (S == 0 && S2 >= L)
        return 1;
    return 0;
}
 
void solve() {
    sort(lapte + 1, lapte + n + 1, csort);
    int st, dr, mij;
    st = 1, dr = 1000;
    while (st <= dr) {
        mij = (st + dr) / 2;
        if (ok(mij))
            dr = mij - 1;
        else st = mij + 1;
    }
    ok(st);
    rez = st;
}
 
void output() {
    int i, j;
    ofstream g("lapte.out");
    g << rez << '\n';
    for (i = 1; i <= n; i++)
        for (j = 1; j <= n; j++)
            if (lapte[j].second == i) {
                g << sol[j].first << ' ' << sol[j].second << '\n';
                break;
            }
    g.close();
}
 
int main() {
    read();
    solve();
    output();
    return 0;
}