传送门
题目描述
分析
01分数规划的板子题
我们二分一个
m
i
d
mid
mid
∑
∣
l
−
r
∣
∑
b
=
m
i
d
\frac{\sum \sqrt{\lvert l-r\rvert}}{\sum b} = mid
∑b∑∣l−r∣=mid ,变形可得
∑
∣
l
−
r
∣
=
m
i
d
∗
∑
b
\sum \sqrt{\lvert l-r\rvert} = mid * \sum b
∑∣l−r∣=mid∗∑b
∑
∣
l
−
r
∣
−
m
i
d
∗
∑
b
=
f
(
m
i
d
)
\sum \sqrt{\lvert l-r\rvert} - mid * \sum b = f(mid)
∑∣l−r∣−mid∗∑b=f(mid)
看成函数来处理,
m
i
d
mid
mid显然大于0,所以这是一个单调递减的函数,如果函数值小于
0
0
0,
m
i
d
mid
mid偏大,二分求函数零点即可,因为要求最小值,所以我们希望函数值尽可能小,这样
m
i
d
mid
mid就可以尽量减小,所以可以转化成最短路问题解决
代码
#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 2e5 + 10;
const ll mod= 1000000007;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a){char c=getchar();T x=0,f=1;while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
int gcd(int a,int b){return (b>0)?gcd(b,a%b):a;}
int n;
double m;
double x[N],y[N];
double d[N];
int pre[N];
bool check(double mid){
for(int i = 1;i <= n;i++) d[i] = 1e30;
for(int i = 1;i <= n;i++)
for(int j = 0;j < i;j++){
double t = d[j] + sqrt(abs(x[i] - x[j] - m)) - mid * y[i];
if(d[i] > t){
d[i] = t;
pre[i] = j;
}
}
return d[n] < 0;
}
void out(int x){
if(!x) return;
out(pre[x]);
printf("%d ",x);
}
int main(){
scanf("%d%lf",&n,&m);
for(int i = 1;i <= n;i++) scanf("%lf%lf",&x[i],&y[i]);
double l = 0,r = 1e6;;
while(r - l >= eps){
double mid = (l + r) / 2;
if(check(mid)) r = mid;
else l = mid;
}
out(n);
return 0;
}
/**
* ┏┓ ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃ ┃
* ┃ ━ ┃ ++ + + +
* ████━████+
* ◥██◤ ◥██◤ +
* ┃ ┻ ┃
* ┃ ┃ + +
* ┗━┓ ┏━┛
* ┃ ┃ + + + +Code is far away from
* ┃ ┃ + bug with the animal protecting
* ┃ ┗━━━┓ 神兽保佑,代码无bug
* ┃ ┣┓
* ┃ ┏┛
* ┗┓┓┏━┳┓┏┛ + + + +
* ┃┫┫ ┃┫┫
* ┗┻┛ ┗┻┛+ + + +
*/