题目链接:codeforces 1400 B. RPG Protagonist
题意:
给出 p, f, cnts, cntw, s, w,有4个未知数,x1, y1, x2, y2,
要求为: x1 * s + y1 * w <= p
x2 * s + y2 * w <= f
x1 + x2 <= cnts
y1 + y2 <= cntw
求max(x1+ y1 + x2 + y2)
- int x2 = min(cnts-x1, f/s);
- int y1 = min(cntw, (p-x1*s)/w);
- int y2 = min(cntw-y1, (f-x2*s)/w);
- ans = max(ans, x1+x2+y1+y2);
解题思路:
根据限制条件,求公式。 求得
x2 = min( cnts - x1 , ( f / s ))
y1 = min(cntw, ( p - x1 * s) / w );
y2 = min(cntw - y1, (f - x2 * s ) / w );
遍历x1 : x1 的限制条件 x1 * s <= p, x1 <= cnts
#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
int a[maxn], b[maxn];
int main(){
int t;
cin >> t;
while(t--) {
int p, f, cnts, cntw, s, w;
scanf("%d%d%d%d%d%d", &p, &f, &cnts, &cntw, &s, &w);
if(s > w) {
swap(s, w);
swap(cnts, cntw);
}
int ans = -1;
for(int x1 = 0; x1 <= cnts && x1 * s <= p; x1++) {
int x2 = min(cnts-x1, f/s);
int y1 = min(cntw, (p-x1*s)/w);
int y2 = min(cntw-y1, (f-x2*s)/w);
ans = max(ans, x1+x2+y1+y2);
}
printf("%d\n", ans);
}
return 0;
}