Cod sursa(job #1573238)

Utilizator laurageorgescuLaura Georgescu laurageorgescu Data 19 ianuarie 2016 15:40:12
Problema Garaj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.62 kb
#include<cstdio>
#include<algorithm>

using namespace std;

FILE *fin = fopen( "garaj.in", "r" ), *fout = fopen( "garaj.out", "w" );

typedef long long i64;

const int nmax = 1e5;
const int tmax = 1000;
const int Size = 52 * 1e5;
int n;
i64 m;
i64 s[ nmax + 1 ];
short int t[ nmax + 1 ], c[ nmax + 1 ];

int pos;
char buff[ Size ];

char next_pos() {
    ++ pos;
    if ( pos == Size ) {
        pos = 0;
        fread( buff, Size, 1, fin );
    }
    return buff[ pos ];
}
int nmb() {
    char ch = next_pos();
    while ( '0' > ch || ch > '9' ) {
        ch = next_pos();
    }
    int x = 0;
    while( '0' <= ch && ch <= '9' ) {
        x = x * 10 + ch - '0';
        ch = next_pos();
    }
    return x;
}
bool check( i64 T ) {
    i64 ans = 0;
    for( int i = 1; i <= n; ++ i ) {
        ans += T / ( 2 * t[ i ] ) * c[ i ];
        if ( ans >= m ) {
            return 1;
        }
    }
    return 0;
}
int main() {
    pos = Size - 1;

    n = nmb(); m = ( i64 )nmb();
    for( int i = 1; i <= n; ++ i ) {
        c[ i ] = nmb(); t[ i ] = nmb();
    }

    i64 n2, ans = nmax * tmax;
    for( n2 = 1; (n2 << 1) <= ans; n2 <<= 1 ) {
    }

    for( i64 step = n2; step > 0; step >>= 1 ) {
        if ( ans - step >= 0 && check( ans - step ) ) {
            ans -= step;
        }
    }

    for( int i = 1; i <= n; ++ i ) {
        s[ i ] = ans / ( 2 * t[ i ] ) * c[ i ];
    }
    sort( s + 1, s + n + 1 );
    i64 sum, cate;
    sum = 0; cate = 0;
    for( int j = n; j > 0 && sum < m; -- j ) {
        sum += s[ j ];
        ++ cate;
    }

    fprintf( fout, "%lld %lld\n", ans, cate );
    fclose( fin );
    fclose( fout );
    return 0;
}