POJ-3273
                                                                                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

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.

Source

 
还是显神的指点,今天告诉我有最大值最小化这么个东西
总结一下最大值最小化的思路
其实觉得这种算法蛮有现实意义的,给一堆乱七八糟的东西分堆,分出来的每堆都不超过一个固定的数值,这是要有技术含量的。
用到二分我也觉得一开始没想到,现在觉得确实用二分挺合理的
1.即首先求出二分的上下限,这个是每次二分必做的准备工作,上限即为这一堆东西的总量,下限即单个最大的物品的值。
2.有上下限之后即开始二分,最难写的部分就出来了,即判断当前分堆是否合理,在判断分堆是否合理中,主要的限制因素为两个,一个是单堆的量肯定不能超过当前的Mid值,另一个是分出的堆数一定不能超过题目要求的M值
3.由判断条件即可将二分范围进行缩小,即,一旦当前Mid值满足分堆要求,意味着我还可以把Mid值缩小,即把二分的right=mid,如果当前Mid值触犯了判断条件,即说明当前值还太小,即把left=mid。
4.由以上二分return出来的结果即为所求值
 
#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(最大值最小化问题)的更多相关文章

  1. POJ-3273 Monthly Expense (最大值最小化问题)

    /* Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10757 Accepted: 4390 D ...

  2. OJ 21658::Monthly Expense(二分搜索+最小化最大值)

        Description Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的.他已经计算了他以后N(1<=N< ...

  3. poj 3273"Monthly Expense"(二分搜索+最小化最大值)

    传送门 https://www.cnblogs.com/violet-acmer/p/9793209.html 题意: 有 N 天,第 i 天会有 a[ i ] 的花费: 将这 N 天分成 M 份,每 ...

  4. 九度OJ 1502 最大值最小化(JAVA)

    题目1502:最大值最小化(二分答案) 九度OJ Java import java.util.Scanner; public class Main { public static int max(in ...

  5. BUAA1389愤怒的DZY(最大值最小化)

    http://acm.buaa.edu.cn/problem/1389/ 愤怒的DZY[问题描述]“愤怒的小鸟”如今已经是家喻户晓的游戏了,机智的WJC最近发明了一个类似的新游戏:“愤怒的DZY”.游 ...

  6. UVA 714 Copying Books 最大值最小化问题 (贪心 + 二分)

      Copying Books  Before the invention of book-printing, it was very hard to make a copy of a book. A ...

  7. hdu 4004 最大值最小化

    http://acm.hdu.edu.cn/showproblem.php?pid=4004 一条线段长度为L,线段上有n个点,最多选取 m-1 个点,使得包括线段端点在内的相邻点之间的最大距离值最小 ...

  8. Crowd Control(输出不在最大值最小化的最短路上的边)

    题意: 就是求完最大值最小化  然后输出在这条最大值最小化的最短路上的点的不在最短路上的边,emm.... 解析: 很明显,先套spfa最大值最小化模板,emm... 在更新d的时候 用一个pre去记 ...

  9. UVa 714 Copying books 贪心+二分 最大值最小化

    题目大意: 要抄N本书,编号为1,2,3...N, 每本书有1<=x<=10000000页, 把这些书分配给K个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的.每个抄写员的速度是相 ...

随机推荐

  1. Lesson 45 Of men and galaxies

    In man's early days, competition with other creatures must have been critical. But this phase of our ...

  2. 手把手教你如何在Presentation中拿高分

    掐指一算,留学生们最近应该马上遇到Presentation作业.一般来说,这类的任务会占最终成绩的20-30%,对于期末成绩有一定的影响,如果想拿高分,就需要好好的准备.所以本文算是系列里的第一篇(扫 ...

  3. 《ES6标准入门》(阮一峰)--12.Symbol

    1.概述 ES5 的对象属性名都是字符串,这容易造成属性名的冲突.比如,你使用了一个他人提供的对象,但又想为这个对象添加新的方法(mixin 模式),新方法的名字就有可能与现有方法产生冲突.如果有一种 ...

  4. Emergency

    题意:有N个点,M条边,每个点有权值,问从起点到终点最短路的个数以及权值最大的最短路的权值. 分析:修改Dijstra模板. #include<bits/stdc++.h> using n ...

  5. windows下安装多台mysql数据库且实现主从复制

    版本如下: windows server 2012 R2 mysql server 5.7.25安装版 / mysql server 5.7.25 解压版 * 这里为啥还要有安装版和解压版勒,主要是因 ...

  6. MySQL 错误代码

    常见: 1005:创建表失败 1006:创建数据库失败 1007:数据库已存在,创建数据库失败 1008:数据库不存在,删除数据库失败 1009:不能删除数据库文件导致删除数据库失败 1010:不能删 ...

  7. python练习题4

    1.将字符串“老男孩”转换成UTF-8编码的字节类型 s = "老男孩" bytes(s,'utf8') s.encode('utf8') 2.简述globals(),locals ...

  8. Selenium -- ActionChains().move_by_offset() 卡顿的解决方法

    测试运行时间 运行时间 发现每次0.5秒,此时需要修改默认的时间 打开Python安装目录下的Lib\site-packages\selenium\webdriver\common\actions\p ...

  9. json字符串转java对象,json中字段名称与对象属性名称不一致

    json字符串转java对象,json字段名称与对象属性名称不一致可以在对象属性上添加注解@SerializedName解决

  10. leetcode1162 As Far from Land as Possible

    """ Given an N x N grid containing only values and , represents water and represents ...