Cod sursa(job #874809)

Utilizator Theorytheo .c Theory Data 9 februarie 2013 12:48:34
Problema Lapte Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.56 kb
#include<fstream>
#include<algorithm>

using namespace std;

ifstream fin("lapte.in");
ofstream fout("lapte.out");

const int bound = 1<<13;
int N; int T;

struct timp { int t1; int t2; int ind; };
struct rezultat {int a; int b;};
rezultat rez[105];

timp t[105];


bool cmp(timp a, timp b){
    return a.t1 - a.t2 < b.t1 - b.t2;
}
void Read() {

    fin >> N>> T;
    for(int i = 1; i <= N; ++i){
        fin >> t[i].t1 >> t[i].t2;
        t[i].ind = i;
    }
    sort(&t[1], &t[1 + N], cmp);
}

bool bea(int x){
    int cnt = 1, B1 = 0, B2 = 0;

    for(int i = 1;i <= N; ++i)
        rez[i].a = rez[i].b = 0 ;

    while(cnt <= N && B1 < T){

        if(B1 + x/t[cnt].t1 >= T){
            rez[t[cnt].ind].a = T - B1;
            B2 = (x - (T - B1) * t[cnt].t1) / t[cnt].t2;
            rez[t[cnt++].ind].b = B2;
            break;
        }
        B1 +=x/t[cnt].t1;
        rez[t[cnt].ind].a = x/t[cnt].t1;
        cnt++;
    }

    while(cnt <= N && B2 < T){
        B2 += x / t[cnt].t2;
        rez[t[cnt].ind].b = x / t[cnt].t2;
        cnt++;
    }


    return B2 >= T;

}

int bs(){
    int step = 1<<13, pos;
    for(step = 1; step <= bound; (step <<=1));

    for(pos = 0; step; (step >>= 1))
       if(pos + step <= bound && bea(pos + step) == false)
            pos += step;
    return pos;
}

int main (){

    Read ();

    int x = bs();
    fout << x + 1<<'\n';
    bea(x + 1); ++x;

    for(int i = 1; i <= N; ++i){
        fout << rez[i].a << " "<< rez[i].b<<'\n';
    }

    return 0;
}