POJ 3273
来源:互联网 发布:淘宝 演唱会票 编辑:程序博客网 时间:2024/06/05 18:07
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
Lines 2.. N+1: Line i+1 contains the number of dollars Farmer John spends on the ith day
Output
Sample Input
7 5100400300100500101400
Sample Output
500
Hint
注意二分的限制条件
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 100010;
int a[N];
int judge(int x);
int n, m;
int main()
{
while(scanf("%d %d", &n, &m)!=EOF)
{
int sum=0;
for(int i=1; i<=n; i++)
{
scanf("%d", &a[i]);
sum+=a[i];
}
int l=0, r=sum, mid;
while(l<=r)
{
mid=(l+r)/2;
if(judge(mid))
{
r=mid-1;
}
else
{
l=mid+1;
}
}
printf("%d\n",l);
}
return 0;
}
int judge(int x)
{
int cnt=0, sum=0;
for(int i=1; i<=n; i++)
{
sum+=a[i];
if(sum==x)
{
if(i==n)
{
break;
}
sum=0;
cnt++;
}
else if(sum>x)
{
sum=a[i];
cnt++;
}
if(a[i]>x||cnt>m-1)
{
return 0;
}
}
if(cnt<=m-1)
{
return 1;
}
return 0;
}
- POJ 3273
- poj 3273
- poj 3273
- Poj 3273
- POJ 3273
- POJ-3273
- poj 3273
- POJ 3273
- poj 3273
- poj 3273
- POJ 3273
- POJ 3273
- 【POJ】3273
- poj 3273 Monthly Expense
- poj 3273 (二分)
- poj 3273(二分。。。很水。。。。)
- poj-3273 Monthly Expense
- poj 3273 二分法
- MySQL创建方法Function错误
- 简单的下拉框二级联动
- Unity设置物体的自转和公转
- 递归获得窗体下所有子窗体信息
- 产生随机订单号
- POJ 3273
- 解密后的暴风一号病毒(1KB文件夹快捷方式病毒)
- 2016中国人工智能大会摘要
- npm install -g ionic cordova 安装失败的解决方案
- 其中 (%{WORD:x_forword}|-) |表示或的意思
- select、poll、epoll之间的区别总结[整理]
- hdu5754 2016 Multi-University Training Contest 3 Life Winner Bo 解题报告
- maven+spring-boot+springfox+swagger2markup+spring restdoc+asciidoctor生成完美的rest文档
- AES加密 - iOS与Java的同步实现