C - Monthly Expense
Posted wzl19981116
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C - Monthly Expense相关的知识,希望对你有一定的参考价值。
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 5
100
400
300
100
500
101
400
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.
对钱在最大值和总钱数之间取二分,但是不知道之前为什么一直wa,后面改了一下写法就过了很奇怪
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define pb push_back
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<map>
#define for(i,a,b) for(int i=a;i<b;i++)
typedef long long ll;
typedef long double ld;
const ll mod=1e12+100;
using namespace std;
const double pi=acos(-1.0);
int a[100005],n,m;
bool judge(int mid)
{
int k=1,ans=0;
for(i,0,n)
{
if(a[i]+ans>mid)
{
k++;
ans=a[i];
}else
ans+=a[i];
}
return k<=m;
}
int main()
{
//freopen("output1.txt", "r", stdin);
cin>>n>>m;
int sum=0,Max=0;
for(i,0,n)
{
sf("%d",&a[i]);
Max=max(Max,a[i]);
sum+=a[i];
}
if(m==1)
{
cout<<sum;
return 0;
}
int left=Max,right=sum,mid;
while(left<right)
{
mid=(left+right)/2;
if(judge(mid))
right=mid;
else
left=mid+1;
}
while(!judge(left))
left++;
cout<<left;
return 0;
}
以上是关于C - Monthly Expense的主要内容,如果未能解决你的问题,请参考以下文章