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个抄写员,要求分配给某个抄写员的那些书的编号必须是连续的.每个抄写员的速度是相 ...
随机推荐
- dataset的reparation和coalesce
/** * Returns a new Dataset that has exactly `numPartitions` partitions, when the fewer partitions * ...
- centos 搭建 git 服务端和客户端
centos 搭建git需要设置远程服务端和客户端.远程代码存放在服务端,多个客户端可以共享和维护服务端代码. 一.服务端主机 1.创建ssh,大部分默认已经安装,有ssh就跳过 yum instal ...
- QAM格雷码映射的规则(Gray Code Mapping in QAM)
高阶调制(QAM,MQAM)信号中做基带映射,格雷码作为一种规范的映射规则,加上I,Q方向上相邻两个星座点对应的Bit_Cluster中只有一个Bit不同,所以有方便统一的特性. 以16QAM为例,先 ...
- MQTT 协议学习:002- 通信报文的构成
背景 之前工作中参与有关协议调试的时候,发现对于协议帧的解析是比较重要的. 参考:<MQTT协议 -- 消息报文格式>.<基于STM32实现MQTT>.<MQTT协议从服 ...
- TCP为什么三次握手四次挥手
TCP概述: 它提供面向连接的服务,在传送数据之前必须先建立连接,数据传送完成后要释放连接.因此TCP是一种可靠的的运输服务,也正因为这样,不可避免的增加了许多额外的开销,比如确认,流量控制等.对应的 ...
- JS - 判断字符串某个下标的值
<html><body> <script type="text/javascript"> var str="0123456789!&q ...
- Ubuntu用sudo apt-get update出错:E: Problem executing scripts APT::Update::Post-Invoke-Success
Ubuntu用sudo apt-get update出错: E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /u ...
- Linux-Journal
Linux-Journal 1. 日志简介 2. 日志的优先级和分类 2.1 优先级 2.2 设施分类 3. 命令帮助 4. 日志查看示例 5. 日志大小限制 6. 手动清理日志文件 1. 日志简介 ...
- java文件的上传
文件的上传和下载在web应用中是非常常用,也是非常有用的功能. 例如:发送电子邮件时可以同过上传附件发送文件,OA系统中可以通过上传文件来提交公文,社交网站通过上传图片来自定义头像等等. 例如:下 ...
- Day4 - J - Rank of Tetris HDU - 1811
自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他将制作一个全球Tetris高手排行榜, ...