Pagini recente » Cod sursa (job #2482977) | Cod sursa (job #1263320) | Cod sursa (job #2534762) | Cod sursa (job #433977) | Cod sursa (job #1559317)
#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 )
{
D[i][0] = 0;
for ( int j = 1; j <= L; ++j )
D[i][j] = -100000;
int maxTake = maxTime / T[i][0];
for ( int p = 0; p <= maxTake; ++p )
{
int takeB = ( maxTime - p * T[i][0] ) / 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;
}