题目链接: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. pro7

    1.本次课学习到的知识点: 函数的作用 确定函数的功能 定义函数 调用函数 2.实验过程中遇到的问题及解决方法: 定义函数时 变量的定义会出现混乱 通过看例题 多练习 逐渐熟悉 需从数学角度解决问题时 ...

  2. QTextCodec::makeDecoder函数,plugins需要是动态链接库

    QT中的QString内容使用Unicode作为文本编码.但是实际系统中通常采用的是其他编码,例如GBK,utf8等.为了便于兼容这些格式,QT中还设置了两个字符串类型: QCString类: C类型 ...

  3. Qt Focus事件,FocusInEvent()与FocusOutEvent()

    描述:一开始我要实现的目的就是,在一个窗体上有多个可编辑控件(比如QLineEdit.QTextEdit等),当哪个控件获得焦点,哪个控件的背景就高亮用来起提示作用,查了下文档应该用focusInEv ...

  4. 《Haskell趣学指南 Learn You a Haskell for Great Good!》-代码实验

    doubleMe x = x + x doubleUs x y = doubleMe x + doubleMe y doubleSmallNumber x = then x else x * doub ...

  5. 设计模式:桥连模式(Bridge)

    定   义:将抽象部分和它的实现部分分离,使它们可以独立的变化. 结构图: 实现类: //Implementor(实现)类 public abstract class Implementor { pu ...

  6. 集合类(Objective-C & Swift)

    内容提要: 本文前两部分讲了Cocoa的集合类和Swift的集合类,其中Cocoa提供的集合类包括NSArray.NSMutableArray.NSDictionary.NSMutableDictio ...

  7. (leetcode)Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  8. sqlserver总结-视图及存储过程

    视图中不能声明变量,不能调用存储过程,如果写比较复杂的查询,需要应用存储过程 视图也可以和函数结合 存储过程通过select或其他语句返回结果集 除此之外,存储过程返回结果只有两种方式 1 retur ...

  9. ibatis传入数组或List

    小结一下ibatis框架下,传入参数为数组类型或者是List类型的sql写法.标签里面都不需要表名 1.传入字符串数组,不需要标明parameterClasss,数组和List类型对象一样都可以用&l ...

  10. SQL server 2012

    MICROSOFT SQL SERVER 2012 企业核心版激活码序列号: FH666-Y346V-7XFQ3-V69JM-RHW28MICROSOFT SQL SERVER 2012 商业智能版激 ...