题目链接
https://codeforces.com/problemset/problem/1400/B
解题思路
肯定先拿重量小的,重量小的拿完了才能拿重量大的,除非小的都拿不完。
首先枚举第一个人拿到重量小的个数,再确定第二个人拿到重量小的个数,再确定第一个人拿到重量大的个数,再最后确定第二个人拿到重量大的个数,四个个数相加的最小值为答案。
我只想说大佬的思路太清晰了,这三个min和一个max直接Orz!!!
AC代码
#include<bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int f, p, cnts, cntw, s, w, ans = 0;
cin >> f >> p >> cnts >> cntw >> s >> w;
if (s > w) {
swap(s, w);
swap(cnts, cntw);
}
for (int f1 = 0; f1 <= f / s && f1 <= cnts; f1++) {//枚举第一个人拿到物品1的个数
int f2 = 0, p1 = 0, p2 = 0;
f2 = min(cntw, (f - s * f1) / w);//确定第一个人最多能拿到多少物品2
p1 = min(cnts - f1, p / s);//确定第二个人最多能拿到多少物品1
p2 = min(cntw - f2, (p - s * p1) / w);//确定第二个人最多能拿到多少物品2
ans = max(ans, f1 + f2 + p1 + p2);
}
cout << ans << endl;
}
}总结
我开始想贪心,贪不出来,只能二分,二分watest2……

京公网安备 11010502036488号