Bzoj "1639": [Usaco2007 mar]monthly Expense monthly expenditure

Source: Internet
Author: User

1639: [Usaco2007 mar]monthly Expense monthly expenditure time limit:5 Sec Memory limit:64 MB
submit:700 solved:347
[Submit] [Status] [Discuss] Description

Farmer John was a surprising accounting genius who had understood that he might spend his money, which 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

The first line: The amount of money that keeps the farm running every month.

Sample Input7 5
100
400
300
100
500
101
400
Sample Output500
Problem: First see this question I unexpectedly thought of greed (scared I hurriedly on a toilet calm down this problem is actually two points, because the data limit m,n are 10000, so the maximum value will not be greater than 100000000, so mid= (1+100000000) >>1; Then add up the cost of each day, if sum>mid, then need to add a month, so tot++; otherwise no brain Gaga can be judged at the end of each sentence, if a[i]>mid| | Tot>m, the value you want to query must be on the right side of mid, and vice versa. Note, however, that the tot initialization is 1.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <cstdlib>7 #defineMAXN 100000+1008 intA[maxn],n,m,ans;9 BOOLPanintq)Ten { One     intsum=0, tot=1;//remember Tot=1; A      for(intI=1; i<=n;++i) -     { -         if(sum+a[i]<=q) sum+=A[i]; the         Else -         { -sum=a[i];++tot; -         } +         if(tot>m| | A[I]&GT;Q)return 0; -     } +     return 1; A } at intErfen (intp) - { -     intL=1, r=100000000; -      while(l<=R) -     { -         intMid= (l+r) >>1; in         if(Pan (mid)) { -ans=mid;r=mid-1; to         } +         ElseL=mid+1; -     } the     returnans; * } $ using namespacestd;Panax Notoginseng intMain () - { theFreopen ("Input.txt","R", stdin); Freopen ("output.txt","W", stdout); +Cin>>n>>m; A      for(intI=1; i<=n;++i) scanf ("%d",&a[i]); the Erfen (n); +cout<<ans; -     return 0; $}
View Code

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.