codeforces 1400 B. RPG Protagonist

题目链接: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)

  1. int x2 = min(cnts-x1, f/s);
  2. int y1 = min(cntw, (p-x1*s)/w);
  3. int y2 = min(cntw-y1, (f-x2*s)/w);
  4. 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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值