POJ2018 Best Cow Fences —— 斜率优化DP
题目链接:https://vjudge.net/problem/POJ-2018
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 11394 | Accepted: 3736 |
Description
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
* 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
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的更多相关文章
- POJ-2018 Best Cow Fences(二分加DP)
Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 10174 Accepted: 3294 Desc ...
- HDU 3045 Picnic Cows(斜率优化DP)
Picnic Cows Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...
- bzoj-4518 4518: [Sdoi2016]征途(斜率优化dp)
题目链接: 4518: [Sdoi2016]征途 Description Pine开始了从S地到T地的征途. 从S地到T地的路可以划分成n段,相邻两段路的分界点设有休息站. Pine计划用m天到达T地 ...
- bzoj-1096 1096: [ZJOI2007]仓库建设(斜率优化dp)
题目链接: 1096: [ZJOI2007]仓库建设 Description L公司有N个工厂,由高到底分布在一座山上.如图所示,工厂1在山顶,工厂N在山脚.由于这座山处于高原内陆地区(干燥少雨),L ...
- [BZOJ3156]防御准备(斜率优化DP)
题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=3156 分析: 简单的斜率优化DP
- 【BZOJ-1096】仓库建设 斜率优化DP
1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3719 Solved: 1633[Submit][Stat ...
- [USACO2003][poj2018]Best Cow Fences(数形结合+单调队列维护)
http://poj.org/problem?id=2018 此乃神题……详见04年集训队论文周源的,看了这个对斜率优化dp的理解也会好些. 分析: 我们要求的是{S[j]-s[i-1]}/{j-(i ...
- BZOJ 1010: [HNOI2008]玩具装箱toy 斜率优化DP
1010: [HNOI2008]玩具装箱toy Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再 ...
- BZOJ 3156: 防御准备 斜率优化DP
3156: 防御准备 Description Input 第一行为一个整数N表示战线的总长度. 第二行N个整数,第i个整数表示在位置i放置守卫塔的花费Ai. Output 共一个整数,表示最小的战 ...
随机推荐
- 遇到的cannot find module 'xxx' 问题
昨天配置新项目的环境,明明都npm inatsll都安装了,再次运行的时候一直报错,缺少模块.我以为就一个没安装上,就手动npm install -g 'xxx' :结果一个装好了,又继续蹦出cann ...
- codevs——1008 选数
1008 选数 2002年NOIP全国联赛普及组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 已知 n ...
- python3:requests模块-写了一点
使用requests,它的七个主要方法,在这里只讲两个:get.post >>> import requests >>> r=requests.get(" ...
- composer配置和安装php框架
第一步:安装composerwin环境安装:下载地址:https://getcomposer.org/Composer-Setup.exe 下载后直接点击安装即可测试:cmd ->compose ...
- 安装Vmware增强工具
主机: Win7 虚拟机: VMware8.0+Debian6 目标: 离线安装软件包和VMware Tools 在虚拟机上安装完debian6后 1.在vmware的菜单中选择Vm->inst ...
- 【spring data jpa】使用repository进行查询,使用userRepository.getOne(id)和userRepository.findById(id)无法从数据库查询到数据
如题: 使用repository进行查询,使用CrudRepository自带的getOne()方法和findById()方法查询,数据库中有这条数据,但是并不能查到. userRepository. ...
- 为了安全,linux下如何使用某个用户启动某个进程?
安全里有个原则,叫最小权限原则 根据这个原则,对于启动某个应用或者进程,应该赋予其最小权限,根据应用权限要求,创建一个相应权限的用户,赋予其应用相应的权限,然后使用这个用户启用这个应用 如何使用某个用 ...
- css 中的浮动
css中 浮动的作用: 使元素脱离正常的文档流并使其移动到其父元素的“最左边”或“最右边”. css中 浮动的特点: 1)改变元素类型,使元素支持宽高: 2)半脱离文档流: 3)文本环绕: 4)顶对齐 ...
- XPath可以快速定位到Xml中的节点或者属性。XPath语法很简单,但是强大够用,它也是使用xslt的基础知识。
示例Xml: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <?xml versio ...
- Cocos2d-x初识
cocos2d-x引擎是什么 在学习游戏的时候就有意的搜索这方面的知识,知道游戏须要游戏引擎,引擎听着非常厉害,只是就是一个游戏框架. 或许某一个游戏框架火起来了,就非常流行了,只是我觉得不论什么游戏 ...