POJ 3273: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
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
题意是给出一系列数,将这些数按顺序分成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 二分好题啊啊啊啊啊啊的更多相关文章
- 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(二分答案)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...
- POJ 3273 Monthly Expense 二分枚举
题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...
- poj 3273 Monthly Expense (二分)
//最大值最小 //天数的a[i]值是固定的 不能改变顺序 # include <algorithm> # include <string.h> # include <s ...
- 二分搜索 POJ 3273 Monthly Expense
题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...
- [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)
Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14158 Accepted: 5697 ...
- poj 3273 Monthly Expense(贪心+二分)
题目:http://poj.org/problem?id=3273 题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和. 思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max) ...
- POJ 3273 Monthly Expense 【二分答案】
题意:给出n天的花费,需要将这n天的花费分成m组,使得每份的和尽量小,求出这个最小的和 看题目看了好久不懂题意,最后还是看了题解 二分答案,上界为这n天花费的总和,下界为这n天里面花费最多的那一天 如 ...
随机推荐
- mysql服务删除成功,依然存在
重启电脑, 这可能是缓存, mysqld remove 删除后,偶尔,mysql服务依然存在,重启电脑,解决,
- python学习 —— post请求方法的应用
声明:本篇仅基于兴趣以及技术研究而对B站曾经发生过的抢楼事件背后相关技术原理进行研究而写.请不要将其作为私利而对B站以及B站用户体验造成影响!谢谢合作!若本文对B站及其用户带来困扰,请联系本人删除本文 ...
- rem布局,在用户调整手机字体大小/用户调整浏览器字体大小后,布局错乱问题
一.用户调整浏览器字体大小,影响的是从浏览器打开的web页. 浏览器设置字体大小,影响浏览器打开的页面.通过js可控制用户修改字体大小,使页面不受影响. (function(doc, win) { / ...
- 【剑指Offer面试编程题】 题目1350:二叉树的深度--九度OJ
题目描述: 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 输入: 第一行输入有n,n表示结点数,结点号从1到n.根结点为1. ...
- 列表推导式、生成器表达式以及zip()max()max()/min()sum()sort()map()filter()的用法
列表推导式: 基本格式: variable = [out_exp_res for out_exp in input_list if out_exp == 2] #out_exp_res: 列表生成元素 ...
- centos7一步一步搭建docker nginx 及重点讲解
系统环境:centos7.7 (VMware中) images版本:nginx:latest (截止2020.01.10最新版) 1.拉取镜像 docker pull nginx 2.启动nginx容 ...
- java中读取文本文件的时候@Test方法中没有中文乱码,但是@Controller中却有中文乱码
今天遇到了一个非常牛逼的BUG,在@Test标注的测试类中没有中文乱码,但是访问@Controller标注的接口却报中文乱码,找了一系列资料以及各种尝试之后,发现是因为eclipse编辑工具字符编码的 ...
- 任意promise串行执行算法 - 童彪
// 任意promise串行执行算法 - 童彪 function runAllPromise() { var p1 = new Promise((resove, reject) => { s ...
- 1.requests+正则表达式爬猫眼电影TOP100
import requests from requests.exceptions import RequestException def get_one_page(url):try: response ...
- CSS - 布局流程
一.为了提高网页制作的效率,布局时通常需要遵守一定的布局流程,具体如下: "版心"(可视区) 是指网页中主体内容所在的区域.一般在浏览器窗口中水平居中显示,常见的宽度值为960px ...