POJ Dynamic Planning DP-2018 Best Cow Fences

Source: Internet
Author: User

This topic I started with the idea is to use two-dimensional DP, the results of tle. Later changed a train of thought, finally AC.

There is no need to judge all the cases, we use dp[i] to represent the largest number of cows in the first I Niu Quan, and this I must first >= limit the Niu Quan tree F. Num[i] Indicates how many NIU Quan are included in Dp[i].

we can know, dp[i] = sum[i]-sum[i-f])/F or dp[i-1] + data[i], before a representative to the first F Niu Quan the number of cows, the latter represents the former i-1 Niu Quan The number of cows in the maximum number of cows + the Niu Quan in the first I. In fact, so far before I num[i-1]+1 a niu Quan cattle number. and determine which condition is judged (sum[i]-sum[i-f])/F and Dp[i-1] + data[i]/(num[i-1]+1) size.

#include <stdio.h> #include <algorithm> #define MAX (x, y) (x>y?x:y) #define MAX 100002double dp[max],data[ Max],num[max],sum[max];int Main () {int i,n,f;double maxval;scanf ("%d%d", &n,&f); for (i=1;i<=n;i++) {scanf ( "%lf", &data[i]); Sum[i] = Data[i] +sum[i-1];num[i] = f;} DP[F] = Sum[f];maxval  = dp[f]*1000/f;for (i=f+1;i<=n;i++) {if ((Sum[i]-sum[i-f])/F > (Dp[i-1] + data[i])/(num[ I-1] + 1)) {Dp[i] = Sum[i]-sum[i-f];num[i] = f;} Else{dp[i] = Dp[i-1] + data[i];num[i] = num[i-1] +1;} Maxval = max (Maxval,dp[i]*1000/num[i]);} printf ("%d\n", (int) (maxval));}



POJ Dynamic Planning DP-2018 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.