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;
}