国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

POJ 3273-Monthly Expense(二分法-最小化最高花費)

2019-11-09 19:08:49
字體:
來源:轉載
供稿:網友
Monthly Expense
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 24397 Accepted: 9484

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

Line 1: Two space-separated integers: N and M Lines 2..N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day

Output

Line 1: The smallest possible monthly limit Farmer John can afford to live with.

Sample Input

7 5100400300100500101400

Sample Output

500

Hint

If Farmer John schedules the months so that the first two days are a month, the third and fourth are a month, and the last three are their own months, he spends at most $500 in any month. Any other method of scheduling gives a larger minimum monthly limit.

Source

USACO 2007 March Silver

題目意思:

給出John在N個月中每個月的花費,John想將N個月分成M個組。每組的月份是連續的,同一組的花費被相加起來,求所有分組情況中最高花費的最低值。

解題思路:

二分法,所求答案ans在[low,high]范圍內二分。low是最高的單月花費;high是各個月所有花費之和,mid是當前的ans。遍歷各個月份,將花費總和不超過ans的月份歸入一個組,統計在當前ans情況下能分成幾個組。如果組數少于M,說明ans偏大,范圍變成前二分之一;否則說明ans偏小,范圍變成后二分之一。
#include<iostream>#include<cstdio>#include<iomanip>#include<cmath>#include<cstdlib>#include<cstring>#include<map>#include<algorithm>using namespace std;#define MAXN 100000int a[MAXN];int n,m,high=0,low=-1,mid;bool solve(){    int res=1,sum=0;    for(int i=0; i<n; ++i)    {        sum+=a[i];//若干月花費總和        if(sum>=mid)//超過限額        {            ++res;//分組數目            if(sum==mid) sum=0;//當前月花費計入當前組            else sum=a[i];//當前月花費計入下一組            if(i==n-1) --res;//最后一個月剛好被計入最后一組        }    }    if(res>m) return false;    else return true;}int main(){#ifdef ONLINE_JUDGE#else    freopen("F:/cb/read.txt","r",stdin);    //freopen("F:/cb/out.txt","w",stdout);#endif    ios::sync_with_stdio(false);    cin.tie(0);    cin>>n>>m;    for(int i=0; i<n; ++i)    {        cin>>a[i];        if(a[i]>low) low=a[i];//最高的單月花費        high+=a[i];//所有花費之和    }    while(low<high)    {        mid=0.5*(low+high);        if(solve()) high=mid-1;        else low=mid+1;    }    cout<<low<<endl;    return 0;}/*7 5100400300100500101400*/
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 寿阳县| 宝鸡市| 含山县| 曲松县| 祁阳县| 营口市| 曲靖市| 郑州市| 桑植县| 文安县| 慈溪市| 新巴尔虎右旗| 临邑县| 桐梓县| 刚察县| 湄潭县| 陵水| 宣恩县| 杭锦后旗| 平山县| 华宁县| 巴马| 高邑县| 襄汾县| 个旧市| 庆阳市| 双峰县| 浠水县| 汉川市| 清流县| 高青县| 武陟县| 丰镇市| 青海省| 陕西省| 陵水| 石泉县| 台湾省| 临西县| 察雅县| 丽江市|