Cod sursa(job #3040160)

Utilizator AnSeDraAndrei Sebastian Dragulescu AnSeDra Data 29 martie 2023 13:52:15
Problema Lapte Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.53 kb
#include <fstream>
#include <vector>

using namespace std;

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

const int Nmax = 100;

int n, l;
pair<int, int> v[Nmax + 5], previ[Nmax + 5][Nmax + 5];
vector<pair<int, int>> dp;

bool sePoate(int mid){
    dp.clear();

    int catiA, catiB;

    dp.push_back({0, 0});

    for(int i = 1; i <= n; i++){
        int t = dp.size();

        for(int j = 0; j < t; j++){
            catiA = mid / v[i].first;
            catiB = (mid - catiA * v[i].first) / v[i].second;

            while(catiA > 0){
                if(dp[j].first + catiA >=l && dp[j].second + catiB >= l){
                    return 1;
                }

                dp.push_back({dp[j].first + catiA, dp[j].second + catiB});
                catiA--;
                catiB = (mid - catiA * v[i].first) / v[i].second;
            }
        }
    }

    return 0;
}

pair<int, int> calculMiscari(int mid){
    dp.clear();

    int catiA, catiB;

    dp.push_back({0, 0});

    for(int i = 1; i <= n; i++){
        int t = dp.size();

        for(int j = 0; j < t; j++){
            catiA = mid / v[i].first;
            catiB = (mid - catiA * v[i].first) / v[i].second;

            while(catiA > 0){
                dp.push_back({dp[j].first + catiA, dp[j].second + catiB});
                previ[dp[j].first + catiA][dp[j].second + catiB] = {dp[j].first, dp[j].second};
                if(dp[j].first + catiA >=l && dp[j].second + catiB >= l){
                    return {dp[j].first + catiA, dp[j].second + catiB};
                }
                catiA--;
                catiB = (mid - catiA * v[i].first) / v[i].second;
            }
        }
    }

    return {-1, -1};
}

void reconst(pair<int, int> val){
    if(val.first == 0 && val.second == 0){
        return;
    }

    reconst(previ[val.first][val.second]);
    fout << val.first - previ[val.first][val.second].first << " " << val.second - previ[val.first][val.second].second << '\n';
}

int main(){
    int lft, rgt, mid, sol;
    pair<int, int> last;

    fin >> n >> l;

    for(int i = 1; i <= n; i++){
        fin >> v[i].first >> v[i].second;
    }

    lft = 1;
    rgt = 100;
    sol = -1;
    while(lft <= rgt){
        mid = (lft + rgt) / 2;

        if(sePoate(mid)){
            sol = mid;
            rgt = mid - 1;
        }
        else{
            lft = mid + 1;
        }
    }

    fout << sol << '\n';

    last = calculMiscari(sol);

    reconst(last);

    return 0;
}