Cod sursa(job #1320440)

Utilizator diana97Diana Ghinea diana97 Data 17 ianuarie 2015 23:21:09
Problema Garaj Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.3 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

ifstream f ("garaj.in");
ofstream g ("garaj.out");

struct Camion {
    int t, c;
};

const int NMAX = 100000 + 1;

int n, m;
int cmax, tmax;
Camion camion[NMAX];
vector <int> sol;


void citeste() {
    f >> n >> m;

    for (int i = 1; i <= n; i++) {
        f >> camion[i].c >> camion[i].t;
        camion[i].t *= 2;
    }
}

int se_poate(int timp) {
    int x = m;
    for (int i = 1; i <= n; i++) {
        if (x <= camion[i].c * (timp / camion[i].t)) return i;
        x = x - camion[i].c * (timp / camion[i].t);
    }
    return -1;
}

inline bool comp(int a, int b) {
    return a > b;
}

int camioane(int timp) {
    for (int i = 1; i <= n; i++)
        sol.push_back(timp / camion[i].t * camion[i].c);
    sort(sol.begin(), sol.end(), comp);

    int x = m;
    for (int i = 0; i <= n; i++) {
        if (x <= 0) return i;
        x = x - sol[i];
    }
    return -1;
}

void rezolva() {
    int st = 1, dr = (1 << 25), m;
    int sol = -1;

    while (st <= dr) {
        m = (st + dr) / 2;
        if (se_poate(m) != -1) {
            sol = m;
            dr = m - 1;
        }
        else st = m + 1;
    }

    g << sol << ' ' << camioane(sol) << '\n';
}

int main() {
    citeste();
    rezolva();
    return 0;
}