Cod sursa(job #2873133)

Utilizator andreifilimonPopescu Filimon Andrei Cosmin andreifilimon Data 18 martie 2022 18:24:07
Problema Garaj Scor 30
Compilator c-64 Status done
Runda concursceva1 Marime 2.63 kb
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

#define MAXLOG10 10
#define BUFSIZE (128 * 1024)
FILE *fin, *fout;
int p10[MAXLOG10 + 1] = { 0, 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000};
int rpos;
char rbuf[BUFSIZE];
int wpos;
char wbuf[BUFSIZE];

static inline void initRead()
{
    rpos=BUFSIZE-1;
}
static inline char readChar()
{
    if(!(rpos=(rpos+1) & (BUFSIZE-1)))
        fread(rbuf, 1, BUFSIZE, fin);
    return rbuf[rpos];
}
int readInt()
{
    int ch, res=0, semn=1;
    while(isspace(ch=readChar()));
    if(ch=='-')
    {
        semn=-1;
        ch=readChar();
    }
    do
        res=10*res+ch-'0';
    while(isdigit(ch=readChar()));

    return semn*res;
}
static inline void initWrite()
{
    wpos=0;
}
static inline void writeChar(char ch)
{
    wbuf[wpos]=ch;
    if(!(wpos=(wpos+1) & (BUFSIZE-1)))
        fwrite(wbuf, 1, BUFSIZE, fout);
}
void writeInt(int x)
{
    int i,cf;

    if(x<0)
    {
        writeChar('-');
        x=-x;
    }
    i=MAXLOG10;
    while(p10[i]>x)
        i--;
    if(i==0)
        writeChar('0');
    else
        do
        {
            cf='0';
            while(p10[i]<=x)
            {
                x-=p10[i];
                cf++;
            }
            writeChar(cf);
        }
        while(--i>0);

}

static inline void flushBuf()
{
    fwrite(wbuf, 1, wpos, fout);
}


# define DIM 100000
# define MAX 2000000000
int C[DIM+3], T[DIM+3];

int main()
{
    fin=fopen("garaj.in","r");
    fout=fopen("garaj.out","w");
    int n,m,t=MAX,N;
    initRead();
    n=readInt();
    m=readInt();
    for(int i=1; i<=n; ++i)
    {
        C[i]=readInt();
        T[i]=readInt();
        T[i]*=2;
    }
    int c;
    for(int st=1, dr=MAX, mij=(st+dr)>>1; st<=dr; mij=(st+dr)>>1)
    {
        c=0;
        int i;
        for(i=1; i<=n && c<m; ++i)
            c+=mij/T[i]*C[i];
        if(c>=m)
        {
            dr=mij-1;
            if(t>mij)
                t=mij;
        }
        else
        {
            st=mij+1;
        }
    }
    for(int i=1; i<=n; ++i)
        T[i]=-(t/T[i]*C[i]);
    int max,p,u,i;
    for(u=n; u>1; u--)
    {
        max=T[0];
        p=0;
        for(i=1; i<=u; i++)
            if(T[i]>max)
            {
                max=T[i];
                p=i;
            }
        T[p]=T[u];
        T[u]=max;
    }
    c=0;
    for(N=0; N<n && c<m; ++N)
    {
        c-=T[N+1];
    }
    writeInt(t);
    writeChar(' ');
    writeInt(N);
    fclose(fin);
    flushBuf();
    fclose(fout);
    return 0;
}