Cod sursa(job #3316667)

Utilizator MihaiZ777MihaiZ MihaiZ777 Data 19 octombrie 2025 18:25:07
Problema Lapte Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 2.74 kb
#include <algorithm>
#include <array>
#include <cassert>
#include <cmath>
#include <deque>
#include <iostream>
#include <fstream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#include <cstring>
using namespace std;
using ll  = long long;

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

#define fast_io ios::sync_with_stdio(false); cin.tie(nullptr);
#define all(x)  (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define vec_read(v, n)                \
	for (int i = 0; i < (n); i++) {   \
		cin >> v[i];                  \
	}
#define vec_print(v, n, sp)           \
	for (int i = 0; i < (n); i++) {   \
		cout << v[i] << sp;           \
	}                                 \
	cout << '\n';
#define vec_print_rev(v, n, sp)          \
	for (int i = (n) - 1; i>= 0; i--) {  \
		cout << v[i] << sp;              \
	}                                    \
	cout << '\n';
#ifdef LOCAL
	#define dbg(x) cerr << #x << " = " << (x) << '\n'
#else
	#define dbg(x)
#endif

const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 1e2 + 5;

int n, l;
int a[MAXN], b[MAXN];
// given t time, dp[i][j] = max liters of b for first i people and j liters of a
int dp[MAXN][MAXN], ans[MAXN][MAXN];

bool Check(int t) {
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= l; j++) {
			dp[i][j] = -INF;
			ans[i][j] = 0;
		}
	}
	dp[0][0] = 0;
	
	// considering first i people
	for (int i = 1; i <= n; i++) {
		// for each possible j liters of a
		for (int j = 0; j <= l; j++) {
			cout << i << ' ' << j << endl;
			// for actual k drank liters of a
			for (int k = 0; k <= j && k * a[i] <= t; k++) {
				// how much of b can drink with k liters of a
				int candidate = (t - k * a[i]) / b[i];
				if (dp[i][j] < dp[i - 1][j - k] + candidate) {
					dp[i][j] = dp[i - 1][j - k] + candidate;
					ans[i][j] = k;
				}
			}
		}
	}

	if (dp[n][l] >= l) {
		return true;
	}
	return false;
}

void Walk(int t, int i, int j) {
	if (i == 0) {
		return;
	}
	Walk(t, i - 1, j - ans[i][j]);
	fout << ans[i][j] << " " << (t - ans[i][j] * a[i]) / b[i] << '\n'; 
}

void Reconstruct(int t) {
	Check(t);
	Walk(t, n, l);
}

void Solve() {
	fin >> n >> l;
	for (int i = 1; i <= n; i++) {
		fin >> a[i] >> b[i];
	}

	int total;
	int step = 128 >> 1;
	for (int lf = 0; step > 0; step >>= 1) {
		cout << step << endl;
		if (Check(lf + step)) {
			total = lf + step;
		} else {
			lf += step;
		}
	}

	fout << total << '\n';
	Reconstruct(total);
}

int main() {
	// fast_io;
#ifdef LOCAL
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
#endif
	int T = 1;
	// cin >> T;
	while (T--) {
		Solve();
	}
	return 0;
}