Pagini recente » Cod sursa (job #986924) | Cod sursa (job #2911215) | Cod sursa (job #1625721) | Cod sursa (job #2047268) | Cod sursa (job #1029277)
#include <fstream>
#include <iostream>
#include <algorithm>
using namespace std;
ifstream input("lapte.in");
ofstream output("lapte.out");
class Om
{
public:
int first , second , vect_poz;
Om(int x = 0 , int y = 0, int poz = 0)
{
first = x;
second = y;
vect_poz = poz;
}
};
int N , L , T;
Om vect[200];
bool cmp(const Om & a , const Om & b)
{
return (double(a.first)/double(a.second)) < ((double(b.first)/double(b.second)));
}
int main()
{
input >> N >> L;
for (int i = 0; i<N; i++)
{
input >> vect[i].first >> vect[i].second;
vect[i].vect_poz = i;
}
int left = 1;
int right = 100;
sort(vect , vect + N , cmp);
int min_time = 1000;
Om raspuns[200];
while (left <= right)
{
int middle = (left + right) >> 1;
Om lapte[200];
int LA = L;
int LB = L;
int i = 0;
for (i = 0; i<N; i++)
{
int lapte_auxA = middle / vect[i].first;
int lapte_auxB = middle / vect[i].second;
if (LA >= lapte_auxA)
{
LA -= lapte_auxA;
lapte[i] = Om(lapte_auxA , 0 , vect[i].vect_poz);
}
else if (LA > 0)
{
int timpA = LA * vect[i].first;
int timpB = middle - timpA;
lapte[i] = Om(LA , timpB / vect[i].second , vect[i].vect_poz);
LB -= lapte[i].second;
LA = 0;
}
else if (LB >= lapte_auxB)
{
LB -= lapte_auxB;
lapte[i] = Om(0 , lapte_auxB , vect[i].vect_poz);
}
else if (LB > 0)
{
lapte[i] = Om(0 , LB , vect[i].vect_poz);
LB = 0;
break;
}
else
{
LB = 0;
break;
}
}
if (LB == 0 && LA == 0)
{
right = middle - 1;
if (middle < min_time)
{
min_time = middle;
for (int i = 0; i<N; i++)
raspuns[lapte[i].vect_poz] = lapte[i];
}
}
else if (LB != 0 || LA != 0)
{
left = middle + 1;
}
}
output << min_time << "\n";
for (int i = 0; i<N; i++)
output << raspuns[i].first << " " << raspuns[i].second << "\n";
return 0;
}