[斜率优化] codefores 660F. Bear and Bowling 4
来源:互联网 发布:淘宝店铺号及二维码 编辑:程序博客网 时间:2024/06/05 02:32
F. Bear and Bowling 4
题意:
给一个序列
简单的说就是可以去掉这个序列的某前缀和某后缀,然后对新得到的
题解:
斜率优化,这个blog前面讲的不错。
花了两天才完全搞懂这个题。
怎么得出来的呢,我们一步一步来。
首先令
然后我们就可以表示出任意
注意到里面
这样可以得到一个
下面来优化。
设有任意三点
过程略,可以自行验证。
为了看起来简单,令
最初我推出的是这个式子,然而我用这个去维护却无法ac,后来我把左边完全化为斜率形式:
显然
前面我们假设
结论就是,对于任意固定的
然而这样还没有得出如何优化,只得到了一个判断谁更优的方法。
同样假设
如果
如果
结论就是,如果存在
所以我们去除所有这样的
本来对于一个
根据斜率来看,也就是任意三个点
这种优化叫做斜率优化,它和几何斜率密切相关,膜一发CDQ女神。
现在对于一个
因为
根据我们维护的斜率的单调性,有一种二分的方法。
假设
于是二分的时候,计算对于一个
满足,说明
不满足,说明
二分的正确性在于我们已经维护好了
容易发现我们是在解集里求一个极值点
显然求极值同样可以采用三分法。
到这里,此题已经算是解决了,可喜可贺,收获颇丰。
再加一个斜率优化DP的题目:here
附代码:
#include<stdio.h>#include<algorithm>using std::max;typedef long long ll;const int N = 2e5+5;ll val[N], sum[N] = {0}, p[N] = {0};int q[N], top, tail;inline ll y(int x){ return p[x] - x*sum[x]; }double g(int j, int k){ double dy = y(j) - y(k); double dx = j - k; return dy/dx;}inline ll getans(int i, int j){ return p[i] - p[j] - j*(sum[i] - sum[j]);}int solve(ll x){ int l = top, r = tail-1, mid, res = l; while(l <= r){ //根据斜率二分求最优点 mid = (l+r) >> 1; if(g(q[mid], q[mid-1]) < -x) l = mid+1, res = mid; else r = mid-1; } return q[res];}int main(){ int n; scanf("%d", &n); for(int i = 1; i <= n; ++i){ scanf("%lld", val+i); sum[i] = sum[i-1] + val[i]; p[i] = p[i-1] + i*val[i]; } top = tail = 0; q[tail++] = 0; ll ans = 0; for(int i = 1; i <= n; ++i){ int j = solve(sum[i]); //对于固定的i,二分求最优点 ans = max(ans, getans(i,j)); //更新答案 while(top < tail-1 && g(i, q[tail-1]) < g(q[tail-1], q[tail-2])) tail--; //满足了g(i,j)<g(j,k) q[tail++] = i; } printf("%lld\n", ans);}
- [斜率优化] codefores 660F. Bear and Bowling 4
- Codeforces 660F:Bear and Bowling 4
- CodeForces 660F Bear and Bowling 4
- CodeForces 660F Bear and Bowling 4(斜率DP+二分)
- F. Bear and Bowling 4 ★ ★ ★
- 【codeforces】660F. Bear and Bowling 4【分治+维护凸壳】
- Codeforces 628F Bear and Fair Set
- CodeForces 573D Bear and Cavalry(线段树优化dp)
- 斜率优化
- 斜率优化
- 斜率优化
- 斜率优化
- 斜率优化
- 斜率优化
- 斜率优化
- 斜率优化
- 斜率优化
- 斜率优化
- 非常使用的日期工具类
- Spark RDD API详解 Map和Reduce
- Photoshop设计网页按钮_非常精致蓝色水晶按钮
- Flask 数据库多对多关系
- React—JSX转换成标准的JavaScript
- [斜率优化] codefores 660F. Bear and Bowling 4
- 【Yii2】Yii2执行完composer install 出现 vendor/bower/jquery/dist 找不到的解决方案
- c++
- Block传值解析
- 简单的文件拷贝工具类
- redis在win10中出现Creating Server TCP listening socket *:6379: bind: No such file or directory
- Spring security 4.1 登录成功后重复进行认证问题
- 安装多个xcode后用cocoapods引用三方报错
- HDU1009 背包+贪心