POJ:3273-Monthly Expense
Monthly Expense
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 32067 Accepted: 12081
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.
解题心得:
1. 题意就是给你一个长度为N的序列,现在要让你把他们切割成M份(所以每一份都是连续的),然后每一份都有一个和sum[i],其中最大的一个是maxSum = max(sum[i]),问这个最大值最小是多少?
2. 最大值最小的问题,二分啊,每次二分枚举然后检查逐步缩小范围。
#include <algorithm>
#include <stdio.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+100;
int n,m,num[maxn];
void init() {
for(int i=0;i<n;i++)
scanf("%d",&num[i]);
}
bool checke(ll sum) {
ll temp = 0,cnt = 1;
for(int i=0;i<n;i++) {
if(num[i] > sum)
return false;
if(temp + num[i] > sum) {
temp = num[i];
cnt++;
} else
temp += num[i];
}
if(cnt <= m)
return true;
return false;
}
ll binary_search() {
ll l = 0,r = 1e9+100;
while(r-l > 1) {
ll mid = (l + r) / 2;
if(checke(mid))
r = mid;
else
l = mid;
}
return r;
}
int main() {
while(scanf("%d%d",&n,&m) != EOF ){
init();
ll ans = binary_search();
printf("%lld\n",ans);
}
return 0;
}
POJ:3273-Monthly Expense的更多相关文章
- 二分搜索 POJ 3273 Monthly Expense
题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
- 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 ...
- POJ 3273 Monthly Expense(二分答案)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...
- poj 3273 Monthly Expense(贪心+二分)
题目:http://poj.org/problem?id=3273 题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和. 思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max) ...
- POJ 3273 Monthly Expense 二分枚举
题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
- POJ 3273 Monthly Expense(二分搜索)
Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...
- POJ 3273 Monthly Expense
传送门 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John is an astounding accounting wiza ...
随机推荐
- Sass基础(一)
一.Sass语法规范 1.Sass老版本:通过tab键严格控制缩进:不带有任何分号和大括号:以“.sass”为扩展名. 2.Sass新语法格式(Scss):外形和css无差,以“.scss”为扩展名. ...
- python复习目录
目录 一.计算机基础 二.python 2.1初始python 2.1 python基础1 2.2 python基础2 三.函数 3.1 函数进阶 3.2 函数之装饰器 3.3 递归函数之二分查找 3 ...
- css float布局--右侧上部固定下部自适应大小
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 如何利用PHP语言压缩图片?PHP入门教程
PHP可以控制缩略图清晰度和缩略图之后产生音量的产生.下面我们就来看看如何使用PHP优化我们的压缩图像. PHP应用程序的开发往往涉及生成缩略图,使用PHP生成缩略图的过程本身并不难,但你知道PHP ...
- Java I/O 工作机制(一) —— Java 的 I/O 类库的基本架构
Java 的 I/O 类库的基本架构 Java 的 I/O 操作类在包 java.io 下,有将近 80 个类. 按数据格式分类: 面向字节(Byte)操作的 I/O 接口:InputStream 和 ...
- MAC读取希捷移动硬盘ntfs
希捷提供了mac读取ntfs磁盘的软件,Paragon. 搜索关键词 "希捷" "mac" 或者通过以下链接进入 https://www.seagate.com ...
- 【2017-06-20】Linux应用开发工程师C/C++面试问题记录之一:Linux多线程程序的同步问题
参考之一:Linux 线程同步的三种方法 链接地址:http://www.cnblogs.com/eleclsc/p/5838790.html 简要回答: Linux下线程同步最常用的三种方法就是互斥 ...
- 会说话的ABAP report
report z. INCLUDE ole2incl. DATA: ole TYPE ole2_object, voice TYPE ole2_object, text ...
- POJ-2377 Bad Cowtractors---最大生成树
题目链接: https://vjudge.net/problem/POJ-2377 题目大意: 给一个图,求最大生成树权值,如果不连通输出-1 思路: kruskal算法变形,sort按边从大到小排序 ...
- POJ-1469 COURSES---二分图最大匹配--匈牙利算法
题目链接: https://vjudge.net/problem/POJ-1469 题目大意: 给你p门课程和n个学生,一个学生可以选0门,1门,或者多门课程,现在要求一个由p个学生组成的集合,满足下 ...