POJ-3273 Monthly Expense (最大值最小化问题)
/* Monthly Expense
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 10757 Accepted: 4390
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 USACO 2007 March Silver
*/
/*坑啊,二分法绝对不能用递归来做,应该用非递归while循环做*/
#include <iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define maxn 100100
int a[maxn],N,M,x,y,minx;
void binary(int x1)
{
if(x>y)
return ;
int sum=;
int t=;
for(int i=; i<N;)
{
if(sum+a[i]>x1)
{
t++;
sum=;
}
else
{
sum+=a[i];
if(i==N-)
t++;
i++;
}
}
int mid;
if(t<=M)
{
minx=x1;
//minx= min(x1,minx);
//if(x1>minx)
//return ;
y=x1-;
mid=(x+y)>>;
binary(mid);
}
if(t>M)
{
x=x1+;
mid=(x+y)>>;
}
binary(mid);
}
int main()
{
while(~scanf("%d%d",&N,&M))
{ y=,x=;
for(int i=; i<N; i++)
{
scanf("%d",&a[i]);
if(a[i]>x)
x=a[i];
y+=a[i];
}
minx=;
binary((x+y)>>);
printf("%d\n",minx);
}
return ;
}
//非递归做的
#include <iostream>
#include <algorithm>
using namespace std;
int money[];
int main()
{
int N;//总共的天数
int M;//分成fajomonths月数
cin>>N>>M;
int low=;
int high=;
for(int i=;i<=N;i++)
{
cin>>money[i];
high+=money[i];
if(money[i]>low)
low=money[i];
}
int mid;
while(low<=high)
{
mid=(low+high)/;
int sum=;
int duanshu=;
for(int i=;i<=N;i++)
{
sum+=money[i]; if(sum<=mid)
;//计算在月最大消费控制在mid下时能分的段数
else
{
sum=money[i];
duanshu++;
}
}
if(duanshu>M)//分的段数比需要的大,说明标准高了
low=mid+;
else if(duanshu<M)//分的段数比需要的小,说明标准低了
high=mid-;
else if(duanshu==M)//分的段数和需要的相同,这时还有可能再严格一点,即使每月的最大支出更小一点
high=mid-;
} cout<<low<<endl;
return ;
}
POJ-3273 Monthly Expense (最大值最小化问题)的更多相关文章
- poj 3273"Monthly Expense"(二分搜索+最小化最大值)
传送门 https://www.cnblogs.com/violet-acmer/p/9793209.html 题意: 有 N 天,第 i 天会有 a[ i ] 的花费: 将这 N 天分成 M 份,每 ...
- Monthly Expense(最大值最小化问题)
POJ-3273 ...
- 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)< ...
- 二分搜索 POJ 3273 Monthly Expense
题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
- [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14158 Accepted: 5697 ...
- OJ 21658::Monthly Expense(二分搜索+最小化最大值)
Description Farmer John是一个令人惊讶的会计学天才,他已经明白了他可能会花光他的钱,这些钱本来是要维持农场每个月的正常运转的.他已经计算了他以后N(1<=N< ...
- POJ 3273 Monthly Expense(二分答案)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...
随机推荐
- CUDA Samples: Ray Tracking
以下CUDA sample是分别用C++和CUDA实现的生成光线跟踪图像,并对其中使用到的CUDA函数进行了解说,code参考了<GPU高性能编程CUDA实战>一书的第六章,CUDA各实现 ...
- [Linux] Boot分区满了的处理方法 The volume "boot" has only 0 bytes disk space remaining
1.查看系统目前正在用的内核 abby@abby:~$ uname -r ..--generic 2.查看/boot保存的所有内核 abby@abby:~$ ls -lah /boot total 3 ...
- SortedList【排序列表】
C# 集合 SortedList 类代表了一系列按照键来排序的键/值对,这些键值对可以通过键和索引来访问. 排序列表是数组和哈希表的组合.它包含一个可使用键或索引访问各项的列表.如果您使用索引访问各项 ...
- Python 之文件上传
基于form表单提交 # 需要指定form-data,不能直接拼键值对 可以指定name照片存在位置 views.py from django.shortcuts import render,redi ...
- javascript的slice()与splice()方法
(1)数组和String对象都有slice()方法. //Array var list = ['A','B','C','D','DS']; console.log(list.slice(,));//截 ...
- 线性回归 Linear regression(1)线性回归的基本算法与求解
本系列内容大部分来自Standford公开课machine learning中Andrew老师的讲解,附加自己的一些理解,编程实现和学习笔记. 第一章 Linear regression 1.线性回归 ...
- Luogu P2742 模板-二维凸包
Luogu P2742 模板-二维凸包 之前写的实在是太蠢了.于是重新写了一个. 用 \(Graham\) 算法求凸包. 注意两个向量 \(a\times b>0\) 的意义是 \(b\) 在 ...
- webdriver常用函数总结
#1 创建浏览器对象 driver = webdriver.Chrome() #2 设置隐式等待10秒 driver.implicitly_wait(10) #3 最大化浏览器窗口 driver.ma ...
- LeetCode Word Abbreviation
原题链接在这里:https://leetcode.com/problems/word-abbreviation/description/ 题目: Given an array of n distinc ...
- 【java基础】java集合之HashTable,HashSet,HashMap
[一]HashSet (1)HashSet内部维护的是一个HashMap,具体原理见java集合之HashMap [二]HashTable (1)HashTable内部维护的是一个Entry的数组.E ...