HDU 2993 MAX Average Problem(斜率优化)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993
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].
#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(斜率优化)的更多相关文章
- hdu 2993 MAX Average Problem(斜率DP入门题)
题目链接:hdu 2993 MAX Average Problem 题意: 给一个长度为 n 的序列,找出长度 >= k 的平均值最大的连续子序列. 题解: 这题是论文的原题,请参照2004集训 ...
- HDU 2993 - MAX Average Problem - [斜率DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 Consider a simple sequence which only contains p ...
- HDU 2993 MAX Average Problem dp斜率优化
MAX Average Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- 数据结构:HDU 2993 MAX Average Problem
MAX Average Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 2993 MAX Average Problem(斜率优化DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给定一个长度为n(最长为10^5)的正整数序列,求出连续的最短为k的子序列平均值的最大 ...
- HDU 2993 MAX Average Problem(斜率DP经典+输入输出外挂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给出n,k,给定一个长度为n的序列,从其中找连续的长度大于等于k的子序列使得子序列中的 ...
- MAX Average Problem(斜率优化dp)
MAX Average Problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- BNUOJ 3958 MAX Average Problem
MAX Average Problem Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Jav ...
- HDU 3045 Picnic Cows(斜率优化DP)
Picnic Cows Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
随机推荐
- 面向对象之virtual
1.父类声明一个虚方法,子类可以对其进行重写(也可以不重写) 2.虚方法必须有方法体,抽象方法必须没有方法体 3.虚方法可以出现在抽象类中,抽象方法必须出现在抽象类中
- Oracle客户端显示乱码解决
1.查找当前服务器的字符集: 2.使用查询得到的结果集,设置本地local字符集:(不要照抄图片)
- 用httpPost对JSON发送和接收
HTTPPost发送JSON: private static final String APPLICATION_JSON = "application/json"; private ...
- JeeSite是基于多个优秀的开源项目,高度整合封装而成的高效,高性能,强安全性的 开源 Java EE快速开发平台
JeeSite本身是以Spring Framework为核心容器,Spring MVC为模型视图控制器,MyBatis为数据访问层, Apache Shiro为权限授权层,Ehcahe对常用数据进行缓 ...
- EF扩展库(批量操作)
EF删除和修改数据只能先从数据库取出,然后再进行删除 delete from Table1 where Id>5; update Table1 set Age=10; 我们需要这样操作 //删除 ...
- EntityFramework执行SQL语句
在EF中执行Sql语句. using (var context = new EFRecipesEntities()) { string sql = @"insert into Chapter ...
- [LeetCode]题解(python):094 Binary Tree Inorder Traversal
题目来源 https://leetcode.com/problems/binary-tree-inorder-traversal/ iven a binary tree, return the ino ...
- [LeetCode]题解(python):049-Groups Anagrams
题目来源 https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For ...
- 关于action script与js相互调用的Security Error问题
大家都知道,as和js相互调用可以通过ExternalInterface.call和ExternalInterface.addCallback来进行. 比较好的做法是使用之前通过ExternalInt ...
- Java 实现网站当前在线用户统计
1. import java.util.HashSet; import javax.servlet.ServletContext; import javax.servlet.http.HttpSess ...