1639: [Usaco2007 mar]monthly Expense monthly expenditure

Source: Internet
Author: User

Title: Farmer John is a surprising accounting genius, he has learned that he may spend his money, the money was supposed to keep the farm running every month. He has calculated the cost of moneyi (1<=moneyi<=10,000) for each day of his next N (1<=n<=100,000) business day, and he wants to be a serial M (1<=m<=n) called "Liquidation Month" for him. Checkout period to make a budget, each "Liquidation month" contains one business day or more consecutive working days, each working day is only included in a "liquidation month". FJ's goal is to arrange these "clearing months" to minimize the cost of each liquidation month, thus determining his monthly spending limit.

Input:

First line: Two integers separated by a space: N and M

2nd.. N+1 Line: Line i+1 contains the cost of FJ on his first business day

Output: First line: The amount of money that keeps the farm running every month

Parsing: Two-minute enumeration of Money X (that is, the maximum spend in the liquidation month), can be counted as a clearing moon (that is, the sum is less than x), even if it is one months, if the total number of months calculated is less than or equal to M, then X is established, otherwise, is not established.

Program:

#include <iostream>#include<cstdio>#include<algorithm>using namespacestd;Long Longp1,m,mid,l,ans,n,l1[50001],d[50001],l2[50001],lef,righ;BOOLCheckLong Longx) {  Long LongK; if(x>l)return false; K=m; P1=0;  for(intI=1; i<=n+1;++i) {if(d[i]-d[p1]<x) k-=1; Elsep1=i; }  if(k<0)return false; Else return true;}intMain () {CIN>>l>>n>>m;  for(intI=1; i<=n;++i) cin>>D[i]; d[0]=0; Sort (d+1, d+1+N); D[n+1]=l; Lef=0; Righ=l+3;  while(lef<=righ)                                                                                                                                          { Mid= (Lef+righ)/2; if(check (mid) = =true) {Lef=mid+1; if(Mid>ans) ans=mid; }   Elserigh=mid-1; } cout<<ans<<Endl; return 0;}

1639: [Usaco2007 mar]monthly Expense monthly expenditure

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.