Cod sursa(job #2093690)

Utilizator tifui.alexandruTifui Ioan Alexandru tifui.alexandru Data 24 decembrie 2017 12:58:01
Problema Garaj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.43 kb
#include <bits/stdc++.h>
#define Nmax 100001
#define tip pair < pair <int,int>, int>
#define c first.first
#define t first.second
#define comp second
#define DIM 70000
using namespace std;
char buffer[DIM];
int poz=DIM-1;
void read(int &x)
{
    x=0;
    while(!isdigit(buffer[poz]))
        if(++poz==DIM)
        {
            poz=0;
            fread(buffer,1,DIM,stdin);
        }
    while(isdigit(buffer[poz]))
    {
        x=x*10+buffer[poz]-'0';
        if(++poz==DIM)
        {
            poz=0;
            fread(buffer,1,DIM,stdin);
        }
    }
}
tip v[Nmax];
int ans;
inline bool cmp(const tip &x, const tip &y)
{
    return x.comp>y.comp;
}
int main()
{
    freopen("garaj.in","r",stdin);
    freopen("garaj.out","w",stdout);
    int n,m,i;
    read(n);
    read(m);
    for(i=1;i<=n;i++)
    {
        read(v[i].c);
        read(v[i].t);
        v[i].t<<=1;
    }
    int lsh=1,rsh=(int)1e9,mid,sum;
    while(lsh<=rsh)
    {
        mid=(lsh+rsh)>>1;
        sum=0;
        for(i=1;i<=n;i++)
        {
            sum+=(v[i].c*(mid/v[i].t));
            if(sum>=m) i=n+12;
        }
        if(i==n+13)
        {
            ans=mid;
            rsh=mid-1;
        }
        else lsh=mid+1;
    }
    for(i=1;i<=n;i++)
        v[i].comp=v[i].c*(ans/v[i].t);
    sort(v+1,v+n+1,cmp);
    for(i=1;m>0;i++)
        m-=v[i].comp;
    printf("%d %d",ans,i-1);

    return 0;
}