标签:vector schedule 之间 rate imu account stream ret monthly
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.
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
Line 1: The smallest possible monthly limit Farmer John can afford to live with.
7 5
100
400
300
100
500
101
400
500
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.
#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;
}
标签:vector schedule 之间 rate imu account stream ret monthly
原文地址:https://www.cnblogs.com/wzl19981116/p/9381859.html