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. (Inno setup打包)检测系统是否已安装程序,若已安装则弹出卸载提示的代码

    原文 http://bbs.itiankong.com/thread-30983-1-5.html 有6天没研究pascal代码了,昨天晚上突然来了灵感,终于解决了苦思冥想好几天没能解决的问题, 因此 ...

  2. 并发(Concurrency)和并行(Parallelism)的区别

    最近在读<real world haskell>里关于并行的一章时,看到作者首先对并发(Concurrency)和并行(Parallelism)的区别进行了定义和解释.以前我对这个问题也是 ...

  3. logstash 处理nginx 访问日志

    [root@dr-mysql01 frontend]# cat logstash_frontend.conf input { file { type => "zj_frontend_a ...

  4. linux之模拟简单登录的脚本

    脚本如下: 运行结果:

  5. #include <hash_set>

    哈希查找,不需要排序,适用于精确查找,比二分查找更快 #define _SILENCE_STDEXT_HASH_DEPRECATION_WARNINGS #include <iostream&g ...

  6. Python OpenGL学习(1): 环境配置及错误篇

    系统环境是:Ubuntu 14.04 个人首次接触OpenGL,学到哪就写到哪. 1.模块安装: sudo apt-get install python-openglpip install PyOpe ...

  7. ExecuteReader(),ExecuteNonQuery(),ExecuteScalar(),ExecuteXmlReader()之间的区别

    本文来自:http://www.cnblogs.com/zhouxiaxue/archive/2006/05/12/398266.html http://www.cnblogs.com/yaoxc/a ...

  8. ROW_NUMBER() OVER函数的基本用法用法【转】

    语法:ROW_NUMBER() OVER(PARTITION BY COLUMN ORDER BY COLUMN) 简单的说row_number()从1开始,为每一条分组记录返回一个数字,这里的ROW ...

  9. google 推荐 android 像素统一使用dip,字体统一使用sp

    像素统一使用dip,字体统一使用sp

  10. 【字母树+贪心】【HDU3460】【Ancient Printer】

    题目大意: 一个打印机 只有 打印,删除,a-z.操作 给你一堆队名,如何才能操作次数最少输出全部 (字典树节点数-1)*2 输入,删除操作数 字符串数 printf操作数 最长字符串的长度 最后一个 ...