Monthly Expense(最大值最小化问题)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 10557 | Accepted: 4311 |
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 5
100
400
300
100
500
101
400
Sample Output
500
Hint
Source
#include <iostream>
#include <cstdio>
#define maxn 100005
using namespace std;
int cost[maxn];
int n,m;
bool judge(int x)//用来判断按当前二分值作为题目要求的最大值,所分出的堆是否合理。
{
int s=0,t=0;
for (int i=0;i<n;i++)
{
if (cost[i]>x) return false;
if (s+cost[i]>x)
{
if (t>=m-1) return false;
t++;
s=cost[i];
}
else
s+=cost[i];
}
return true;
}
int binary(int max,int sum) //二分部分
{
int mid,left=max,right=sum;
while (left<right)
{
mid=left+(right-left)/2;
if (!judge(mid)) left=mid+1;
else
right=mid;
}
return left;
}
int main()
{
int max=0;//二分的下界
int sum=0;//二分的上界
scanf("%d%d",&n,&m);
for (int i=0;i<n;i++)
{
scanf("%d",&cost[i]);
max=max>cost[i]?max:cost[i];
sum+=cost[i];
}
printf("%d\n",binary(max,sum));
return 0;
}
此外,提醒一下,POJ上的这道题目如果用cin cout会TLE,估计是数据量的问题,建议用C的输入输出
Monthly Expense(最大值最小化问题)的更多相关文章
- POJ-3273 Monthly Expense (最大值最小化问题)
/* Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10757 Accepted: 4390 D ...
- OJ 21658::Monthly Expense(二分搜索+最小化最大值)
Description Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的.他已经计算了他以后N(1<=N< ...
- poj 3273"Monthly Expense"(二分搜索+最小化最大值)
传送门 https://www.cnblogs.com/violet-acmer/p/9793209.html 题意: 有 N 天,第 i 天会有 a[ i ] 的花费: 将这 N 天分成 M 份,每 ...
- 九度OJ 1502 最大值最小化(JAVA)
题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...
- BUAA1389愤怒的DZY(最大值最小化)
http://acm.buaa.edu.cn/problem/1389/ 愤怒的DZY[问题描述]“愤怒的小鸟”如今已经是家喻户晓的游戏了,机智的WJC最近发明了一个类似的新游戏:“愤怒的DZY”.游 ...
- UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)
Copying Books Before the invention of book-printing, it was very hard to make a copy of a book. A ...
- hdu 4004 最大值最小化
http://acm.hdu.edu.cn/showproblem.php?pid=4004 一条线段长度为L,线段上有n个点,最多选取 m-1 个点,使得包括线段端点在内的相邻点之间的最大距离值最小 ...
- Crowd Control(输出不在最大值最小化的最短路上的边)
题意: 就是求完最大值最小化 然后输出在这条最大值最小化的最短路上的点的不在最短路上的边,emm.... 解析: 很明显,先套spfa最大值最小化模板,emm... 在更新d的时候 用一个pre去记 ...
- UVa 714 Copying books 贪心+二分 最大值最小化
题目大意: 要抄N本书,编号为1,2,3...N, 每本书有1<=x<=10000000页, 把这些书分配给K个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的.每个抄写员的速度是相 ...
随机推荐
- js 对象补充
js 对象 普通对象与对象简写 js 没有字典类型,只有对象类型,对象可以完全替代字典来使用 js 中对象的属性名,都采用字符串类型,所以就可以省略字符串的引号标识 对象中属性值为函数时,称之为方法, ...
- 5G时代能携号转网,你会提前换新手机吗?
每一次网络制式的变革,总能引发业界和大众的强烈关注.毕竟网络制式的迭代,能为社会的方方面面带来极大嬗变.而5G时代即将开启,必然会出现更多弄潮儿,也会带来让人心动的美好生活.不过,鉴于5G本身的特殊性 ...
- Metasploit学习笔记——客户端渗透攻击
1.浏览器渗透攻击实例——MS11-050安全漏洞 示例代码如下 msf > use windows/browser/ms11_050_mshtml_cobjectelement msf exp ...
- 118.django中表单的使用方式
表单 HTML中的表单: 从前端来说,表单就是用来将数据提交给服务器的,不管后台使用的是django还是php等其他的语言.只要把input标签放在form标签中,然后再添加一个提交的按钮,就可以将i ...
- TP5分页函数paginate中的each()传参
在使用each()时,each()里面只能使用局部变量,当使用外部变量时会报未定义变量的错误,但是有时候我们还必须使用外部变量,就需要想是时作用域的问题,但是如果使用 global 全局虽然不报错,但 ...
- jquery动态选中radio,获取radio选中值
//动态选中radio值,1:表示radio的name 2:表示后台传过来的radio值$(":radio[name='1'][value='" + 2 + "']&qu ...
- ①java环境变量配置以及简单的dos框操作
DOS:英文全称Disk Operation System,即磁盘操作系统,是个人电脑上使用的一种操作系统,并曾是世界上最广泛使用的操作系统. JDK: 英文全称Java Development Ki ...
- C# enable为false时不变颜色
[System.Runtime.InteropServices.DllImport("user32.dll ")] public static ex ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 辅助类:屏幕阅读器
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- docker-compose(grafana influxdb) + telegraf 快速搭建简单监控
灵活实现方案: 1: telegraf 为go 语言写得占用内存小 收集主机各项监控数据 定时写入 时序DB influxdb ------------------------&qu ...