Best Cow Fences POJ - 2018

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

题意:n块田地,每块田地有cows【i】头牛,求出一个长度不小于F的子段,使子段牛的平均数最大。思路:我们令 avr = sum【i,j】/(i-j+1)   那么这题就是 求是avr的最大值,我们二分枚举ans,判断 avr 是否不小于 ans,即avr >= ans,为了不维护(i-j+1)的值,变形成sum【i,j】 - ans*(i-j+1) >= 0,我们把原数组cows【i】-ans,那么Sum【i,j】 = sum【i,j】-ans(i-j+1)。现在关键就是取得一个  max{Sum【i,j】},对于Sum【i,j】,我们可以用前缀和相减的方式求得,sum(i)-min(sum(j)), 0 <= j <= i-F。

坑点:注意最后答应的是二分的R值,我打印L值Wa的怀疑人生.而且题目要求精度是1e-4,当我们枚举的精度不小于题目要求精度的时候L和R值都是OK的我感觉是如果两个值转换为整数不同的话,R值转换的整数值在L~R区间内的,而l转换的值是小于l的,如果两个值转换的值相同,打印那个都行,且整数肯定小于L。

其实像是最大值最小,最小值最大,都可以用二分解决,答案是单调的,这题还有种用凸包方法写的,以后再填坑吧。

 1 #include<cstdio>
 2 #include<iostream>
 3 using namespace std;
 4
 5 const int maxn = 1e5+5;
 6 int n,f;
 7 int cows[maxn];
 8 const double eps = 1e-8;
 9
10 bool solve(double x,int f)
11 {
12     double fcows[n+1];
13     double sum[n+1];
14     for(int i=1;i<=n;i++)fcows[i] = cows[i] - x;
15     for(int i=1;i<=n;i++)sum[i] = sum[i-1]+fcows[i];
16     double ans = -1e10,minn = 1e10;
17     for(int i=f;i<=n;i++)
18     {
19         minn = min(minn,sum[i-f]);
20         ans = max(ans,sum[i]-minn);
21     }
22     return ans >= 0;
23 }
24 int main()
25 {
26     scanf("%d%d",&n,&f);
27     double low=0;
28     double high = 0;
29     for(int i=1;i<=n;i++)
30     {
31         scanf("%d",&cows[i]);
32         high += cows[i];
33     }
34     while(low + eps < high)
35     {
36         double mid = (low+high)/2;
37         if(solve(mid,f))low = mid;
38         else high = mid;
39     }
40     printf("%d\n",(int)(1000*high));
41 }



原文地址:https://www.cnblogs.com/iwannabe/p/10160942.html

时间: 2024-10-05 17:30:05

Best Cow Fences POJ - 2018的相关文章

POJ 2018 Best Cow Fences

斜率优化DP...<浅谈数形结合思想在信息学竞赛中的应用 安徽省芜湖一中 周源>例题... Best Cow Fences Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 9311   Accepted: 2986 Description Farmer John's farm consists of a long row of N (1 <= N <= 100,000)fields. Each field c

POJ 2018 Best Cow Fences(二分答案)

POJ 2018 Best Cow Fences(二分答案) Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 12144 Accepted: 3958 Description Farmer John's farm consists of a long row of N (1 <= N <= >100,000)fields. Each field contains a certain nu

POJ2018 Best Cow Fences

Best Cow Fences Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11175   Accepted: 3666 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 <

一本通 1434:【例题2】Best Cow Fences

Best Cow Fences 二分答案 + 前缀和 个人认为题意没有表述清楚,本题要求的是满足题意的连续子序列(难度大大降低了有木有). 本题的精度也是非常令人陶醉,请您自行体会吧! #include <iostream> #include <cstdio> #include <algorithm> using namespace std; //Mystery_Sky // #define M 10000100 #define ex 1e-5 double l = 1

poj 动态规划DP - 2018 Best Cow Fences

这道题目我一开始的思路是用二维DP,结果TLE了.后来换了个思路,终于AC了. 不需要判断所有的情况,我们用dp[i]表示前i个牛圈中最大的牛数,而这个i首先必须>=限制的牛圈树f.用num[i]表示dp[i]中包含了多少牛圈. 我们可以知道,dp[i] = sum[i] - sum[i-f])/f  or  dp[i-1] + data[i], 前一个代表到i为止前f个牛圈的牛数,后一个代表前i-1个牛圈中最大牛数+第i个牛圈中的牛数.其实也就是到i为止前num[i-1]+1个牛圈的牛数.而判

G - Best Cow Fences (POJ - 2018)

- 题目大意 给你n个牛的自身价值,让你找出连续的且数量大于等于F的一段区间,使这段区间内的牛的平均价值最大. - 解题思路 这道题可以用二分法也可以结合前缀数组来求和来做,我就是用前缀数组求和和二分答案法来做的. - 代码 #include <iostream> #include <algorithm> using namespace std; const int maxn = 1e5 + 10; int sum[maxn]; int main() { int n, f, a;

POJ 2018 Best Cow Fences(二分最大区间平均数)题解

题意:给出长度>=f的最大连续区间平均数 思路:二分这个平均数,然后O(n)判断是否可行,再调整l,r.判断方法是,先求出每个数对这个平均数的贡献,再求出长度>=f的最大贡献的区间,如果这个最大贡献大于0说明这个二分出来的数可行. 代码: #include<set> #include<map> #include<stack> #include<cmath> #include<queue> #include<vector>

Cow Acrobats POJ - 3045

Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the circus. Their hoofed feet prevent them from tightrope walking and swinging from the trapeze (and their last attempt at firing a cow out of a cannon met wit

Greedy:Cow Acrobats(POJ 3045)

牛杂技团 题目大意:一群牛想逃跑,他们想通过搭牛梯来通过,现在定义risk(注意可是负的)为当前牛上面的牛的总重量-当前牛的strength,问应该怎么排列才能使risk最小? 说实话这道题我一开始给书上的二分法给弄懵了,后来看了一下题解发现根本不是这么一回事,其实就是个简单的贪心法而已. 这题怎么贪心呢?就是按w+s从小到大排序就好了,证明一下: 1.先证明如果不满足w+s的序列性,无论谁在谁的上面,都会违反题设:(设A在B的上面) 如果 A.s+A.w<B.s+B.w 则如果B.s<m+A