MAX Average Problem

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

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://blog.sina.com.cn/s/blog_ad1f8960010174el.html
由于把题目数据范围从10^4提到了5*10^5,我用自己出的数据对拍,网上的标程基本爆掉。
只有一点要注意,就是不加读入优化绝对TLE。一般O(n)的题都是这样。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long double real;
#define MAXN 510000
#define max(x,y) ((x)>(y)?(x):(y))
typedef long long qword;
int num[MAXN];
int sum[MAXN];
int n;
real f[MAXN];
real ans=;
struct Point
{
qword x,y;
}pl[MAXN];
int topl=-;
qword xmul(Point p1,Point p2,Point p3)
{
return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
real get_k(Point p1,Point p2)
{
return real(p1.y-p2.y)/(p1.x-p2.x);
}
inline int getInt(int &ret)
{
char ch=getchar();
ret=;
while (ch>''||ch<'')ch=getchar();
while ()
{
ret=ret*+ch-'';
ch=getchar();
if (ch>''||ch<'')break;
}
} int main()
{
freopen("input.txt","r",stdin);
int i,j,k;
while (~scanf("%d%d",&n,&k))
{
Point pp;
for (i=;i<=n;i++)
{
// scanf("%d",&num[i]);
getInt(num[i]);
sum[i]=sum[i-]+num[i];
}
/*
for (i=1;i<=n;i++)
{
for (j=i-k+1;j>=1;j--)
{
ans=max(ans,real(sum[i]-sum[j-1])/(i-j+1));
}
}*/
ans=;
int ptr=-;
int now;
Point pi;
int head=;
topl=-;
for (i=;i<=n;i++)
{
now=i-k;
if (now<)continue;
pi.x=i;pi.y=sum[i];
pp.x=now;
pp.y=sum[now];
if (topl<=head)
{
pl[++topl]=pp;
}else
{
while (topl>=head+&&xmul(pl[topl-],pl[topl],pp)<=)
{
topl--;
}
pl[++topl]=pp;
}
while (ptr==-||(ptr<topl&&get_k(pl[ptr],pi)<=get_k(pl[ptr+],pi)))
{
ptr++;
head=ptr;
}
if (ans<get_k(pl[ptr],pi))
{
--++i;
}
ans=max(ans,get_k(pl[ptr],pi));
}
printf("%.2lf\n",(double)ans);
}
return ;
}

HDU 2993 MAX Average Problem dp斜率优化的更多相关文章

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

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

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

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 Problem Description Consider a simple sequence w ...

  3. HDU 2993 MAX Average Problem(斜率DP经典+输入输出外挂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给出n,k,给定一个长度为n的序列,从其中找连续的长度大于等于k的子序列使得子序列中的 ...

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

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

  5. MAX Average Problem(斜率优化dp)

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

  6. 数据结构:HDU 2993 MAX Average Problem

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

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

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

  8. HDU 3507 Print Article(DP+斜率优化)

     Print Article Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others) ...

  9. HDU 3669 [Cross the Wall] DP斜率优化

    问题分析 首先,如果一个人的\(w\)和\(h\)均小于另一个人,那么这个人显然可以被省略.如果我们将剩下的人按\(w[i]\)递增排序,那么\(h[i]\)就是递减. 之后我们考虑DP. 我们设\( ...

随机推荐

  1. NuGet的使用心得

    前言 上星期发布了NuGet的使用和服务搭建后,同时NuGet在部门里也使用了起来.经过这些天的使用,总结了些小技巧和注意点,希望和大家分享下. 问题提出 使用了NuGet的朋友们估计都知道,在签入代 ...

  2. hadoop错误org.apache.hadoop.yarn.exceptions.YarnException Unauthorized request to start container

    错误: 14/04/29 02:45:07 INFO mapreduce.Job: Job job_1398704073313_0021 failed with state FAILED due to ...

  3. window mac iPhone 三种比较相似的字体

    win: 华文黑体 mac:stheiti iphone:heitisc-light heitisc-medium

  4. 利用反射把数据库查询到的数据转换成Model、List(改良版)

    之前也写过一篇这样的博文,但是非常的粗糙.    博文地址 后来看到了一位前辈(@勤快的小熊)对我的博文的评论后,让我看到了更加优雅的实现方式,于是重构了之前的代码. public static Li ...

  5. Android开发之适配器-ListView适配器的重复数据

    适配器是Android中的数据与View视图的桥梁,作用就是将数据通过适配器显示到对应的View视图上. 工作中,在用ListView做适配器数据时候,有些人肯定碰见过,如何优化效率,但是又出现重复数 ...

  6. Mybaitis配置总结

    在mybatis-config.xml中配置分页插件,插件配置必须放在mapper前面 <plugins> <plugin interceptor="com.rrtong. ...

  7. HTML语言语法大全

    (文章转载至博客园 dodo-yufan) <! - - ... - -> 註解 <!> 跑馬燈 <marquee>...</marquee>普通捲動  ...

  8. WPF Radio组的绑定

    都是控件编,RadioButtion 简单绑定使用,model.cs下边定义属性 private int _isSuccess; public int IsSuccess { get { return ...

  9. php编译安装扩展curl

    ./configure --with-php-config=/opt/software/php5.4/bin/php-configyum install curl curl-devel

  10. DBA 培训相应内容笔记

    014-12-23 slow hang lock 三种常见性能问题 hang;整个数据库不响应,无法创建新连接. hanganalyze输出. dump文件是否有问题信息, 文件是否有其他信息 工具: ...