MAX Average Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7574    Accepted Submission(s): 1667

Problem Description
Consider a simple sequence which only contains positive integers as a1, a2 ... an, and a number k. Define ave(i,j) as the average value of the sub sequence ai ... aj, i<=j. Let’s calculate max(ave(i,j)), 1<=i<=j-k+1<=n.
 
Input
There multiple test cases in the input, each test case contains two lines. The first line has two integers, N and k (k<=N<=10^5). The second line has N integers, a1, a2 ... an. All numbers are ranged in [1, 2000].
 
Output
For every test case, output one single line contains a real number, which is mentioned in the description, accurate to 0.01.
 
Sample Input
10 6
6 4 2 10 3 8 5 9 4 1
 
Sample Output
6.50
 

题解:

http://www.docin.com/p-47950655.html这篇论文讲的斜率优化,讲的很清楚;

给定一个长度为n的序列,从其中找连续的长度大于m的子序列使得子序列中的平均值最小。

总结就是:构造下凸折线,维护下凸折线(凸包维护),找与下凸折线相切的斜率(也可以用二分来找)

没用二分:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x) scanf("%lf",&x)
#define P_ printf(" ")
typedef long long LL;
const int MAXN=1e5+;
int sum[MAXN],s[MAXN],a[MAXN];
bool cross(int i,int j,int k){
if((sum[j]-sum[i])*(k-i)>=(sum[k]-sum[i])*(j-i))return true;
return false;
}
double flx(int i,int t){
double temp;
temp=1.0*(sum[t]-sum[i])/(t-i);
return temp;
}
int main(){
int N,k;
while(~scanf("%d%d",&N,&k)){
sum[]=;
for(int i=;i<=N;i++)SI(a[i]),sum[i]=sum[i-]+a[i];
int top=,low=;
double ans=;
for(int i=k;i<=N;i++){
int j=i-k;
while(top-low>=&&cross(s[top-],s[top],j))top--;
s[++top]=j;
while(top-low>=&&flx(s[low+],i)>=flx(s[low],i))low++;
ans=max(ans,flx(s[low],i));
}
printf("%.2lf\n",ans);
}
return ;
}

用了二分:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x) scanf("%lf",&x)
#define P_ printf(" ")
typedef long long LL;
const int MAXN=1e5+;
int sum[MAXN],s[MAXN],a[MAXN];
bool cross(int i,int j,int k){
if((sum[j]-sum[i])*(k-i)>=(sum[k]-sum[i])*(j-i))return true;
return false;
}
double flx(int i,int t){
double temp;
temp=1.0*(sum[t]-sum[i])/(t-i);
return temp;
}
int erfen(int l,int r,int i){
int mid;
while(l<=r){
mid=(l+r)>>;
if(cross(s[mid],s[mid+],i))r=mid-;
else l=mid+;
}
return r+;
}
int main(){
int N,k;
while(~scanf("%d%d",&N,&k)){
sum[]=;
for(int i=;i<=N;i++)SI(a[i]),sum[i]=sum[i-]+a[i];
int top=,low=;
double ans=;
for(int i=k;i<=N;i++){
int j=i-k;
while(top-low>=&&cross(s[top-],s[top],j))top--;
s[++top]=j;
ans=max(ans,flx(s[erfen(,top,i)],i));
}
printf("%.2lf\n",ans);
}
return ;
}

MAX Average Problem(斜率优化dp)的更多相关文章

  1. hdu 2993 MAX Average Problem(斜率DP入门题)

    题目链接:hdu 2993 MAX Average Problem 题意: 给一个长度为 n 的序列,找出长度 >= k 的平均值最大的连续子序列. 题解: 这题是论文的原题,请参照2004集训 ...

  2. UVALive 4726 Average ——(斜率优化DP)

    这是第一次写斜率优化DP= =.具体的做法参照周源论文<浅谈数形结合思想在信息学竞赛中的应用>.这里仅提供一下AC的代码. 有两点值得注意:1.我这个队列的front和back都是闭区间的 ...

  3. HDU 2993 - MAX Average Problem - [斜率DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 Consider a simple sequence which only contains p ...

  4. HDU 2993 MAX Average Problem dp斜率优化

    MAX Average Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  5. HDU 2993 MAX Average Problem(斜率优化DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给定一个长度为n(最长为10^5)的正整数序列,求出连续的最短为k的子序列平均值的最大 ...

  6. BNUOJ 3958 MAX Average Problem

    MAX Average Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Jav ...

  7. [BZOJ3156]防御准备(斜率优化DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3156 分析: 简单的斜率优化DP

  8. HDU 3045 Picnic Cows(斜率优化DP)

    Picnic Cows Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. 蒟蒻关于斜率优化DP简单的总结

    斜率优化DP 题外话 考试的时候被这个玩意弄得瑟瑟发抖 大概是yybGG的Day4 小蒟蒻表示根本不会做..... 然后自己默默地搞了一下斜率优化 这里算是开始吗?? 其实我讲的会非常非常非常简单,, ...

随机推荐

  1. 一步一步学python(五) -条件 循环和其他语句

    1.print 使用逗号输出 - 打印多个表达式也是可行的,但要用逗号隔开 >>> print 'chentongxin',23 SyntaxError: invalid synta ...

  2. mobile端

    1.技术解决方案--------->(widget/event/ajax)->function->data------>XMLHttpRequest----->Serve ...

  3. C#中使用MATLAB

    原文 http://www.cnblogs.com/sorex/archive/2012/08/01/2617469.html 闲来无聊写篇文章聊以慰藉. 本文写了Matlab的2种基本调用方式,且同 ...

  4. Inno Setup:卸载时判断要调用的dll是否存在

    原文 http://zwkufo.blog.163.com/blog/static/2588251201072581947474/ [Code]function SuiteRemovedAlert2: ...

  5. (七)boost库之单例类

    (七)boost库之单例类 一.boost.serialzation的单件实现 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类.通过单例模式可以保证系统中一个类只有一 ...

  6. Unix/Linux环境C编程入门教程(34) 编程管理系统中的用户

    1.用户管理相关函数介绍 geteuid(取得有效的用户识别码) 相关函数 getuid,setreuid,setuid 表头文件 #include<unistd.h> #include& ...

  7. 全国计算机等级考试二级教程-C语言程序设计_第2章_C程序设计的初步知识

    正负号与被除数一致. 3 % (-5) == 3 (-3) % 5 == -3 不用求余运算符,求出余数. int x, y; 答:x - x / y * y; const int i = 10; c ...

  8. javascript第十三课:Json

    js中的json就是字典,Dictionary,就是字典的简化创建方式,json的遍历使用for in的方式,进行遍历 遍历复杂json格式 (如果数组里面存储的是键值对的话,字符串最好用双引号) v ...

  9. linux系统关机与重新启动命令

    在linux下关机和重新启动系统有shutdown.halt.reboot.init,对于他们来说他们的内部工作过程是不同样的. 1.shutdown命令 使用它能够安全地关闭系统.然而在关闭系统时. ...

  10. ViewState存储到服务器

    把viewstate保存在服务器上 将ViewState持久化保持在服务器端的代码,这样ViewState不占用网络带宽,因此其存取只是服务器的磁盘读取时间.并且它很小,可以说是磁盘随便转一圈就能同时 ...