题目链接:https://vjudge.net/problem/POJ-2018

Best Cow Fences
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 11394   Accepted: 3736

Description

Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field contains a certain number of cows, 1 <= ncows <= 2000.

FJ wants to build a fence around a contiguous group of these fields in order to maximize the average number of cows per field within that block. The block must contain at least F (1 <= F <= N) fields, where F given as input.

Calculate the fence placement that maximizes the average, given the constraint.

Input

* Line 1: Two space-separated integers, N and F.

* Lines 2..N+1: Each line contains a single integer, the number of cows in a field. Line 2 gives the number of cows in field 1,line 3 gives the number in field 2, and so on.

Output

* Line 1: A single integer that is 1000 times the maximal average.Do not perform rounding, just print the integer that is 1000*ncows/nfields. 

Sample Input

10 6
6
4
2
10
3
8
5
9
4
1

Sample Output

6500

Source

题意:

给出一个序列,求一段个数大于等于F的子序列,使得它的(和/个数)最大。

题解:

1.最暴力的做法是:先求出前缀和,再枚举序列的起点终点。时间复杂度为O(n^2),因此不能通过。

2.我们可以把前缀和sum[i]看作是坐标轴的y坐标,个数i看作是坐标轴的x坐标。这样就转化为求:(sum[i]-sum[j])/(i-j)最大,显然这是一个斜率的表达式,因而要求的是最大斜率。

3.根据第2点,我们可以用斜率进行优化:由于求的是最大斜率,因而备选点要维持下凸性。

4.关于每组的个数最少为F的处理,详情在:HDU3045 Picnic Cows

代码如下:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = 1e5+; double sum[MAXN], dp[MAXN];
int head, tail, q[MAXN]; double slope(int i, int j) //斜率
{
return (sum[j]-sum[i])/(j-i);
} int main()
{
int n, F;
while(scanf("%d%d",&n,&F)!=EOF)
{
sum[] = ;
for(int i = ; i<=n; i++)
{
int val;
scanf("%d", &val);
sum[i] = sum[i-] + val;
} double ans = ;
head = tail = ;
q[tail++] = ;
for(int i = F; i<=n; i++)
{
while(head+<tail && slope(q[head],i)<slope(q[head+], i)) head++;
ans = max(ans, slope(q[head],i)); int j = i-F+; //不能直接放i,因为要求了每一组至少为F,故i不能为i+1转移。
while(head+<tail && slope(q[tail-], j)<slope(q[tail-],q[tail-])) tail--;
q[tail++] = j;
} printf("%d\n", (int)(ans*));
}
}

POJ2018 Best Cow Fences —— 斜率优化DP的更多相关文章

  1. POJ-2018 Best Cow Fences(二分加DP)

    Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10174 Accepted: 3294 Desc ...

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

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

  3. bzoj-4518 4518: [Sdoi2016]征途(斜率优化dp)

    题目链接: 4518: [Sdoi2016]征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地 ...

  4. bzoj-1096 1096: [ZJOI2007]仓库建设(斜率优化dp)

    题目链接: 1096: [ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L ...

  5. [BZOJ3156]防御准备(斜率优化DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3156 分析: 简单的斜率优化DP

  6. 【BZOJ-1096】仓库建设 斜率优化DP

    1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3719  Solved: 1633[Submit][Stat ...

  7. [USACO2003][poj2018]Best Cow Fences(数形结合+单调队列维护)

    http://poj.org/problem?id=2018 此乃神题……详见04年集训队论文周源的,看了这个对斜率优化dp的理解也会好些. 分析: 我们要求的是{S[j]-s[i-1]}/{j-(i ...

  8. BZOJ 1010: [HNOI2008]玩具装箱toy 斜率优化DP

    1010: [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再 ...

  9. BZOJ 3156: 防御准备 斜率优化DP

    3156: 防御准备 Description   Input 第一行为一个整数N表示战线的总长度. 第二行N个整数,第i个整数表示在位置i放置守卫塔的花费Ai. Output 共一个整数,表示最小的战 ...

随机推荐

  1. HDU 4081 Qin Shi Huang's National Road System 最小生成树+倍增求LCA

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 Qin Shi Huang's National Road System Time Limit: ...

  2. 【APIO2015】Bali Sculptures

    题目描述 印尼巴厘岛的公路上有许多的雕塑,我们来关注它的一条主干道. 在这条主干道上一共有 $N$ 座雕塑,为方便起见,我们把这些雕塑从 $1$ 到 $N$ 连续地进行标号,其中第 $i$ 座雕塑的年 ...

  3. 线程安全-一个VC下多个网络请求

    一.线程安全变量控制显示隐藏loading框 问题描写叙述: 同一页面有两个异步网络请求,第一个请求開始,loading旋转.第二个请求開始loading旋转.第一个结束,loading停止旋转,但是 ...

  4. Android Design Support Library概览

    尊重劳动成果.转载请注明出处:http://blog.csdn.net/growth58/article/details/47972467 关注新浪微博:@于卫国 邮箱:yuweiguocn@gmai ...

  5. 完美删除vector的内容与释放内存

    问题:stl中的vector容器常常造成删除假象,这对于c++程序员来说是极其讨厌的,<effective stl>大师已经将之列为第17条,使用交换技巧来修整过剩容量.内存空洞这个名词是 ...

  6. Cent OS编译环境安装

    在进行编译的时候发现总是缺少一些编译的包,安装上了一个,却又少了另一个,最后百度出来结果,记录一下: yum install gcc gcc-c++ gcc-g77 flex bison autoco ...

  7. yum安装zabbix监控

    公司的服务器由于没有监控软件监控,最感觉不安全,就开始研究zabbix的安装,最后找到一个最简单的安装方法,在这里记录一下,方便以后的查阅 1.安装zabbix官方的软件配置仓库 rpm -ivh h ...

  8. C#给指定doc文件写入宏

    private void InsertMacro() { Word.Application oWord; Word.Document oDoc; VBIDE.VBComponent oModule; ...

  9. hadoop集群ambari搭建(2)之制作hadoop本地源

    准备好源资源server,我使用之前的一台node4,配置都是1GB内存20GB存储 集群最好的安装方式一定是通过本地源的,假设是公共源,那么网络将会严重影响我们的安装进度.所以制作本地源是每个大数据 ...

  10. kubernetes滚动更新

    系列目录 简介 当kubernetes集群中的某个服务需要升级时,传统的做法是,先将要更新的服务下线,业务停止后再更新版本和配置,然后重新启动并提供服务.如果业务集群规模较大时,这个工作就变成了一个挑 ...