Bzoj 1639: [usaco2007 Mar] monthly expense monthly expenditure

Source: Internet
Author: User
Description

Farmer John is a surprising accounting genius and he understands that he may spend his money to keep the farm running properly every month. He has calculated the cost of moneyi (1 <= moneyi <= 100,000) per day in N (1 <= n <= 10,000) working days ), he wants to make a budget for his consecutive M (1 <= m <= N) checkout period called "liquidation month, each "liquidation month" includes one working day or more consecutive working days, and each working day is only included in one "liquidation month. FJ's goal is to arrange these "liquidation months" to minimize the maximum expense of each liquidation month to determine his monthly expenditure limit. Input

First line: two integers separated by spaces: N and m

2nd. n + 1 row: line I + 1 contains the output of FJ's spending on the I + 1 business day.

Row 1: The amount of money that can maintain normal farm operation every month

Question:

M <= n. If it is divided into <m liquidation months to meet the requirements, it can also be divided into M liquidation months.

The maximum value is the smallest. Two points are considered.

Divide it into two ans.

Then simulate how much money is used up every day,

The expected liquidation month count CNT is obtained.

If CNT <= m is valid, otherwise it is invalid.

Code:

#include<cstdio>#include<cstring>#include<algorithm>//by zrt//problem:using namespace std;typedef long long LL;LL a[100005],n,k;LL maxx=0;LL l,r;bool judge(LL x){    LL sum=0;    LL cnt=0;    for(int i=1;i<=n;i++){        if(sum+a[i]>x){            sum=a[i];            cnt++;            if(cnt>k) return 0;        }else{            sum+=a[i];        }    }    if(sum)cnt++;    if(cnt<=k) return 1;    else return 0;}LL sum;int main(){    #ifdef LOCAL    freopen("in.txt","r",stdin);    freopen("out.txt","w",stdout);    #endif    scanf("%lld%lld",&n,&k);    for(int i=1;i<=n;i++){        scanf("%lld",&a[i]);        maxx=max(maxx,a[i]);        sum+=a[i];    }    l=maxx-1,r=sum;    while(r-l>1){        int m=(l+r)>>1;        if(judge(m)){            r=m;        }else l=m;    }    printf("%lld\n",r);    return 0;}

Bzoj 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.