Cod sursa(job #874817)

Utilizator Theorytheo .c Theory Data 9 februarie 2013 12:52:21
Problema Lapte Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.51 kb
#include<fstream>
#include<algorithm>

using namespace std;

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

int N; int T;

struct timp { int t1; int t2; double f; int ind;int c ; };

const int bound = 1<<13;
timp t[102];


bool cmp(timp a, timp b){
    return a.f < b.f;
}

void Read() {

    fin >> N>> T;
    for(int i = 1; i <= N; ++i){

        fin >> t[i].t1 >> t[i].t2;
        t[i].f = (double)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)
        t[i].c = 0;

    while(cnt <= N && B1 + x / t[cnt].t1 <= T){
        B1 += (x/t[cnt].t1);
        t[cnt].c = x/t[cnt].t1;
        cnt++;

    }

    if(B1 < T && cnt <= N){
        B2 += (x - (T - B1) * t[cnt].t1) / t[cnt].t2;
        t[cnt].c = (T - B1);
        cnt++;
    }

     for(int i = cnt; i <= N; ++i)
        B2 += (x/t[i].t2);
    return B2 >= T;

}

int bs(){

    int step, 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;
}

bool cmp2 (timp a, timp b){
    return a.ind < b.ind;
}
int main (){

    Read ();

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

    sort(&t[1], &t[1 + N], cmp2);

    for(int i = 1; i <= N; ++i)
        fout << t[i].c <<" "<<(x - t[i].c * t[i].t1)/t[i].t2<<'\n';

    return 0;
}