poj2018 Best Cow Fences
来源:互联网 发布:淘宝新店如何提升信誉 编辑:程序博客网 时间:2024/05/15 23:05
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 9985 Accepted: 3237
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.
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.
* 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 66 4210385941
Sample Output
6500
这题可以用斜率优化做也可以用二分做,我用的是二分做法。
题意:给你n个牛的自身价值,让你找出连续的且数量大于等于F的一段区间,使这段区间内的牛的平均价值最大。
思路:用二分枚举平均值ave,每个牛的价值都减去ave,看是否有连续的超过f长度的区间使得这段区间的价值大于等于0,如果能找到,那么说明这个平均值可以达到。先每个a[i]减去ave得到b[i],用dp[i]表示以i为结尾区间连续长度大于等于f的最大连续区间和,maxx[i]表示以i为结尾的最大连续区间和,sum[i]表示1~i的价值总和那么maxx[i]=max(maxx[i-1]+b[i],b[i]),dp[i]=maxx[i-f+1]+sum[i]-sum[i-f+1],判断是否有一个i(i>=f)满足dp[i]>=0.
#include<iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<math.h>#include<vector>#include<map>#include<set>#include<queue>#include<stack>#include<string>#include<algorithm>using namespace std;#define inf 99999999#define maxn 100050#define eps 1e-6double sum[maxn],a[maxn],b[maxn],dp[maxn],maxx[maxn];int main(){ int n,m,i,j,f,ant; double l,r,mid,ans; while(scanf("%d%d",&n,&f)!=EOF) { l=2000.0;r=1.0; for(i=1;i<=n;i++){ scanf("%lf",&a[i]); l=min(l,a[i]); r=max(r,a[i]);//这里不能省,不然会影响精度 } while(r-l>eps){ mid=(l+r)/2.0; sum[0]=0;maxx[0]=0; for(i=1;i<=n;i++){ b[i]=a[i]-mid; sum[i]=sum[i-1]+b[i]; maxx[i]=max(b[i],maxx[i-1]+b[i]); } ans=sum[f]; for(i=f+1;i<=n;i++){ dp[i]=maxx[i-f+1]+sum[i]-sum[i-f+1]; if(ans<dp[i])ans=dp[i]; } if(ans>=0)l=mid; else r=mid; } ant=1000*r; printf("%d\n",ant); } return 0;}
0 0
- poj2018 Best Cow Fences
- poj2018 Best Cow Fences
- POJ2018 Best Cow Fences DP或二分
- poj2018 Best Cow Fences(直接求斜率)
- usaco 2003 月赛 Best Cow Fences & poj2018 题解
- poj2018 Best Cow Fences(求至少k个连续数 使得平均值最大)
- poj 2018 Best Cow Fences
- poj 2018 Best Cow Fences
- poj 2018 Best Cow Fences
- POJ 2018 Best Cow Fences
- POJ 2018 Best Cow Fences
- POJ 2018 Best Cow Fences
- poj 2018 Best Cow Fences
- 【斜率】【poj 2018】Best Cow Fences
- POJ - 2018 Best Cow Fences(DP)
- POJ 2018 Best Cow Fences 笔记
- poj 2018 Best Cow Fences 二分查找dp
- poj 2018 Best Cow Fences dp+单调队列优化
- MAHOUT文本向量相似度计算
- PAT(甲级)1027
- String、StringBuffer与StringBuilder的区别
- 扫描由元组组成列表
- 改变UITextField placeHolder颜色、字体
- poj2018 Best Cow Fences
- POJ 3278 Catch That Cow
- ajax 跨域访问配置
- UVA - 455 Periodic Strings
- 第十九篇:猜图游戏UI完整代码
- C# WindowsForm 员工管理系统三【注册】
- 百度offe面经及找工作一个月体会
- LeetCode----Longest Valid Parentheses
- PAT(甲级)1028