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. WCF - 学习总目录

    WCF - 基础 WCF - 地址 WCF - 绑定 WCF - 绑定后续之自定义绑定 WCF - 契约 WCF - 序列化 WCF - 消息 WCF - 实例与会话 WCF - REST服务

  2. Android(java)学习笔记179:BroadcastReceiver之 有序广播和无序广播(BroadcastReceiver优先级)

    之前我们在Android(java)学习笔记178中自定义的广播是无序广播,下面我们要了解一下有序广播:   1.   我们首先了解一下有序广播和无序广播区别和联系? (1) 有序广播> 接受者 ...

  3. springMvc解决json中文乱码

    springMvc解决json中文乱码 springMvc解决json中文乱码,springMvc中文乱码,spring中文乱码 >>>>>>>>> ...

  4. 深入探讨C#中Socket一次性搞定消息发送

    转载自:http://tech.chinaunix.net/a2010/0909/1101/000001101396.shtml     [IT168 技术文档]最近浏览了几篇有关Socket发送消息 ...

  5. Activity是如何挂载Pargment的Day35

    Activity是如何挂载Pargment的Day35 mebile5.0 1.Fragment优化早上任务 思路 一个主Activity 短信群发器 中秋给朋友发短信,都能够加上他们的姓名,这样他们 ...

  6. c语言学习之基础知识点介绍(七):循环结构

    本节主要介绍循环结构 一.while循环 /* 语法: while(表达式){ //循环体; } 注意:循环变量.循环条件和循环控制语句三者缺一不可. 例如: */ ; //循环变量 ){ //循环条 ...

  7. [uiview animation ...] 这个函数有多少没有认识的可能!翻盘效果 上下左右怎么翻都不怕

    1.自己还想着怎么3d 变形    让一个视图绕x/y 轴线翻转 就这么一句代码 [UIView transitionWithView:self.startButton duration:0.5 op ...

  8. IE6 兼容问题总结

    1 IE6,IE7下设置body{overflow:hidden;}失效Bug

  9. 回退符\b

    回退符\b #include <stdio.h> int main(){ printf("hello\b"); getchar(); getchar(); ; } 实验 ...

  10. 冒泡排序(C++)

    冒泡排序(C++) 冒泡排序(C++): 原理是临近的数字两两进行比较,按照从小到大或者从大到小的顺序进行交换, 这样一趟过去后,最大或最小的数字被交换到了最后一位, 然后再从头开始进行两两比较交换, ...