Hamburgers 二分答案

在这里插入图片描述
在这里插入图片描述

题目大意:有一种汉堡,用B、S、C三种原料做成,现在告诉你当前有的B、S、C的个数,到商店买的B、S、C的单价(商店无限供应这三种原料),还有你拥有的钱。问最多能做多少个汉堡。


刚开始我还以为是模拟,先把能用的用完,再去买。但是写了半天写不下去了,找了一下题解才发现是二分答案板子题。发现自己对二分还是不是很敏感。

AC代码:

//https://blog.csdn.net/hesorchen
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define ll long long
#define endl "\n"
#define INF 0x3f3f3f3f
#define MAX 100010
#define mod 1000000007

int main()
{
    string a;
    cin >> a;
    ll b, s, c;
    b = s = c = 0;
    int len = a.size();
    for (int i = 0; i < len; i++)
        if (a[i] == 'B')
            b++;
        else if (a[i] == 'S')
            s++;
        else if (a[i] == 'C')
            c++;
    ll NOW_b, NOW_s, NOW_c, V_b, V_s, V_c;
    cin >> NOW_b >> NOW_s >> NOW_c >> V_b >> V_s >> V_c;
    ll money;
    cin >> money;
    ll l = 1, r = 10000000000000;
    ll ans=0;
    while (l <= r)
    {
        ll SUM = 0;
        ll mid = (l + r) / 2;
        ll need_b = mid * b;
        ll need_s = mid * s;
        ll need_c = mid * c;
        if (need_b > NOW_b)
            SUM += (need_b - NOW_b) * V_b;
        if (need_s > NOW_s)
            SUM += (need_s - NOW_s) * V_s;
        if (need_c > NOW_c)
            SUM += (need_c - NOW_c) * V_c;
        if (SUM <= money)
        {
            l = mid + 1;
            ans = mid;
        }
        else
            r = mid - 1;
    }
    cout << ans << endl;
    return 0;
}
发布了76 篇原创文章 · 获赞 64 · 访问量 9327

猜你喜欢

转载自blog.csdn.net/hesorchen/article/details/104682906