CodeForces - 1538G Gift Set(二分)
題目鏈接:點擊查看
題目大意:給出 a,b,x,ya,b,x,ya,b,x,y,分別表示有 aaa 個藍色糖果和 bbb 和紅色糖果,現在有兩種打包方式:
問最多能打包多少袋糖果
題目分析:賽場上寫了個三分死活過不去,死后把三分的范圍改成 100010001000 就過了,然后第二天睡了一覺起來又被 hackhackhack 了,神奇
假設需要 t1t_1t1? 種第一種打包方式,需要 t2t_2t2? 種第二種打包方式,我們的目標是需要滿足下列不等式的前提下,使得 t1+t2t_1+t_2t1?+t2? 最大
比較顯然的一點是,知道 t1t_1t1? 之后,就可以貪心計算 t2t_2t2? 了,所以賽場上就是去三分 t1t_1t1? 然后求 t1+t2t_1+t_2t1?+t2? 的最大值,但很可惜這樣是錯誤的
所以我們嘗試去二分 t1+t2t_1+t_2t1?+t2? 的值,記為 midmidmid,現在我設一個新的變量 kkk,記 t1=k,t2=mid?kt_1=k,t_2=mid-kt1?=k,t2?=mid?k,現在我們只需要找到一個 kkk,滿足上述條件即可
為了方便起見,我假設 x<=y&&a<=bx<=y\ \&\&\ a<=bx<=y?&&?a<=b
將上面的不等式代入 kkk 得到:
將 kkk 拿出來,得到了一個不等式:
0<=x?b?mida?b<=k<=y?mid?ab?a<=mid0<=\frac{x-b*mid}{a-b}<=k<=\frac{y-mid*a}{b-a}<=mid0<=a?bx?b?mid?<=k<=b?ay?mid?a?<=mid
然后直接 checkcheckcheck 就好了,需要注意的是,因為涉及到了除法,所以分母不能為 000,需要特判一下 a=ba=ba=b 的情況
再者就是取整問題,因為我們需要取,在范圍內靠近 kkk 的那個數,所以左邊的式子需要向上取整,而右邊的式子需要向下取整
代碼:
// Problem: G. Gift Set // Contest: Codeforces - Codeforces Round #725 (Div. 3) // URL: https://codeforces.com/contest/1538/problem/G // Memory Limit: 256 MB // Time Limit: 2000 ms // // Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2) // #pragma GCC optimize("Ofast","inline","-ffast-math") // #pragma GCC target("avx,sse2,sse3,sse4,mmx") #include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cmath> #include<cstring> #include<algorithm> #include<stack> #include<climits> #include<queue> #include<map> #include<set> #include<sstream> #include<cassert> #include<bitset> #include<list> #include<unordered_map> #define lowbit(x) x&-x using namespace std; typedef long long LL; typedef unsigned long long ull; template<typename T> inline void read(T &x) {T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f; } template<typename T> inline void write(T x) {if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0'); } const int inf=0x3f3f3f3f; const int N=1e6+100; LL x,y,a,b; bool check(int mid) {int l=ceil(1.0*(x-b*mid)/(a-b));int r=floor(1.0*(y-a*mid)/(b-a));l=max(l,0),r=min(r,mid);return l<=r; } int main() { #ifndef ONLINE_JUDGE // freopen("data.in.txt","r",stdin); // freopen("data.out.txt","w",stdout); #endif // ios::sync_with_stdio(false);int w;cin>>w;while(w--) {read(x),read(y),read(a),read(b);if(b==a) {cout<<min(x,y)/a<<endl;continue;}if(x>y) {swap(x,y);}if(a>b) {swap(a,b);}int l=0,r=inf,ans=-1;while(l<=r) {int mid=(l+r)>>1;if(check(mid)) {ans=mid;l=mid+1;} else {r=mid-1;}}cout<<ans<<endl;}return 0; }總結
以上是生活随笔為你收集整理的CodeForces - 1538G Gift Set(二分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CodeForces - 1485F C
- 下一篇: POJ - 2914 Minimum C