Codeforces Round #218 (Div. 2) 371C - Hamburgers (二分)

Codeforces Round #218 (Div. 2) 371C - Hamburgers (二分)

题目大意:

给你一个目标序列,包含 t1个 ‘B’ , t2个 ‘S’ , t3个 ‘C’,你已经有了n1个 ‘B’ , n2个 ‘S’ , n3个 ‘C’,另外可以在商店每购买一个B/S/C,分别花费 p1, p2 , p3 元,你拥有 R 元,要求最多能得到多少个目标序列。

思路:

假设购买 x1个’B’,x2个’S’,x3个’C’

  • p1 · x1+p2 · x2+p3 · x3<=R
  • 求max(min((n1+x1)/t1,(n2+x2)/t2,(n3+x3)/t3))=ans;
  • 这样的话,可以知道答案ans一定是具有单调性的,故选择 二分 求解

Code:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
using namespace std;
#define Please return 
#define Accepted 0
#define int long long
#define endl "\n"
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<double, double> PDD;
const int N = 200010,M=2*N,INF=0x3f3f3f3f;
string s;
int t1,t2,t3;
int n1,n2,n3;
int p1,p2,p3;
int x1,x2,x3;
int R;
bool check(int x)
{
//(n1+x1)/t1,(n2+x2)/t2,(n3+x3)/t3)=x;
	x1=max(0ll,x*t1-n1);
	x2=max(0ll,x*t2-n2);
	x3=max(0ll,x*t3-n3);
//p1*x1+p2*x2+p3*x3<=r
	int sum=p1*x1+p2*x2+p3*x3;
	if(sum<=R)	return true;
	else return false;
}
void slove(int _case)
{ 
	cin>>s;
	cin>>n1>>n2>>n3>>p1>>p2>>p3>>R;
	for(int i=0;i<s.size();i++){
		if(s[i]=='B')	t1++;
		else if(s[i]=='S') t2++;
		else t3++;
	} 
	int l=0,r=1e12+200;//要稍微大点
	int ans=0;
	while(l<r)
	{
		int mid=l+r>>1;
		if(check(mid))	ans=mid,l=mid+1;
		else r=mid;
//		cout<<l<<" "<<r<<" "<<ans<<endl;
	}
	cout<<ans<<endl;
}

signed main()
{
	ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int T=1;
//	cin>>T;
	for(int _case=1;_case<=T;_case++)
	{
		slove(_case);
	}
	Please Accepted;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值