Monthly Expense
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 19207   Accepted: 7630

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.

题意是给出一系列数,将这些数按顺序分成M组,求每组和的最小值是多少。

枚举答案,left是一个数一个组,即最大值。right是所有数的和。

每次去和M比较来判断mid是多是少。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int N,M;
int value[100000]; int solve(int mid)
{
int i,sum=0,result=1;
if(mid==501)
i=1;
for(i=1;i<=N;i++)
{
if(value[i]+sum<=mid)
{
sum += value[i];
}
else
{
sum=value[i];
result++;
}
}
if(result>M)
{
return 1;
}
else
return -1;
} int main()
{
int i,sum,max_x;
cin>>N>>M; sum=0;
max_x=0;
for(i=1;i<=N;i++)
{
cin>>value[i];
sum += value[i];
max_x=max(max_x,value[i]);
} int mid=(sum+max_x)/2;
while(max_x<sum)
{
int temp=solve(mid);
if(solve(mid)==-1)
{
sum = mid-1;
}
else
{
max_x = mid+1;
}
mid = (max_x+sum)/2;
} cout<<mid<<endl;
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 3273:Monthly Expense 二分好题啊啊啊啊啊啊的更多相关文章

  1. POJ 3273 Monthly Expense二分查找[最小化最大值问题]

    POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...

  2. POJ 3273 Monthly Expense(二分查找+边界条件)

    POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...

  3. POJ 3273 Monthly Expense(二分答案)

    Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...

  4. POJ 3273 Monthly Expense 二分枚举

    题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...

  5. poj 3273 Monthly Expense (二分)

    //最大值最小 //天数的a[i]值是固定的 不能改变顺序 # include <algorithm> # include <string.h> # include <s ...

  6. 二分搜索 POJ 3273 Monthly Expense

    题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...

  7. [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)

    Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14158   Accepted: 5697 ...

  8. poj 3273 Monthly Expense(贪心+二分)

    题目:http://poj.org/problem?id=3273 题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和. 思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max) ...

  9. POJ 3273 Monthly Expense 【二分答案】

    题意:给出n天的花费,需要将这n天的花费分成m组,使得每份的和尽量小,求出这个最小的和 看题目看了好久不懂题意,最后还是看了题解 二分答案,上界为这n天花费的总和,下界为这n天里面花费最多的那一天 如 ...

随机推荐

  1. Nginx 反向代理报400错误解决方法!

    如果后端真是的服务器设置有类似防盗链或者根据http请求头中的host字段来进行路由或判断功能的话,如果反向代理层的nginx不重写请求头中的host字段,将会导致请求失败,报400错误,解决办法: ...

  2. 【内容摘录自 MDN】变量作用域

    有如下自定义函数:(此函数为全局函数,任何地方均可调用) function output(value) { var para = document.createElement('p'); docume ...

  3. MySQL 之单表查询

    数据库数据: # (1) 创建数据表 create table employee( id int primary key auto_increment, emp_name char(12) not n ...

  4. python3报:ImportError: No module named 'MySQLdb'

    问题描述: 项目在转到python3.6时,原先的导入MySQLdb模块都提示无法导入,pip install mysqldb也安装失败. 问题原因: python2和python3在数据库模块支持这 ...

  5. luogu P2704 炮兵阵地(经典状态压缩DP)

    方格有m*n个格子,一共有2^(m+n)种排列,很显然不能使用暴力法,因而选用动态规划求解. 求解DP问题一般有3步,即定义出一个状态 求出状态转移方程 再用算法实现.多数DP题难youguan点在于 ...

  6. Python 基础之import导包

    首先需要将import内容建立一个大概如下层级的包: 以黑色框为第一级,蓝色框为第二级,棕色框为第三级,红色框为第四级 一.import 引入初识 首先在module.py写入代码如下: xboy = ...

  7. 通过修改select的value值

    1 <input type="button" id="btn" value="按钮"/> 2 <select name=& ...

  8. Xcode10升级问题:Multiple commands produce Info.plist

    升级到Xcode10以后,编译过程遇到的第一个问题就是类似于这样的: Multiple commands produce '/Users/jiaxiaoyan/Library/Developer/Xc ...

  9. python字符串和数值操作函数大全(非常全)

    字符串和数值型数字的操作大全 1.反斜杠\的使用规则:一般使用表示续行的操作,可以其他符号相结合组成其他的一些使用符号,转义字符\‘的使用会识别引号,使得字符串中的引号和外面本来的啊引号相区分. (1 ...

  10. python中时间戳的探索

    声明 本文章只针对python3.6及以上版本. 问题提出 首先,我们先import一些必要模块: In [1]: from datetime import datetime, timezone, t ...