POJ3273 Monthly Expense —— 二分
Posted h_z_cong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3273 Monthly Expense —— 二分相关的知识,希望对你有一定的参考价值。
题目链接:http://poj.org/problem?id=3273
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 29231 | Accepted: 11104 |
Description
Farmer John is an astounding accounting wizard and has realized he might run out of money to run the farm. He has already calculated and recorded the exact amount of money (1 ≤ moneyi ≤ 10,000) that he will need to spend each day over the next N (1 ≤ N ≤ 100,000) days.
FJ wants to create a budget for a sequential set of exactly M (1 ≤ M ≤ N) fiscal periods called "fajomonths". Each of these fajomonths contains a set of 1 or more consecutive days. Every day is contained in exactly one fajomonth.
FJ‘s goal is to arrange the fajomonths so as to minimize the expenses of the fajomonth with the highest spending and thus determine his monthly spending limit.
Input
Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Sample Input
7 5 100 400 300 100 500 101 400
Sample Output
500
Hint
Source
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <vector> 7 #include <queue> 8 #include <stack> 9 #include <map> 10 #include <string> 11 #include <set> 12 #define ms(a,b) memset((a),(b),sizeof((a))) 13 using namespace std; 14 typedef long long LL; 15 const double EPS = 1e-8; 16 const int INF = 2e9; 17 const LL LNF = 2e18; 18 const int MAXN = 1e5+10; 19 20 int n, m; 21 int a[MAXN]; 22 23 bool test(int mid) 24 { 25 int cnt = 0, sum; 26 for(int i = 1; i<=n; i++) 27 { 28 if(a[i]>mid) return false; 29 30 if(i==1 || sum+a[i]>mid) // i==1特别注意 31 cnt++, sum = a[i]; 32 else 33 sum += a[i]; 34 } 35 return cnt<=m; 36 } 37 38 int main() 39 { 40 while(scanf("%d%d",&n,&m)!=EOF) 41 { 42 for(int i = 1; i<=n; i++) 43 scanf("%d", &a[i]); 44 45 int l = 0, r = INF; 46 while(l<=r) 47 { 48 int mid = (l+r)>>1; 49 if(test(mid)) 50 r = mid - 1; 51 else 52 l = mid + 1; 53 } 54 cout<<l<<endl; 55 } 56 }
以上是关于POJ3273 Monthly Expense —— 二分的主要内容,如果未能解决你的问题,请参考以下文章