poj3273——典型的二分答案题目
来源:互联网 发布:淘宝贷款拖欠还款 编辑:程序博客网 时间:2024/05/08 12:54
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 5100400300100500101400
Sample Output
500
Hint
题意:给出n个数,要求划分成m份,求每一份的总和的最大值的最小值,即最小化最大值,明显二分,即使二分不是很会,但是水水还是过了。
题解:即二分,二分答案的题目,首先必须有个判断答案是否正确的函数,下面代码为can(),然后在最小值答案和最大值答案之间进行二分,找到最小的答案,复杂度O(nlogn)。(最小值答案很明显就是分成n份的答案,最大值答案就是分成一份的答案)。
代码:
#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <queue>#include <sstream>#include <fstream>#include <set>#include <map>#define INF 1e9#define M_PI 3.14159265358979323846using namespace std;int n,m;int s[100005];bool can(int a)//判断函数,判断最小值为a时能否分为m份{ int sum = 0,cnt = 0; for (int i = 0;i < n;i++) { sum += s[i]; if(sum>a) { sum = s[i]; cnt++; } } if(cnt+1>m)//当cnt+1比m大时,说明无法分成m份
return false; else return true;}int main(){ //infile.open("1234.txt",ios::in); while (cin>>n>>m) { int mos = 0,sum = 0; for (int i = 0;i < n;i++){ cin>>s[i]; mos = max(mos,s[i]); sum += s[i]; } int lb = mos,ub = sum; while(ub-lb>1)//二分查找 { int mid = (ub+lb)/2; if(can(mid)){ ub = mid; } else{ lb = mid+1; } } if(can(lb)){ cout<<lb<<endl; } else cout<<lb+1<<endl; } return 0;}
- poj3273——典型的二分答案题目
- poj3273-Monthly Expense典型的二分题目
- poj3273 poj3258 二分答案
- poj3273——Monthly Expense(二分)
- POJ3273 Monthly Expense(二分答案)
- Monthly Expense+POJ+典型的二分答案
- poj3273 二分
- POJ3273 二分
- 【POJ3273】二分
- poj3273 二分,以及关于二分的一些思考
- poj3273 二分搜索
- POJ3273:Monthly Expense(二分)
- poj3273+poj3258 二分易错点
- poj3273 二分枚举
- POJ3273:Monthly Expense(二分)
- poj3273(二分)
- poj3273--Monthly Expense(二分)
- poj3273 Monthly Expense(二分)
- Linux-C[1]: Is linux using big-endian or little endian ?
- R语言循环添加列表元素
- LeetCode 461 191 477
- Java虚拟机对象创建管理与垃圾回收知识整理与分析--Jvm虚拟机篇
- 322. Coin Change
- poj3273——典型的二分答案题目
- 语言模型(N-Gram)
- 读《第一行代码》时遇到的问题——第4章 FragmentBestPractice
- MELP算法的Python实现(一)
- 利用de Casteljau算法绘制Bezier曲线
- Visual Studio 2015 解决方案资源管理器
- 想念你的时候
- Scala 赋值语句
- hibernate向Mysql中插入中文乱码的问题