【POJ 3273】 Monthly Expense (二分)
【POJ 3273】 Monthly Expense (二分)
一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 每一个月的花费是这个月的花费加和 必须按计划表的顺序来
全部天中花费中最大花费作为下界 全部花费加和作为上界 二分上下界间的花费可能 找出最少每月花费就可以
代码例如以下:
- #include <iostream>
- #include <cstdio>
- using namespace std;
- int ned[100000],n,m;
- bool can(int x)//推断是否可行
- {
- int i,sum = 0,cnt = 1;
- for(i = 0; i < n; ++i)
- {
- sum += ned[i];
- if(sum > x)//加上这天后此划分超过x 割开
- {
- sum = ned[i];
- cnt++;
- }
- }
- if(cnt > m) return false;//划分块数>m 不可行
- return true;
- }
- int main()
- {
- //freopen("in.in","r",stdin);
- int l,r,i,mid,ans;
- scanf("%d %d",&n,&m);
- r = l = 0;
- for(i = 0; i < n; ++i)
- {
- scanf("%d",&ned[i]);
- r += ned[i];
- l = max(l,ned[i]);
- }
- while(l <= r)
- {
- mid = (l+r)>>1;
- if(can(mid)) //可按mid划分的时候 此题数据真水 開始二分写挫了 还过了 3 2 4 4 4这组就能卡掉
- {
- ans = mid;
- r = mid-1;
- }
- else l = mid+1;
- }
- printf("%d\n",ans);
- 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天里面花费最多的那一天 如 ...
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
随机推荐
- FastDFS介绍(非原创)
文章大纲 一.FastDFS介绍二.FastDFS安装与启动(Linux系统)三.Java客户端上传图片四.参考文章 一.FastDFS介绍 1. 什么是FastDFS FastDFS是用C语言编 ...
- B - Beautiful Year
Problem description It seems like the year of 2013 came only yesterday. Do you know a curious fact? ...
- 查找DLL,并复制出来
Subst b: %windir%\assembly 执行完后,会发现硬盘分区多了个B盘,打开后看到了所有assembly下的DLL,于是在这里就搜到了Microsoft.ReportViewer.P ...
- 修改 Mac 默认 PHP 运行环境
更新了自带php版本后,修改默认php环境变量 首先,创建 .bash_profile 文件 sudo nano ~/.bash_profile # 添加一行.注意 PHP5.4.10 修改成你正在运 ...
- week1 notebook1
初识Python 一.python介绍 - 解释器: cpython(默认使用) ipython(shell) jpython(java) ironpython rubypython - 编码: as ...
- 仿QQ浏览器mac版官网主页 html+css3特效
这是一款超酷的CSS3动态背景动画特效,CSS3仿QQ浏览器官网彗星动画背景特效. 在线演示本地下载
- 极光推送设置标签和别名无效的解决办法:JPush设置别名不走成功回调
极光推送设置标签和别名无效的解决办法 JPush设置别名不走成功回调的解决办法 http://www.cnblogs.com/chenqitao/p/5506023.html 主要是网络加载过快导致的 ...
- HDL之Bitwise operation
1 Verilog 1.1 Bitwise operator Bitwise operators perform a bit wise operation on two operands. They ...
- 多态&接口
多态 多态定义:允许一个父类变量引用子类的对象:允许一个接口类型引用实现类对象. 多态的调用:使用父类的变量指向子类的对象:所调用的属性和方法只限定父类中定义的属性和方法,不能调用子类中特有的属性和方 ...
- CDR是什么?CorelDRAW矢量绘图
CorelDRAW是矢量绘图软件 CorelDRAW Graphics Suite是加拿大Corel公司的平面设计软件: CorelDRAW 非凡的设计能力广泛地应用于商标设计.标志制作.模型绘制.插 ...