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 <= 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

【题解】

  这是一道极其恶心的卡精度题,据说有O(n)做法,但太高端了。

  nlogn做法很显然是二分答案,关键就在如何检查了。

  直接去求平均值是否大于某个数跟暴力无异,因此我们转换一下,把每个数都减去答案,求一个满足条件的区间且区间和大于0。

  预处理dp[i]表示i及i向右的区间最大是多少,可以不选,为0。

  线性做就可以了

  放大处理,被卡了老半天精度,最终弃疗,老老实实乘了1ex

  

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <cmath>
 6
 7 inline void read(long long &x)
 8 {
 9     x = 0;char ch = getchar(),c = ch;
10     while(ch < ‘0‘ || ch > ‘9‘)c = ch, ch = getchar();
11     while(ch <= ‘9‘ && ch >= ‘0‘)x = x * 10 + ch - ‘0‘, ch = getchar();
12     if(c == ‘-‘)x = -x;
13 }
14
15 const int MAXN = 100000 + 10;
16
17 long long n,num[MAXN],len,sum,tmp[MAXN],dp[MAXN];
18
19 int check(long long m)
20 {
21     for(register int i = 1;i <= n;++ i)
22         tmp[i] = num[i] - m;
23     //dp[i]表示从i开始向右的最大和
24     for(register int i = n;i >= 1;-- i)
25         if(dp[i + 1] + tmp[i] > 0)dp[i] = dp[i + 1] + tmp[i];
26         else dp[i] = 0;
27     for(register int i = 1;i <= n;++ i)
28         tmp[i] += tmp[i - 1];
29     //最短长度len + 向右最大和
30     for(register int i = len;i <= n;++ i)
31         if(tmp[i] - tmp[i - len] + dp[i + 1] >= 0)
32             return 1;
33     return 0;
34 }
35
36 int main()
37 {
38     read(n), read(len);
39     for(register int i = 1;i <= n;++ i)
40         read(num[i]), num[i] *= 10000000, sum += num[i];
41     register long long l = 1, r = sum, mid, ans;
42     while(l <= r)
43     {
44         mid = (l + r) >> 1;
45         if(check(mid)) l = mid + 1;
46         else r = mid - 1;
47     }
48     printf("%lld", l/10000);
49     return 0;
50 }
时间: 2024-11-03 22:21:25

POJ2018 Best Cow Fences的相关文章

poj2018——Best Cow Fences

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 th

POJ2018 Best Cow Fences 二分答案/DP

显然二分答案,然后减去对应的mid,求超过L的最大子段和验证就好了. 当然记录下长度的直接DP也是可以的. 然而二分答案怎么都WA,很好奇为什么 直接输出r反而是能过的. 看了下https://blog.csdn.net/jiangshibiao/article/details/21963437 想起来double取整输出的时候要加上eps 1 https://blog.csdn.net/jiangshibiao/article/details/21963437 原文地址:https://www

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

一本通 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;

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 nu

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

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