Cod sursa(job #1029289)

Utilizator UnforgivenMihai Catalin Botezatu Unforgiven Data 15 noiembrie 2013 12:10:48
Problema Lapte Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.65 kb
#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)));
}

bool cmpAfisare(const Om & a , const Om & b)
{
    return (a.vect_poz < b.vect_poz);
}


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[i] = lapte[i];
            }

        }
        else if (LB != 0 || LA != 0)
        {
            left = middle + 1;
        }
    }
    sort(raspuns , raspuns + N , cmpAfisare);
    output << min_time << "\n";
    for (int i = 0; i<N; i++)
        output << raspuns[i].first << " " << raspuns[i].second << "\n";

    return 0;
}