#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的更多相关文章

  1. Codeforces 643C Levels and Regions 斜率优化dp

    Levels and Regions 把dp方程列出来, 把所有东西拆成前缀的形式, 就能看出可以斜率优化啦. #include<bits/stdc++.h> #define LL lon ...

  2. DP的优化总结

    一.预备知识 \(tD/eD\) 问题:状态 t 维,决策 e 维.时间复杂度\(O(n^{e+t})\). 四边形不等式: 称代价函数 w 满足凸四边形不等式,当:\(w(a,c)+w(b,d)\l ...

  3. DP 优化方法大杂烩 & 做题记录 I.

    标 * 的是推荐阅读的部分 / 做的题目. 1. 动态 DP(DDP)算法简介 动态动态规划. 以 P4719 为例讲一讲 ddp: 1.1. 树剖解法 如果没有修改操作,那么可以设计出 DP 方案 ...

  4. Codeforces 526E Transmitting Levels

    http://codeforces.com/contest/526/problem/E 题意:给一个环,每个点有权值,每次给一个数B,求把这个环切割成若干部分,每个部分不超过B,至少要切成几块? #i ...

  5. 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 ...

  6. Codeforces Round #354 (Div. 2)-B

    B. Pyramid of Glasses 题目链接:http://codeforces.com/contest/676/problem/B Mary has just graduated from ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #284 (Div. 2)

    题目链接:http://codeforces.com/contest/499 A. Watching a movie You have decided to watch the best moment ...

随机推荐

  1. Gin框架04:趣谈参数绑定与校验

    导读 在第二节,我们学习了Gin框架的路由定义与参数接收,今天应一位同学的要求,来讲解一下参数的绑定与校验. 为什么校验参数? 本不必抛出这个问题的,但顾及到初出茅庐的同学,这里解释一下. 假设做一个 ...

  2. stm32:实现呼吸灯

    1.main.c #include "sys.h" #include "delay.h" #include "key.h" #define ...

  3. Win32程序:与"LPCWSTR"类型的形参不兼容

    出现该问题的原因是通常手动输入的字符串都是LPCSTR类型的, 解决办法如下: 在工程处右键,属性-常规-字符集,将Unicode字符集改为为多字节字符集,应用并确认即可.   字符串常量报错: 在常 ...

  4. 掌握使用gitlab ci构建Android包的正确方式

    最近公司在做移动端的项目,自然而然的需要搭建打包的环境.本来计划用Jenkins的,但是发现在gitlab上创建完项目后,提示去配置pipeline,于是决定用gitlab去尝试下,毕竟我觉得Jenk ...

  5. Bitmap之内存缓存和磁盘缓存详解

    原文首发于微信公众号:躬行之(jzman-blog) Android 中缓存的使用比较普遍,使用相应的缓存策略可以减少流量的消耗,也可以在一定程度上提高应用的性能,如加载网络图片的情况,不应该每次都从 ...

  6. Hadoop(十一):组合任务概述和格式

    组合任务概述 一些复杂的任务很难由一个MR处理完成,所以一般需要将其拆分成为多个简单的MR子任务来执行. MapReduce框架中对于这类的问题提供了几种方式进行任务执行流程的控制,主要包括以下几种方 ...

  7. Linux网络安全篇,进入SELinux的世界(二)

    一.简单的网页制作 1.启动httpd服务 /etc/init.d/httpd start 2.编写首页网页文件 echo "hello,this is my first webPage&q ...

  8. 【第一章】黎姿的python学习笔记

  9. "为文本添加下划线"组件:<u> —— 快应用组件库H-UI

     <import name="u" src="../Common/ui/h-ui/text/c_tag_underline"></impor ...

  10. AJ整理问题之:copy,对象自定义copy 什么是property

    AJ分享,必须精品 copy copy的正目的 copy 目的:建立一个副本,彼此修改,各不干扰 Copy(不可变)和MutableCopy(可变)针对Foundation框架的数据类型. 对于自定义 ...