日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CodeForces - 1538G Gift Set(二分)

發布時間:2024/4/11 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CodeForces - 1538G Gift Set(二分) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:點擊查看

題目大意:給出 a,b,x,ya,b,x,ya,b,x,y,分別表示有 aaa 個藍色糖果和 bbb 和紅色糖果,現在有兩種打包方式:

  • xxx 個藍色糖果和 yyy 個紅色糖果
  • yyy 個藍色糖果和 xxx 個紅色糖果
  • 問最多能打包多少袋糖果

    題目分析:賽場上寫了個三分死活過不去,死后把三分的范圍改成 100010001000 就過了,然后第二天睡了一覺起來又被 hackhackhack 了,神奇

    假設需要 t1t_1t1? 種第一種打包方式,需要 t2t_2t2? 種第二種打包方式,我們的目標是需要滿足下列不等式的前提下,使得 t1+t2t_1+t_2t1?+t2? 最大

  • t1?x+t2?y<=at_1*x+t_2*y<=at1??x+t2??y<=a
  • t2?x+t1?y<=bt_2*x+t_1*y<=bt2??x+t1??y<=b
  • 比較顯然的一點是,知道 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 得到:

  • k?x+(mid?k)?y<=ak*x+(mid-k)*y<=ak?x+(mid?k)?y<=a
  • k?y+(mid?k)?x<=bk*y+(mid-k)*x<=bk?y+(mid?k)?x<=b
  • 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(二分)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。