CodeForces 674C Levels and Regions
#include<bits/stdc++.h>
using namespace std;
const int maxn=2e5+5;
int N,K,head,tair;
int q[maxn];
double S[maxn],A[maxn],B[maxn],dp[maxn][55];
double calc(int i,int j,int k){return dp[k][j-1]+A[i]-A[k]-S[k]*(B[i]-B[k]);}
double x(int k){return S[k];}
double y(int k,int j){return dp[k][j-1]-A[k]+S[k]*B[k];}
bool judge(int a,int b,int c,int j){return (x(a)-x(c))*(y(a,j)-y(b,j))>=(y(a,j)-y(c,j))*(x(a)-x(b));}
int main()
{
while(~scanf("%d%d",&N,&K))
{
S[0]=A[0]=B[0]=0;
for(int i=1;i<=N;++i)
{
double t;scanf("%lf",&t);
S[i]=S[i-1]+t;
B[i]=B[i-1]+1/t;
A[i]=A[i-1]+S[i]/t;
}
//printf("start\n");
for(int i=1;i<=N;++i)dp[i][1]=A[i];
for(int j=2;j<=K;++j)
{
q[head=tair=1]=j-1;
for(int i=j;i<=N;++i)
{
while(head<tair&&calc(i,j,q[head])>=calc(i,j,q[head+1]))head++;
dp[i][j]=calc(i,j,q[head]);
while(head<tair&&judge(q[tair-1],q[tair],i,j))tair--;
q[++tair]=i;
}
}
printf("%.6f\n",dp[N][K]);
}
}
CodeForces 674C Levels and Regions的更多相关文章
- Codeforces 643C Levels and Regions 斜率优化dp
Levels and Regions 把dp方程列出来, 把所有东西拆成前缀的形式, 就能看出可以斜率优化啦. #include<bits/stdc++.h> #define LL lon ...
- DP的优化总结
一.预备知识 \(tD/eD\) 问题:状态 t 维,决策 e 维.时间复杂度\(O(n^{e+t})\). 四边形不等式: 称代价函数 w 满足凸四边形不等式,当:\(w(a,c)+w(b,d)\l ...
- DP 优化方法大杂烩 & 做题记录 I.
标 * 的是推荐阅读的部分 / 做的题目. 1. 动态 DP(DDP)算法简介 动态动态规划. 以 P4719 为例讲一讲 ddp: 1.1. 树剖解法 如果没有修改操作,那么可以设计出 DP 方案 ...
- Codeforces 526E Transmitting Levels
http://codeforces.com/contest/526/problem/E 题意:给一个环,每个点有权值,每次给一个数B,求把这个环切割成若干部分,每个部分不超过B,至少要切成几块? #i ...
- Codeforces Round #284 (Div. 2)A B C 模拟 数学
A. Watching a movie time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #354 (Div. 2)-B
B. Pyramid of Glasses 题目链接:http://codeforces.com/contest/676/problem/B Mary has just graduated from ...
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分
C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...
- Codeforces Beta Round #6 (Div. 2 Only) D. Lizards and Basements 2 dp
题目链接: http://codeforces.com/problemset/problem/6/D D. Lizards and Basements 2 time limit per test2 s ...
- Codeforces Round #284 (Div. 2)
题目链接:http://codeforces.com/contest/499 A. Watching a movie You have decided to watch the best moment ...
随机推荐
- Python学习笔记:迭代器(Iterator)详解
一.可迭代的对象(Iterable) 1.定义:可以直接用在循环的数据类型,如list,tuple,dict,set,str,还有generator(生成器), 和带yield的函数,这些直接可以用在 ...
- Pytest系列(9) - 参数化@pytest.mark.parametrize
如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 pytest允许在多个级别启 ...
- 项目踩坑实记 :2019年(SSM 架构)
1.Bootstarp 相关 JS 结合 Bootstarp 初始化表格后,如果是 Ajax 请求获得返回数据,重新渲染数据到表格的话,用下面的函数. ChanInfTable 是表格的 id. 2. ...
- ubuntu上安装lamp环境命令清单
#install configuration manager sudo apt-get install tasksel #install basic lamp stack sudo tasksel i ...
- django中设置定时任务
django中设置定时任务 在django中设置定时任务我们可以借用django-crontab这个第三包来实现 django-crontab只能在linux系统下使用 安装: pip install ...
- vue-shop项目第一天(用于记录 个人学习)
vue-shop 第一天 一.项目初始化 1.安装vuecli脚手架(依赖于webpack)[前端自动构建工具]. 2.安装插件(element-ui)[第三方插件库], 安装依赖(axios)[调用 ...
- <E> 泛型
/* * 使用集合存储自定义对象并遍历 * 由于集合可以存储任意类型的对象,当我们存储了不同类型的对象,就有可能在转换的时候出现类型转换异常, * 所以java为了解决这个问题,给我们提供了一种机制, ...
- shell 中获取子字符串的正确姿势
前言 shell 取子串的方式有点特别,你写的匹配字符串是需要从头开始匹配的,第一个匹配到了才开始匹配下一个,这个类似于python中的match的工作方式. 1,获取子串有两种方式 使用字符串匹配的 ...
- IDEA默认KeyMap映射快捷键
编辑 快捷键 描述 Ctrl + 空格 基础代码补全(任意类.方法.变量的名字) Ctrl + Shift + 空格 智能代码补全(过滤期望类型的方法和变量列表) Ctrl + Shift + 回车 ...
- Jquery+php鼠标滚动到页面底部自动加载更多内容,使用分页
1.index.php <style type="text/css"> #container{margin:10px auto;width: 660px; border ...