Cod sursa(job #1784660)

Utilizator eu3neuomManghiuc Teodor-Florin eu3neuom Data 20 octombrie 2016 12:33:59
Problema Lapte Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.23 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("lapte.in");
ofstream fout("lapte.out");

const int NMax = 105;

int A[NMax];
int B[NMax];

inline bool check(const int &n, const int &m, const int &t){

    int D[2][NMax];
    for(int i = 0; i <= m; i++) D[0][i] = -INFINITY;
    for(int i = 0; i <= m; i++) D[1][i] = 0;
    D[0][0] = 0;

    int cur = 1;
    for(int i = 1; i <= n; i++){
        for(int j = 0; j <= m && j * A[i] <= t; j++){
            const int l = (t - j * A[i]) / B[i];
            for(int k = m; k - j >= 0; k--){
                D[cur][k] = max(D[cur][k], D[1 - cur][k - j] + l);
            }
        }
        cur = 1 - cur;
    }

    cur = 1 - cur;
    return D[cur][m] >= m;

}

inline void solve(const int &n, const int &m, const int &t){

    int D[NMax][NMax];
    int from[NMax][NMax];
    for(int i = 0; i <= n; i++){
        for(int j = 0; j <= m; j++){
            D[i][j] = -INFINITY;
            from[i][j] = 0;
        }
    }
    D[0][0] = 0;

    for(int i = 1; i <= n; i++){
        for(int j = 0; j <= m && j * A[i] <= t; j++){
            const int l = (t - j * A[i]) / B[i];
            for(int k = m; k - j >= 0; k--){
                if(D[i - 1][k - j] + l > D[i][k]){
                    D[i][k] = D[i - 1][k - j] + l;
                    from[i][k] = k - j;
                }
            }
        }
    }

    vector < int > ans;
    int now = m;
    for(int i = n; i > 0; i--){

        ans.push_back(now);
        now = from[i][now];

    }

    fout << ans[n - 1] << " " << D[1][ans[n - 1]] << "\n";


    for(int i = n - 2; i >= 0; i--){
        fout << ans[i] - ans[i + 1] << " " << D[n - i][ans[i]] - D[n - i - 1][ans[i + 1]] << "\n";
    }

}

int main(){

    ios::sync_with_stdio();
    fin.tie(NULL);

    int n, m;
    fin >> n >> m;

    for(int i = 1; i <= n; i++) fin >> A[i] >> B[i];

    int lo, hi, mid, ans;
    lo = 1; hi = NMax * NMax;
    while(lo <= hi){

        mid = (lo + hi) / 2;
        if(check(n, m, mid) == true){
            ans = mid;
            hi = mid - 1;
        } else {
            lo = mid + 1;
        }

    }

    fout << ans << "\n";
    solve(n, m, ans);

    return 0;
}