三分-abc279d

By xiaruize

前置知识 三分

俗话说:二分不行就三分

那么,三分是什么?

如果你不会二分,请点击链接跳转

二分需要一个序列满足单调性,那么,如果是一个单峰函数,如何求极值呢?

f(x)f(x)x=ax=a 时取到 maxmaxbb

l,rl,r 表示当前的两端,l<a,r>al<a,r>a

取区间 [l,r][l,r] 的三等分点 lmid,rmidlmid,rmid

l=f(lmid),r=f(rmid)l'=f(lmid),r'=f(rmid)

l<rl'<r', lmid<almid < a

l>rl'>r', rmid>armid > a

对应的收缩区间

abc279_d

显然,这题是一个单峰函数,所以可以三分

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Problem: D - Freefall
// Contest: AtCoder - TOYOTA SYSTEMS Programming Contest 2022(AtCoder Beginner Contest 279)
// URL: https://atcoder.jp/contests/abc279/tasks/abc279_d
// Memory Limit: 1024 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)

/*
Name:
Author: xiaruize
Date:
*/
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ull unsigned long long
#define ALL(a) (a).begin(), (a).end()
#define pb push_back
#define mk make_pair
#define pii pair<int, int>
#define pis pair<int, string>
#define sec second
#define fir first
#define sz(a) int((a).size())
#define rep(i, x, y) for (int i = x; i <= y; i++)
#define repp(i, x, y) for (int i = x; i >= y; i--)
#define Yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define No cout << "No" << endl
#define NO cout << "NO" << endl
#define debug(x) cerr << #x << ": " << x << endl
#define double long double
const int INF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1000000007;
const int N = 2e5 + 10;

// bool st;
double a, b;
// bool en;

double calc(int x)
{
double g = x + 1;
return 1.0 * a / sqrt(g) + 1.0 * b * x;
}

signed main()
{
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// cerr<<(&en-&st)/1024.0/1024.0<<endl;
// auto t_1=chrono::high_resolution_clock::now();
cin >> a >> b;
int l = 0, r = 1e18;
for (int i = 1; i <= 100000; i++)
{
int lmid = (r - l) / 3 + l, rmid = r - (r - l) / 3;
double ll = calc(lmid), rr = calc(rmid);
// cerr << l << ' ' << r << endl;
if (ll < rr)
r = rmid;
else if (ll > rr)
l = lmid;
else
{
l = lmid;
r = rmid;
}
}
double res = INF;
for (int i = l; i <= r; i++)
res = min(res, calc(i));
cout << fixed << setprecision(10) << res << endl;
// auto t_2=chrono::high_resolution_clock::now();
// cout <<". Elapsed (ms): " << chrono::duration_cast<chrono::milliseconds>(t_2 - t_1).count() << endl;
return 0;
}