这是一个典型的贪心算法问题。我们需要找到用有限的钱购买最多糖果的方法。
关键思路:
对于每种糖果,计算平均价格(两颗糖果的总价除以2) 优先选择平均价格最低的糖果 code: #include <bits/stdc++.h> using namespace std;
const int MAXN = 100000; long long x[100005], y[100055], a[100005];
int main() { int n; long long m; cin >> n >> m; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; a[i] = x[i] + y[i]; }
sort(a, a + n);
long long total = 0;
long long money = m;
for (int i = 0; i < n; i++)
{
if (money <= 0) break;
long long cost = a[i];
long long maxn = money / cost;
if (maxn > 0)
{
total += maxn * 2;
money -= maxn * cost;
}
}
long long minn = *min_element(x, x + n);
if (money >= minn)
{
total++;
}
cout << total << endl;
return 0;
}