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的更多相关文章
- Divide and Conquer:Monthly Expense(POJ 3273)
Monthly Expense 题目大意:不废话,最小化最大值 还是直接套模板,不过这次要注意,是最小化最大值,而不是最大化最小值,判断的时候要注意 联动3258 #include <iostr ...
- Monthly Expense(二分查找)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...
- BZOJ1639: [Usaco2007 Mar]Monthly Expense 月度开支
1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 529 Solved: ...
- POJ 3273 Monthly Expense(二分查找+边界条件)
POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...
- [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14158 Accepted: 5697 ...
- BZOJ 1639: [Usaco2007 Mar]Monthly Expense 月度开支( 二分答案 )
直接二分答案然后判断. ----------------------------------------------------------------------------- #include&l ...
- 1639: [Usaco2007 Mar]Monthly Expense 月度开支
1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 593 Solved: ...
- POJ 3273 Monthly Expense(二分答案)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...
- bzoj1639 / P2884 [USACO07MAR]每月的费用Monthly Expense
P2884 [USACO07MAR]每月的费用Monthly Expense 二分经典题 二分每个段的限制花费,顺便统计下最大段 注意可以分空段 #include<iostream> #i ...
- POJ3273 Monthly Expense 2017-05-11 18:02 30人阅读 评论(0) 收藏
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25959 Accepted: 10021 ...
随机推荐
- Session.Abandon和Session.Clear的实现和区别
我在网上找了一个比较贴切的描述: Session.Clear()就是把Session对象中的所有项目都删除了,Session对象里面啥都没有.但是Session对象还保留.Session.Abando ...
- 最佳实战Docker持续集成图文详解
最佳实战Docker持续集成图文详解 这是一种真正的容器级的实现,这个带来的好处,不仅仅是效率的提升,更是一种变革:开发人员第一次真正为自己的代码负责——终于可以跳过运维和测试部门,自主维护运行环境( ...
- 018-Go将磁盘目录实现简单的静态Web服务
package main import( "net/http" ) func main(){ http.Handle("/", http.FileServer( ...
- 使用Buildozer部署Kivy到移动设备上
在安装好Buildozer软件之后,我们在包含main.py的文件夹下运行buildozer init这个命令,然后我们就会看到在该文件夹下有一个buildozer.spec这个文件,这个文件主要是用 ...
- Caused by: java.lang.ClassNotFoundException: Cannot find class解决办法
mapper.xml中resultMap导致的.
- IntelliJ IDEA for Mac(Java 语言开发的集成环境)破解版安装
1.软件简介 IntelliJ IDEA 是 macOS 系统上一款 java 语言开发的集成环境,IntelliJ 在业界被公认为最好的 java 开发工具之一,尤其在智能代码助手.代码自动提 ...
- 【C语言】数组名传递给函数,数组的sizeof变为4的原因
C语言中,数组名作为参数传递给函数时,退化为指针,sizeof对指针操作结果应该是4.例子如下: #include<iostream> using namespace std; void ...
- jQuery的deferred对象详解(转)
jQuery的开发速度很快,几乎每半年一个大版本,每两个月一个小版本. 每个版本都会引入一些新功能.今天我想介绍的,就是从jQuery 1.5.0版本开始引入的一个新功能----deferred对象. ...
- Github忽略keil工程生成的链接、编译等文件
*.bak *.ddk *.edk *.lst *.lnp *.mpf *.mpj *.obj *.omf *.plg *.rpt *.tmp *.__i *.crf *.o *.d *.axf *. ...
- Service discovery
https://www.cnblogs.com/dirt2/p/5987067.html Use Assigned Numbers in the Service Discovery Protocol ...