题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993

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.
 
题目大意:给n个数和k,求数的个数大于等于k的子段的最大平均值。
思路:可以去看IOI国家集训队论文:《浅谈数形结合思想在信息学竞赛中的应用》——周源
也可以直接看这个http://blog.sina.com.cn/s/blog_ad1f8960010174el.html
 
PS:这就是传说中的来自数据组数的恶意吗,看上去似乎有100组大数据的感觉……G++超时的可以尝试用C++交,HDU的C++读入比G++快,而且优化的程度也不同。
 
代码(C++ 500MS/G++ 906MS):
 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cctype>
using namespace std;
typedef long long LL; const int MAXN = ; int sum[MAXN];
int n, k; int readint() {
char c = getchar();
while(!isdigit(c)) c = getchar();
int res = ;
while(isdigit(c)) res = res * + c - '', c = getchar();
return res;
} struct Point {
int x, y;
Point() {}
Point(int x, int y): x(x), y(y) {}
Point operator - (const Point &rhs) const {
return Point(x - rhs.x, y - rhs.y);
}
double slope() {
return (double)y / x;
}
}; LL cross(const Point &a, const Point &b) {
return (LL)a.x * b.y - (LL)a.y * b.x;
} LL cross(const Point &o, const Point &a, const Point &b) {
return cross(a - o, b - o);
} Point que[MAXN], p;
int head, tail; double solve() {
double res = ;
head = ; tail = -;
for(int i = k; i <= n; ++i) {
p = Point(i - k, sum[i - k]);
while(head < tail && cross(que[tail - ], que[tail], p) <= ) --tail;
que[++tail] = p; p = Point(i, sum[i]);
while(head < tail && cross(que[head], que[head + ], p) >= ) ++head;
res = max(res, (p - que[head]).slope());
}
return res;
} int main() {
while(scanf("%d%d", &n, &k) != EOF) {
for(int i = ; i <= n; ++i) sum[i] = sum[i - ] + readint();
printf("%.2f\n", solve());
}
}

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

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

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

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

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

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

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

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

    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. HDU 2993 MAX Average Problem(斜率DP经典+输入输出外挂)

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

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

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

  8. BNUOJ 3958 MAX Average Problem

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

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

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

随机推荐

  1. jquery easyui Combobox 实现 两级联动

    具体效果如下图:

  2. VC更换图标文件

    步骤 1. 删除图标 2. 在VC中移除图标 3. 用新图标替换原来图标 4. 添加图标到VC 5. 重新编译 参考文档 http://www.cnblogs.com/BloodAndBone/arc ...

  3. pro10

    1.本次课学习到的知识点: 什么是数组?为什么要使用数组?如何定义数组? 如何引用数组元素? 2. 实验过程中遇到的问题及解决方法: 在最后一个试验中,在输出最大小标那里进行了仔细的思考,刚开始思考应 ...

  4. 物联网操作系统HelloX V1.80测试版发布

    经过HelloX开发团队近半年的努力,在HelloXV1.79版本基础上,增加许多功能特性,并对V1.79版本的一些特性进行了进一步优化之后,正式形成HelloX V1.80测试版本.经相对充分的测试 ...

  5. Windows下MinGW编译Qt4

    还真不是吹,这个问题折磨我一天……前些天转载了一篇关于<Eclipse+Qt开发环境设置>和<Eclipse+MinGW+Qt开发环境设置>两片文章,里面讲述了QT的开源版本q ...

  6. Mars 是微信官方的终端基础组件,是一个使用 C++ 编写的业平台性无关的基础组件

    http://www.oschina.net/p/wechat-mars http://www.oschina.net/news/80453/wewechat-open-source-plan

  7. 执行动态sql返回参数

    ref: https://support.microsoft.com/en-us/kb/262499 ) ) DECLARE @IntVariable INT ) SET @SQLString = N ...

  8. ios证书

    内容提要: 安装app时提示 “无法下载应用,此时无法安装“XXX””.我遇到过多次是由于ios的app出现证书问题.本篇文章讲解用ios证书制作过程,以及每个步骤的解释. 正文: Xcode签名至少 ...

  9. [LeetCode] Letter Combinations of a Phone Number(bfs)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  10. margin负值

    一列li并排的时候,需要一些间距的时候,又不需要最右边或者最左边有间距. <!DOCTYPE html> <html lang="zh-CN"> <h ...