Codeforces 674C Levels and Regions



C. Levels and Regions
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Radewoosh is playing a computer game. There are n levels, numbered 1 through n. Levels are divided into k regions (groups). Each region contains some positive number of consecutive levels.

The game repeats the the following process:

  1. If all regions are beaten then the game ends immediately. Otherwise, the system finds the first region with at least one non-beaten level. Let X denote this region.
  2. The system creates an empty bag for tokens. Each token will represent one level and there may be many tokens representing the same level.
    • For each already beaten level i in the region X, the system adds ti tokens to the bag (tokens representing the i-th level).
    • Let j denote the first non-beaten level in the region X. The system adds tj tokens to the bag.
  3. Finally, the system takes a uniformly random token from the bag and a player starts the level represented by the token. A player spends one hour and beats the level, even if he has already beaten it in the past.

Given nk and values t1, t2, ..., tn, your task is to split levels into regions. Each level must belong to exactly one region, and each region must contain non-empty consecutive set of levels. What is the minimum possible expected number of hours required to finish the game?

Input

The first line of the input contains two integers n and k (1 ≤ n ≤ 200 0001 ≤ k ≤ min(50, n)) — the number of levels and the number of regions, respectively.

The second line contains n integers t1, t2, ..., tn (1 ≤ ti ≤ 100 000).

Output

Print one real number — the minimum possible expected value of the number of hours spent to finish the game if levels are distributed between regions in the optimal way. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 4.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if .

Examples
input
4 2
100 3 5 7
output
5.7428571429
input
6 2
1 2 4 8 16 32
output
8.5000000000
Note

In the first sample, we are supposed to split 4 levels into 2 regions. It's optimal to create the first region with only one level (it must be the first level). Then, the second region must contain other three levels.

In the second sample, it's optimal to split levels into two regions with 3 levels each.


没想到这把cf死在了最擅长的DP上面,代码一复杂,就各种bug,差点没过掉。这个斜率DP本身难度不大。直接开DP[i][j]表示做到第k个group最后一个level是j的最优解。然后转移用斜率DP优化一下。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <time.h>
using namespace std;
#define eps 1e-8
double dp[55][202020];
double sum1[202020];
double sum2[202020];
double S[202020];
int que[55][202020],tail[55],head[55];
int n,k,a[202020];
int from[1010][1010];
double getup(int t,int i,int k)
{
//	printf("%d %d %d--",t,i,k);
//	printf("%.8f %.8f %.8f %.8f %.8f %.8f\n",dp[t][i],dp[t][k],S[i],S[k],sum2[i]*sum1[i],sum2[k]*sum1[k]);
	return dp[t][i]-dp[t][k]-S[i]+S[k]+sum2[i]*sum1[i]-sum2[k]*sum1[k];
	
}
double getdown(int t,int i,int k)
{
	return sum1[i]-sum1[k];
}
double getdp(int t1,int v1,int t2,int v2)
{
	return dp[t1][v1]+S[v2]-S[v1]-(sum2[v2]-sum2[v1])*(sum1[v1]);
}
int main()
{
	clock_t st=clock();
	freopen("1.in","r",stdin);
	scanf("%d%d",&n,&k);
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		sum1[i]=sum1[i-1]+a[i];
		sum2[i]=sum2[i-1]+1.0/a[i];
		S[i]=S[i-1]+1.0/a[i]*sum1[i];
	}
	for(int j=1;j<=n;j++)
	{
		for(int i=1;i<=min(j,k);i++)
		{
			if(i==1)
			{
				dp[i][j]=getdp(0,0,i,j);
				
			}
			else{
				if(i==j)
				{
					que[i][tail[i]++]=j-1;
				}
				while(head[i]+1<tail[i])
				{
					double u1=getup(i-1,que[i][head[i]+1],que[i][head[i]]);
					double d1=getdown(i-1,que[i][head[i]+1],que[i][head[i]]);
			//		printf("%d %d--",que[i][head[i]+1],que[i][head[i]]);
			//		printf("%.8f %.8f %.8f\n",u1,d1,sum2[j]);
					if(u1<d1*sum2[j])
					head[i]++;
					else break;
				}
				dp[i][j]=getdp(i-1,que[i][head[i]],i,j);
				from[i][j]=que[i][head[i]];
				while(head[i]+1<tail[i]){
					double u1=getup(i-1,j,que[i][tail[i]-1]);
					double d1=getdown(i-1,que[i][tail[i]-1],que[i][tail[i]-2]);
					double u2=getup(i-1,que[i][tail[i]-1],que[i][tail[i]-2]);
					double d2=getdown(i-1,j,que[i][tail[i]-1]);
			//		printf("%d %d--",que[i][tail[i]-1],que[i][tail[i]-2]);
			//		printf("%.5f %.5f %.5f %.5f\n",u1,d1,u2,d2);
					if(u1*d1<u2*d2)
					tail[i]--;
					else break;
				}
			}
			que[i][tail[i]++]=j;
		}
	}

	printf("%.10f\n",dp[k][n]);
	printf("%dms\n",clock()-st);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值