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 ...
随机推荐
- 面试官:JavaScript 原始数据类型 Symbol 有什么用?
以前提到 JavaScript 原始数据类型时,我们知道有Number,String,Null,Boolean,Undefined这几种.ES6 引入了新的基本数据类型Symbol和BigInt.今天 ...
- oracle实现分页功能 limit功能例子
oracle数据库不支持mysql中limit功能,但可以通过rownum来限制返回的结果集的行数,rownum并不是用户添加的字段,而是oracle系统自动添加的. 记录两种常用方法: (1)使查询 ...
- rem样板
window.onload=window.onresize=function(){ var rem=document.documentElement.clientWidth*20/320; docum ...
- PXE基础装机环境
PXE基础装机环境 案例1:PXE基础装机环境 案例2:配置并验证DHC ...
- tornado自定义实现django include方法
tornado自定义实现django include方法 自定义URLmethod模块 from Custom.errors import * def include(base_url, expan ...
- Linux bash篇(四 命令)
1.一次执行多个命令 ; eg: ls -al ; touch data.txt 2.根据情况执行命令 && || cmd1 && c ...
- spark rdd元素println
1.spark api主要分两种:转换操作和行动操作.如果在转化操作中println spark打印了 我也看不到. val result = sqlContext.sql(sql) val resu ...
- 【Server】Windows系统安装Tomcat服务器
安装Tomcat服务器 Tomcat服务器地址:https://tomcat.apache.org/download-80.cgi 当前版本点选8以上版本,最新的可能不稳定,所以选8或者9版本 直接解 ...
- unity3d之简单动画
Unity3d中有两个关于动画的概念,Animation和Animator,看一下他们的创建和区别 1.创建一个物体后可以添加Animator和Animation组件如图所示 2.Animation和 ...
- 在数组添加元素时报错:IndexError: list index out of range
今天第一次发随笔还有许多不足之处,欢迎评论!!! 最近在写一个成语接龙的小游戏,结果在数组添加元素时报错:IndexError: list index out of range 源码: import ...