06: Monthly Expenses

Source: Internet
Author: User

Title Link: http://noi.openjudge.cn/ch0111/06/

Total time limit: 1000ms memory limit: 65536kB
Describe
The farmer John is a shrewd accountant. He realized that he might not have enough money to keep the farm running. He calculates and records the costs that are needed for the next N (1≤n≤100,000) day.

John intends to create a budget for the successive M (1≤m≤n) fiscal cycle, naming a fiscal period of fajo months. Each Fajo month consists of one or more consecutive days, each day being contained in a Fajo month.

  John's goal is to rationalize the number of days that each Fajo month contains, making the cost of the most expensive Fajo month as low as possible.

Input
The first line contains two integer n,m separated by a single space.
Next n rows, each line contains an integer between 1 and 10000, giving the cost per day for the next N days in order.
Output
  An integer that is the minimum value for the maximum monthly cost.

Sample input
9 {
100
400
300
100
500
101
400
Sample output
500
Input/Output Sample description
Joyo the first two days as one months, the third to fourth two days as one months, the last three days respectively as one months, the maximum monthly cost is 500. Any other allocation will be larger than this.

First look at the AC code:

1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 intCheckLong*a,LongNLong LongMidLongM);5 intMain ()6 {7     Longn,m;8     Long*a=null,i;9     Long Longleft=0, right=0, mid=0;Ten     intRes; One      Ascanf"%ld%ld",&n,&M); -A= (Long*)malloc(nsizeof(Long)); -Memset (A,0, N); the      for(i=0; i<n;i++) -     { -scanf"%ld",&a[i]); -         if(A[i]>left) left=A[i]; +right=right+A[i]; -     } +  A      while(left<Right ) at     { -mid=left+ (Right-left)/2; -res=check (a,n,mid,m); -         if(res==1) right=mid; -         ElseLeft=mid+1; -     } inprintf"%lld\n", left); -     return 0; to } +  - //assuming the maximum monthly cost is mid, the statistics need to be divided into months. And then see if the number of months is too much or too little the intCheckLong*a,LongNLong LongMidLongM) * { $     LongCount=1, i,temp=0;Panax Notoginseng      for(i=0; i<n;i++) -     { the         if(Temp+a[i]<=mid) temp=temp+a[i];//the first day of the month is counted to the current count . +         Else if(A[i]<=mid)//can be separated into a month A         { thecount++;//Start a new month +temp=A[i]; -             if(count>m)return-1;//The maximum monthly overhead is too small, resulting in too many sub-groups.  $         } $         Else return-1;//The maximum monthly cost mid is too small, resulting in some of the more expensive days alone constitute one months.  -     } -     if(count>m)return-1; the     Else if(count<=m)return 1;//The maximum monthly cost mid is too large, resulting in too few sub-groups -}

Description of the idea:

The meaning of the topic must be understood clearly!!! " make reasonable arrangements for the number of days per Fajo month, so that the cost of the most expensive fajo months is as low as possible." "" outputs an integer, which is the minimum value for the maximum monthly cost. "

is to divide all the days into a number of segments, first to find the sum of the numbers in each paragraph, and then to count the sum of the maximum value of each paragraph, this value should be as small as possible. Now what we're looking for is this "cumulative sum of the maximum" minimum can be how much.

First of all, this topic should be two points, because the interval of the solution is clear and can be divided into the real solution of the interval.

Suppose the interval of the Left~right, where left is the largest number of the N-day overhead, right is the sum of the N-day overhead.    (Imagine a limit situation, to make every one months the cost is as small as possible, then each day alone to do one months is good, so this time the maximum monthly cost is the maximum value of the day in n days, so left can take Max (A1,......, an). Imagine another extreme situation, combining all the days together to form one months, then the maximum monthly cost is sum (A1,A2,......, an), so the right value is n days of accumulation and. )

One area to note is the code for the two-part loop:

1      while(left<Right )2     {3mid=left+ (Right-left)/2;4res=check (a,n,mid,m);5         if(res==1) right=mid;6         ElseLeft=mid+1;7     }8printf"%lld\n", left);

Where left=mid+1 must be added 1, otherwise it may die in circulation.

Additionally, the output value is left. This place also needs special attention. (Please fill in your own brain why is left?)

About the sub-function check (), well the code comment is very clear, do not say.

06: Monthly Expenses

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.