您的位置:首页 > 其它

Codeforces 674C Levels and Regions

2016-05-08 02:59 429 查看
C. Levels and Regionstime limit per test3 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputRadewoosh 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: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 denotethis region.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 tokensto the bag (tokens representing the i-th level).Let j denote the first non-beaten level in the region X.The system adds tj tokensto the bag.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 n, k andvalues 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?InputThe first line of the input contains two integers n and k (1 ≤ n ≤ 200 000, 1 ≤ 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).OutputPrint 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.Examplesinput
4 2100 3 5 7
output
5.7428571429
input
6 21 2 4 8 16 32
output
8.5000000000
NoteIn 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-8double 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]);printf("%dms\n",clock()-st);}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: