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. request库解析中文

    官网地址: http://cn.python-requests.org/zh_CN/latest/ 高级用法 本篇文档涵盖了 Requests 的一些高级特性. 会话对象 会话对象让你能够跨请求保持某 ...

  2. subprocess.run()用法python3.7

    def run(*popenargs, input=None, capture_output=False, timeout=None, check=False, **kwargs): "&q ...

  3. Mysql基本用法-存储引擎-03

    看到存储引擎这个地方感到很多细节比较陌生,所以总结小记一些 为了适应各种不同的运行环境,MYSQL提供了多种不同的存储引擎(Storage Engine ),在应用程序开发这个层面上,开发者可以根据不 ...

  4. NSObject类的API介绍

    这篇文章围绕的对象就是NSObject.h文件,对声明文件中的属性.方法进行必要的“翻译”. 该文件大致由两部分组成:NSObject协议和NSObject类. (一)NSObject协议 - (BO ...

  5. python 基础文件操作

    实时刷新到硬盘里 f= open('hh','w',encoding='utf8') f.write('gyftyftft') f.write('hghgh\njkkjk') f.flush()#实时 ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:显示在 <abbr> 元素中的文本以小号字体展示,且可以将小写字母转换为大写字母

    <!DOCTYPE html> <html> <head> <title>菜鸟教程(runoob.com)</title> <meta ...

  7. ES6新语法概览

    简介 ES6是JavaScript语言的新一代标准,加入了一些新的功能和语法,正式发布于2015年6月,亦称ES2015:该标准由ECMA(欧洲计算机制造联合会)的第39号技术专家委员会(TC39)制 ...

  8. redhat 7.6 安装 inotify-tools 文件监控工具 搭配rsync

    1.解压inotify-tools tar -zxvpf inotify-tools-3.14.tar.gz 2.cd 到解压的目录 3../configure  编译,然后失败,提示checking ...

  9. Codeforces1303C. Perfect Keyboard

    本题可以转换成图论来做,每两个相邻点连一条边,然后统计每一个点的degree,如果>=2说明有一个点要相邻三个点,不满足题意,然后找出每个degree<2的点,这些点可以作为一段的起点,然 ...

  10. Lamda表达式学习笔记一

    Lamda表达式学习笔记一 一.Lamda语法诠释 三傻大闹宝莱坞的主人公兰彻说的一句话让我映像深刻:用简单的语言来表达同样的意 我并不是说书上的定义怎么怎么不对,而是应该理解书本上的定义,并用简单的 ...