Cod sursa(job #1399424)

Utilizator addy01adrian dumitrache addy01 Data 24 martie 2015 18:56:44
Problema Garaj Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.12 kb

#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;

const int maxn = 100010;

struct camion
{
    int c,t,s;
} v[maxn];

int N,M,smax,sum,T=1<<20,sol;

inline bool mycomp(camion a,camion b)
{
    return a.s>b.s;
}

int main()
{
    ifstream in("garaj.in");
    ofstream out("garaj.out");

    in>>N>>M;
    int i;
    for(i=1; i<=N; i++)
    {
        in>>v[i].c>>v[i].t;
        v[i].t*=2;
    }
    smax=200000000;
    int st=1,dr=smax,mid;

    while(st<dr)
    {
        mid=(st+dr)/2;
        sum=0;

        for(i=1; i<=N; i++)
        {
            sum+=(mid/v[i].t)*v[i].c;

            if(sum>=M)
            {
                dr=mid;
                i=N+1;
                if(mid<T)
                    T=mid;
            }
        }
        if(sum<M)
        {
            st=mid+1;
        }
    }

    for(i=1; i<=N; i++)
        v[i].s=(T/v[i].t)*v[i].c;

    sort(v+1,v+N+1,mycomp);


    sol=0;
    i=1;
    while(sol<T&&i<=N)
    {
        sol+=v[i].s;
        i++;
    }
    sol=i;

    out<<T<<" "<<sol;

    return 0;
}