Cod sursa(job #1559312)

Utilizator bciobanuBogdan Ciobanu bciobanu Data 30 decembrie 2015 16:18:47
Problema Lapte Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.8 kb
#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 100;

int T[MAX_N + 1][2];

int D[MAX_N + 1][MAX_N + 1];
int B[MAX_N + 1][MAX_N + 1];
int A[MAX_N + 1][MAX_N + 1];

bool canPartition(const int &N, const int &L, const int &maxTime)
{
    D[0][0] = 0;
    for ( int j = 1; j <= L; ++j )
        D[0][j] = -100000;
    for ( int i = 1; i <= N; ++i )
    {
       for ( int j = 1; j <= L; ++j )
            D[i][j] = -100000;

        int maxTake = maxTime / T[i][0];

        for ( int p = 1; p <= maxTake; ++p )
        {
            int takeA = p * T[i][0];
            int takeB = ( maxTime - takeA ) / T[i][1];
            for ( int j = L; j >= p; --j )
            {
                if ( D[i][j] < D[i - 1][j - p] + takeB )
                {
                    D[i][j] = D[i - 1][j - p] + takeB;
                    A[i][j] = j - p;
                    B[i][j] = takeB;
                }
            }
        }
    }
    return D[N][L] >= L;
}

void printSolution( const int N, const int L, ofstream &out, const int &maxTime )
{
    if (N != 0)
    {
        printSolution( N - 1, A[N][L], out, maxTime );
        out << ( maxTime - B[N][L] * T[N][1] ) / T[N][0] << ' ' << B[N][L] << '\n';
    }
}

int main()
{
    ifstream in("lapte.in");
    ofstream out("lapte.out");
    in.tie(0);
    ios_base::sync_with_stdio(0);
    int N, L;

    in >> N >> L;
    for ( int i = 1; i <= N; ++i )
        in >> T[i][0] >> T[i][1];

    int lo = 0, hi = MAX_N + 1;
    while ( hi - lo > 1 )
    {
        int S = ( lo + hi ) / 2;

        if ( canPartition( N, L, S ) )
            hi = S;
        else
            lo = S;
    }

    out << hi << '\n';
    canPartition( N, L, hi );
    printSolution( N, L, out, hi );
    out.close();
    return 0;
}