2021暑期牛客8-K Yet Another Problem About Pi

2021暑期牛客8-K Yet Another Problem About Pi

思路

solution中讲的很清晰了,除了最后那个显然(

这里只证明一下那个显然的内容

在不等式 (ax+byleq pi) 下最大化 (2x+3y) ,其中 (a=min(w,d) and b=sqrt{w^2+d^2})​ ,且 (x,y) 为整数

不妨设 (wleq d)​ ,那么 (a=w and b=sqrt{w^2+d^2} geq sqrt2w)

考虑一个值 (1.5) ,当 (b=1.5w) 时,我们用 3 个 a 可以交换得到 2 个 b ,且答案不变

那么,当 (b>1.5w)​​ 时,用 a 交换 b 会使答案更劣,所以将交换反向答案就会变得更优

而当 (b<1.5w)​ 时,用 a 交换 b 会使答案更优

所以,无论什么情况下,最优解一定满足 (x<3) 或者满足 (y<2)

因为当 (b>1.5w) 时,将 2 个 b 换成 大于 3 个 a 显然会使答案更优,所以此时 (y<2)

另外同理

代码

#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

#define ll long long
#define ull unsigned long long
#define cint const int&

const int mod = 1e9+7;
const int inf_int = 0x7fffffff;
const ll inf_ll = 0x7fffffffffffffff;
const double ept = 1e-9;

int t;
long double pi = acos(-1);
long double w, d;

int main() {
    cin >> t;
    while(t--) {
        int ans = 0;
        cin >> w >> d;
        long double r = sqrt(w*w+d*d);
        w = min(w, d);
        for(int i=0; i<=3; i++) {
            if(w*i < pi) {
                int st = (pi-w*i)/r;
                ans = max(ans, 2*i+3*st);
            }
            if(r*i < pi) {
                int st = (pi-r*i)/w;
                ans = max(ans, 2*st+3*i);
            }
        }
        cout << ans << endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/ullio/p/15139635.html