Educational Codeforces Round 88 (Rated for Div. 2) C. Mixing Water ###K ###K //K
题意:给一个杯子 可以倒热水h 和冷水c 必须热冷热冷这样倒,可以倒无限次
问至少倒多少次的时候 温度的总和/倒的次数 最接近给的温度t
题目链接:https://codeforces.ml/contest/1359/problem/C
思路:设出函数考虑单调性 再考虑二分
1 假设倒了x杯热水的时候,冷水也倒了x杯
那么f(x)=(c*x+h*x)/(2*x) 则f(x)=(c+h)/2
所以倒了倒了偶数次的时候温度恒为 (c+h)/2
2.假设冷水倒了x-1杯的时候
那么f(x)=(c*(x-1)+h*x)(x*2-1) 运用求导知识 可以得 导函数为 (c-h)/(2*x-1)^2 可知恒小于0
所以单调递减 把1和正无穷带入函数 可知值域为((c+h)/2,h]
那么就可以利用二分 check 找到合法答案
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 #define ull unsigned long long 5 #define pb push_back 6 const int maxn=2e5+10; 7 const int mod=1e9+7; 8 double h,c,t; 9 double cal(double x) 10 { 11 return (x*h+(x-1)*c)/(2*x-1); 12 } 13 int check(int x) 14 { 15 double t1=cal(x),t2=cal(x-1); 16 if(abs(t1-t)<abs(t2-t)) 17 { 18 return 1; 19 } 20 else 21 return 0; 22 } 23 24 25 int main() 26 { 27 ios::sync_with_stdio(false); 28 cin.tie(0); 29 int q; 30 cin>>q; 31 while(q--) 32 { 33 34 cin>>h>>c>>t; 35 double even=(h+c)/2.0; 36 if(t<=even) 37 { 38 cout<<2<<'\n'; 39 } 40 else 41 { 42 int l=1,r=1e9; 43 int ans=0; 44 while(l<=r) 45 { 46 int mid=(l+r)/2; 47 if(check(mid)) 48 { 49 ans=mid*2-1; 50 l=mid+1; 51 } 52 else 53 r=mid-1; 54 55 } 56 cout<<ans<<'\n'; 57 } 58 } 59 60 61 62 63 64 65 66 }

浙公网安备 33010602011771号