Pagini recente » Istoria paginii runda/23dezile_4/clasament | Istoria paginii runda/ty/clasament | Cod sursa (job #2072061) | Istoria paginii runda/test21341/clasament | Cod sursa (job #2427854)
#include <fstream>
#include <algorithm>
#define MAX 100
using namespace std;
struct lapte
{
int first;
int second;
int poz;
}v[MAX], cant[MAX], cantMin[MAX];
bool cmp(lapte a, lapte b)
{
if(a.first + b.second < b.first + a.second)return true;
return false;
}
bool cmpPoz(lapte a, lapte b)
{
if(a.poz < b.poz)return true;
return false;
}
bool solve(int N, int L, int T)
{
int cantA, cantB, i;
cantA = cantB = 0;
for(i = 0; i < N; i++)
{
cant[i].poz = v[i].poz;
if(cantA + T / v[i].first < L)
{
cantA += T / v[i].first;
cant[i].first = T / v[i].first;
cant[i].second = 0;
}
else{
int dif = L - cantA;
int rest = T - dif * v[i].first;
cantA = L;
cantB += rest / v[i].second;
cant[i].first = dif;
cant[i].second = rest / v[i].second;
}
}
if(cantA == L && cantB >= L)return true;
return false;
}
int main()
{
int n, l, i, st, dr, mij, gasit;
ifstream fin("lapte.in");
ofstream fout("lapte.out");
fin >> n >> l;
for(i = 0; i < n; i++)
{
fin >> v[i].first >> v[i].second;
v[i].poz = i;
}
sort(v, v + n, cmp);
st = 1;
dr = MAX;
while(st <= dr)
{
mij = (st + dr) / 2;
if(solve(n, l, mij))
{
sort(cant, cant + n, cmpPoz);
for(i = 0; i < n; i++)
{
cantMin[i].first = cant[i].first;
cantMin[i].second = cant[i].second;
}
gasit = mij;
dr = mij - 1;
}
else st = mij + 1;
}
fout << gasit << '\n';
for(i = 0; i < n; i++)fout << cantMin[i].first << " " << cantMin[i].second << '\n';
fin.close();
fout.close();
return 0;
}