Poj2018 best cow fences

Source: Internet
Author: User

My understanding of binary: https://www.cnblogs.com/AKMer/p/9737477.html

Question Portal: http://poj.org/problem? Id = 2018

Let's divide an average into two parts, and set \ (A \) array to each number minus the average to \ (B \) array. If \ (B \) an array with a length greater than \ (k \) and a weight greater than \ (0 \) indicates that the final average value must be greater than the average value of our current binary.

So how can we find the weight and length of an array greater than \ (0 \) and greater than \ (k? Obviously, we can enumerate the following fields:

for(int i=1;i<=n;i++)    for(int j=0;j<=i-k;j++)        if(sum[i]-sum[j]>0)return 1;

But this \ (N ^ 2 \) method obviously does not work. We can find that the Section ending with \ (I \) is \ (sum [0] ~ The minimum value in sum [I-K] \) is \ (sum [I]-Mn> 0. So we only need to \ (I + 1 \), \ (Mn \) and \ (sum [I-k + 1] \) take \ (Min \) that's all.

Time Complexity: \ (O (nloga )\)

Space complexity: \ (O (n )\)

The Code is as follows:

# Include <cstdio> # include <algorithm> using namespace STD; const double EPS = 1e-6; const int maxn = 1e5 + 5; int N, K; int A [maxn]; double sum [maxn]; int read () {int x = 0, F = 1; char CH = getchar (); (; ch <'0' | ch> '9'; CH = getchar () if (CH = '-') F =-1; (; CH> = '0' & Ch <= '9'; CH = getchar () x = x * 10 + CH-'0'; return x * F ;} bool check (double AVE) {for (INT I = 1; I <= N; I ++) sum [I] = sum [I-1] + (double) A [I]-Ave; double Mn = 0; For (INT I = K; I <= N; I ++) {If (sum [I]-Mn> 0) return 1; Mn = min (Mn, sum [I-k + 1]);} return 0;} int main () {double L = 1e9, r =-1e9; N = read (), k = read (); For (INT I = 1; I <= N; I ++) {A [I] = read (); L = min (L, (double) A [I]); r = max (R, (double) A [I]);} while (L + EPS <R) {double mid = (L + r)/2; If (check (MID) L = mid; else r = mid;} printf ("% d \ n ", (INT) (R * 1000); // output R * 1000 return 0 because the average value must be greater than l ;}

Poj2018 best cow fences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.