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];}elseans+=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;elseleft=mid+1;}while(!judge(left))left++;cout<<left;return 0;
}
转载于:https://www.cnblogs.com/wzl19981116/p/9381859.html
C - Monthly Expense相关推荐
- 二分搜索 POJ 3273 Monthly Expense
题目传送门 1 /* 2 题意:分成m个集合,使最大的集合值(求和)最小 3 二分搜索:二分集合大小,判断能否有m个集合. 4 */ 5 #include <cstdio> 6 #incl ...
- 【POJ 3273】 Monthly Expense (二分)
[POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...
- Monthly Expense【二分】
B - Monthly Expense POJ - 3273 Farmer John is an astounding accounting wizard and has realized he mi ...
- bzoj1639[Usaco2007 Mar]Monthly Expense 月度开支*
bzoj1639[Usaco2007 Mar]Monthly Expense 月度开支 题意: n天,每天有一个花费,现在要将它们分成连续的m段,要求所有段的总花费的最大值最小.求这个值.n,m≤10 ...
- BZOJ 1639: [Usaco2007 Mar]Monthly Expense 月度开支( 二分答案 )
直接二分答案然后判断. ----------------------------------------------------------------------------- #include&l ...
- bzoj 1639: [Usaco2007 Mar]Monthly Expense 月度开支(二分)
1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 1057 Solve ...
- USACO Monthly Expense
洛谷 P2884 [USACO07MAR]每月的费用Monthly Expense https://www.luogu.org/problem/P2884 POJ 3273 Monthly Expen ...
- Monthly expense(二分)
又又又又是二分啦 憨憨桃子又来啦 题目链接:monthly expense 题目描述: Farmer John is an astounding accounting wizard and has r ...
- Monthly Expense(二分)
Monthly Expense Farmer John is an astounding accounting wizard and has realized he might run out of ...
- Monthly Expense POJ - 3273(二分最大值最小化)
Farmer John is an astounding accounting wizard and has realized he might run out of money to run the ...
最新文章
- 联邦学习:AI大规模落地又一革命性突破
- VS2013+openCV3.0无脑配置方法+解决警告问题【windows平台】
- 机器学习:多分类的logistic回归
- 我的工作流为什么启动不了
- iOS判断是模拟器还是真机
- Linux一些经典书籍
- C# ReaderWriterLockSlim类
- VSTS For Testers读书笔记(5)
- 对齐方式有那些_Excel基础:开始菜单之对齐方式,那些被遗忘的实用功能
- C++ Primer 第五版 第6章 6.3——函数返回类型和return语句阅读笔记
- Linux_service cloudera-scm-server start failed
- webrtc在ubuntu14.04上的编译过程(12.04亦可)
- C#使用DevExpress中的chartcontrol
- 淘宝 - 淘宝买家信誉怎么查询?
- 《软件测试》[(美)Ron Patton]第三章阅读总结
- 云计算与大数据——数据挖掘常用算法
- [ vulhub漏洞复现篇 ] Tiki Wiki CMS Groupware 认证绕过漏洞CVE-2020-15906
- malloc、calloc、realloc函数讲解
- flink流处理示例开发
- mac pro词典无法使用问题