Can I use Chinese?
这个题是一道二分题目
he question is a two-point question.
题意很简单,不需要解释了
It is very simple, do not need to explain.
但是我一直没过样例,原因是我的变量类型搞错了
But I haven't had the sample because my variables are wrong.
应该是`long long`,但是我写成了`int`
It should be `long long`, but I use the `int`.
下面是代码
Next is my code.
#include <iostream> #include <cstring> #include <cstdio> using namespace std; int q[5]; // B C S int c[5]; int m[5]; char s[110]; int B,C,S; long long tot,ans; int check(long long n) { long long tmp=tot; if(n*c[1]>q[1]) tmp-=(n*c[1]-q[1])*m[1]; if(n*c[2]>q[2]) tmp-=(n*c[2]-q[2])*m[2]; if(n*c[3]>q[3]) tmp-=(n*c[3]-q[3])*m[3]; if(tmp>=0) return 1; else return 0; } int main() { cin>>s; for(int i=0;i<strlen(s);i++) { if(s[i]=='B') c[1]++; if(s[i]=='S') c[2]++; if(s[i]=='C') c[3]++; } cin>>q[1]>>q[2]>>q[3]; cin>>m[1]>>m[2]>>m[3]; cin>>tot; int d=tot; long long l=1,r=2333333333333; long long mid; while(l<=r) { mid=(l+r)/2; if(check(mid)) l=mid+1,ans=mid; else r=mid-1; } cout<<ans; return 0; }
还可以用贪心
贪心就比较好想了
尽可能地将原有材料耗光,然后买

#include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; int nb,ns,nc; int mb,ms,mc; long long m; char a[109]; int B,S,C; long long ans; int main() { cin>>a; for(int i=0;i<strlen(a);i++) { if(a[i]=='B') B++; if(a[i]=='C') C++; if(a[i]=='S') S++; } cin>>nb>>ns>>nc; cin>>mb>>ms>>mc; cin>>m; int minn=233333333; if(B) minn=min(minn,nb/B); if(C) minn=min(minn,nc/C); if(S) minn=min(minn,ns/S); ans+=minn; ns-=S*minn;nb-=B*minn;nc-=C*minn; if(S==0) ns=0; if(C==0) nc=0; if(B==0) nb=0; int zz=B*mb+C*mc+S*ms; int tot=0; while(nb!=0||ns!=0||nc!=0) { tot=0; if(nb>=B) nb-=B; else { tot+=(B-nb)*mb; nb=0; } if(nc>=C) nc-=C; else { tot+=(C-nc)*mc; nc=0; } if(ns>=S) ns-=S; else { tot+=(S-ns)*ms; ns=0; } if(tot>m) break; else m-=tot,ans++; } ans+=m/zz; cout<<ans; return 0; }