每沿著網格線走一段貢獻為2,每沿著斜角線走一段貢獻為3,但斜角線比網格線長,令a = min(w, d), b = sqrt(w * w + d * d),答案即為在滿足 a * x + b * y <= π的 情況下,2 * a + 3 * b的最大值。
w和d的范圍只有5,π只有3,所以可以暴力枚舉x和y的值,滿足條件時用 2 * x + 3 * y + 4更新答案即可(4是起始和結束的貢獻)
// p.s. this is wa!!!wa!!!wa!!!wa!!!wa!!!wa!!!wa!!!#include<iostream>#include<algorithm>#include<cmath>#include<cstring>#include<vector>#include<unordered_map>#include<unordered_set>#include<set>#defineendl'\n'#defineIOSios::sync_with_stdio(false); cin.tie(0); cout.tie(0)usingnamespace std;typedeflonglong ll;constdouble pi =acos(-1);intmain(){IOS;int T;cin >> T;while(T --){double w, d;cin >> w >> d;double a =min(w, d), b =sqrt(w * w + d * d);ll ans =4;for(int x =0; x <=100&& a * x <= pi; x ++)// 貼線走ans =max(ans,2* x +(ll)(3*((pi - x * a)/ b))+4);for(int y =0; y <=100&& b * y <= pi; y ++)// 斜線走ans =max(ans,2*(ll)((pi - y * b)/ a)+3* y +4);cout << ans << endl;}return0;}// ac#include<iostream>#include<algorithm>#include<cmath>#include<cstring>#include<vector>#include<unordered_map>#include<unordered_set>#include<set>#defineendl'\n'#defineIOSios::sync_with_stdio(false); cin.tie(0); cout.tie(0)usingnamespace std;typedeflonglong ll;constdouble pi =acos(-1);intmain(){IOS;int T;cin >> T;while(T --){double w, d;cin >> w >> d;double a =min(w, d), b =sqrt(w * w + d * d);ll ans =4;for(int x =0; x <=100&& a * x <= pi; x ++)// 貼線走ans =max(ans,2* x +3*(ll)(((pi - x * a)/ b))+4);for(int y =0; y <=100&& b * y <= pi; y ++)// 斜線走ans =max(ans,2*(ll)((pi - y * b)/ a)+3* y +4);cout << ans << endl;}return0;}