Cod sursa(job #1490515)

Utilizator alexpetrescuAlexandru Petrescu alexpetrescu Data 23 septembrie 2015 18:29:27
Problema Garaj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.4 kb
#include <cstdio>
#include <cctype>
#include <algorithm>
#define BUF_SIZE 4096
#define MAXN 100000
typedef struct{
    int c, t;
}mycreation;
mycreation v[MAXN];
int e[MAXN];
int n, m, rez;
int pos=BUF_SIZE;
char buf[BUF_SIZE];
FILE *fin;
inline char getch(){
    if(pos==BUF_SIZE){
        fread(buf, BUF_SIZE, 1, fin);
        pos=0;
    }
    return buf[pos++];
}
inline int read(){
    int x=0;
    char ch=getch();
    while(!isdigit(ch)){
        ch=getch();
    }
    while(isdigit(ch)){
        x=10*x+ch-'0';
        ch=getch();
    }
    return x;
}
bool cmp(const int a, const int b){
    return (a>b);
}
bool ok(int x){
    long long s=0;
    for(int i=0; i<n; i++){
        s+=v[i].c*(x/v[i].t);
    }
    return (s>=m);
}
int main(){
    int i, s, pas;
    FILE *fout;
    fin=fopen("garaj.in", "r");
    fout=fopen("garaj.out", "w");
    n=read();
    m=read();
    for(i=0; i<n; i++){
        v[i].c=read();
        v[i].t=read();
        v[i].t*=2;
    }
    rez=0;
    for(pas=1<<29; pas; pas>>=1){
        if(!ok(rez+pas)){
            rez+=pas;
        }
    }
    rez++;
    for(i=0; i<n; i++){
        e[i]=v[i].c*(rez/v[i].t);
    }
    std::sort(e, e+n, cmp);
    i=0;
    s=0;
    while((s<m)&&(i<n)){
        s+=e[i];
        i++;
    }
    fprintf(fout, "%d %d\n", rez, i);
    fclose(fin);
    fclose(fout);
    return 0;
}