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

USACO 2003 March Green

题意:

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

题解:

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

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

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

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

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <vector>
 6 #include <cmath>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 const int INF = 2e9;
15 const LL LNF = 9e18;
16 const int mod = 1e9+7;
17 const int MAXM = 1e5+10;
18 const int MAXN = 1e5+10;
19
20 double sum[MAXN], dp[MAXN];
21 int head, tail, q[MAXN];
22
23 double slope(int i, int j)  //斜率
24 {
25     return (sum[j]-sum[i])/(j-i);
26 }
27
28 int main()
29 {
30     int n, F;
31     while(scanf("%d%d",&n,&F)!=EOF)
32     {
33         sum[0] = 0;
34         for(int i = 1; i<=n; i++)
35         {
36             int val;
37             scanf("%d", &val);
38             sum[i] = sum[i-1] + val;
39         }
40
41         double ans = 0;
42         head = tail = 0;
43         q[tail++] = 0;
44         for(int i = F; i<=n; i++)
45         {
46             while(head+1<tail && slope(q[head],i)<slope(q[head+1], i))  head++;
47             ans = max(ans, slope(q[head],i));
48
49             int j = i-F+1;  //不能直接放i,因为要求了每一组至少为F,故i不能为i+1转移。
50             while(head+1<tail && slope(q[tail-1], j)<slope(q[tail-2],q[tail-1])) tail--;
51             q[tail++] = j;
52         }
53
54         printf("%d\n", (int)(ans*1000));
55     }
56 }

View Code

转载于:https://www.cnblogs.com/DOLFAMINGO/p/8207520.html

POJ2018 Best Cow Fences —— 斜率优化DP相关推荐

  1. NOI2007 货币兑换 - CDQ分治斜率优化dp

    斜率优化dp维护一个凸壳.如果\(x, y\)坐标都递增,可以用单调队列,如果只有\(x\)递增,可以在凸壳上二分斜率,如果\(x, y\)都不递增,则需要在凸包中插入,可以用平衡树或cdq分治维护. ...

  2. CF-311B Cats Transport(斜率优化DP)

    题目链接 题目描述 小S是农场主,他养了 \(M\)只猫,雇了 \(P\) 位饲养员. 农场中有一条笔直的路,路边有 \(N\) 座山,从 \(1\) 到 \(N\)编号. 第 \(i\) 座山与第 ...

  3. 【洛谷3648】[APIO2014] 序列分割(斜率优化DP)

    点此看题面 大致题意: 你可以对一个序列进行\(k\)次分割,每次得分为两个块元素和的乘积,求总得分的最大值. 区间\(DPor\)斜率优化\(DP\) 这题目第一眼看上去感觉很明显是区间\(DP\) ...

  4. HDU-3507Print Article 斜率优化DP

    学习:https://blog.csdn.net/bill_yang_2016/article/details/54667902 HDU-3507 题意:有若干个单词,每个单词有一个费用,连续的单词组 ...

  5. APIO2010 特别行动队 斜率优化DP算法笔记

    做完此题之后 自己应该算是真正理解了斜率优化DP 根据状态转移方程$f[i]=max(f[j]+ax^2+bx+c),x=sum[i]-sum[j]$ 可以变形为 $f[i]=max((a*sum[j ...

  6. HDU 3507 Print Article(斜率优化DP)

    题目链接 题意 : 一篇文章有n个单词,如果每行打印k个单词,那这行的花费是,问你怎么安排能够得到最小花费,输出最小花费. 思路 : 一开始想的简单了以为是背包,后来才知道是斜率优化DP,然后看了网上 ...

  7. HDU 2993 MAX Average Problem(斜率优化DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2993 题目大意:给定一个长度为n(最长为10^5)的正整数序列,求出连续的最短为k的子序列平均值的最大 ...

  8. YBTOJ洛谷P3195:玩具装箱(斜率优化dp)

    传送门 文章目录 前言 解析 代码 前言 斜率优化dp,就是利用斜率优化的dp (逃) 解析 第一道斜优的题 分析题目 设sumisum_isumi​为1-i的c的前缀和 容易写出dp转移式: dpi ...

  9. bzoj1010[HNOI2008]玩具装箱toy 斜率优化dp

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MB Submit: 11893  Solved: 5061 [Submit] ...

最新文章

  1. 邮件header中的subject或者from解码
  2. 51单片机50个实例代码_常用的51单片机代码
  3. 动态反射——Load,LoadFrom和LoadFile
  4. 5款新颖的ReSharper插件
  5. 解决oracle数据库连接不上的问题
  6. 目标检测之两阶段算法--Fast R-CNN详解
  7. L1-042 日期格式化
  8. [引]VS2005帮助文档 : 加密 概述
  9. java被oracle收购,sun被oracle收购了,openoffice和virtualbox会不会死?
  10. python微信聊天机器人_教你用Python创建微信聊天机器人
  11. 威纶通触摸屏可以解密吗_【实例】西门子PLC变频器和触摸屏综合应用
  12. 教你自定义百度网盘分享密码 提取码
  13. 浪潮服务器 U盘安装 Windows server 2016系统
  14. 路由与交换|实验一   路由器基本配置
  15. Cent OS 7 的日常操作
  16. mac地址是由多少个bit组成_MAC地址是什么,MAC地址是怎么构成的,MAC地址占多少位,作用如何? 简单答案!!...
  17. 小甲鱼汉诺塔代码理解
  18. kali局域网ARP攻击欺骗+图片获取
  19. CCPC 2018网络预赛 hdu 6447 YJJ's Salesman
  20. ME02 认知之2017罗胖跨年演讲

热门文章

  1. 螺纹铣刀与丝锥攻丝有什么区别,谁的优势大呢?
  2. IMU预积分--详细推导过程
  3. java/php/net/python员工管理系统设计与实现设计
  4. 原生html例子,原生js的innerHTML用法示例
  5. 防反接保护电路及功耗计算
  6. lol网通区服务器的位置,lol各大区人数排行2020,lol网通区实力排名
  7. android原型图工具下载,xiaopiu原型工具
  8. 【应用统计学】第一类/α/弃真错误与第二类/β/取伪错误的解释与举例
  9. 手持无线电综合测试仪都有什么功能
  10. 【手撕AHB-APB Bridge】~ AMBA总线 之 APB