D - WE POJ - 3273 (二分法)
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
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f3;
int v[],n,m,w[];
int searchs(int low,int high)
{
int maxn=;
while(low<=high)
{
// printf("**%d**%d\n",low,high);
int mid=(low+high)>>;
// int temp=1;
// w[1]=v[1];
// for(int i=2;i<=n;++i)
// {
// w[i]=v[i];
// if(w[i]+w[i-1]<=mid)
// {
// w[i]+=w[i-1];
// }
// else
// {
// ++temp;
// }
// }
int temp = ,sum = ;
for(int i=; i<=n; i++)
{
if(sum+v[i] <= mid)
{
sum += v[i];
}
else
{
temp++;
sum = v[i];
}
}
if(temp==m)
{
// printf("%d*%d\n",mid,temp);
maxn=mid;
high=mid-;
}
else if(temp>m)
{
// printf("%d**%d\n",mid,temp);
low=mid+;
}
else high=mid-,maxn=mid;//printf("%d***%d\n",mid,temp);;
}
return maxn;
}
int main()
{ scanf("%d%d",&n,&m);
int a=,b=;
for(int i=; i<=n; ++i)
{
scanf("%d",&v[i]);
a+=v[i];
b=max(b,v[i]); //这里可不能在b中存输入数据的最小值,因为题目是要让
} //我们求分组后的最大值,你这样可能求出来的值小于输入的最大值
int mid=,temp=; //因为当mid<v[i]的时候temp仅仅加了一,但是这并不符合题意
int pow=searchs(b,a);
printf("%d\n",pow);
}
D - WE POJ - 3273 (二分法)的更多相关文章
- POJ 3273 Monthly Expense(二分查找+边界条件)
POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...
- 二分搜索 POJ 3273 Monthly Expense
题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...
- 【POJ 3273】 Monthly Expense (二分)
[POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...
- POJ 3273 Monthly Expense二分查找[最小化最大值问题]
POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...
- poj 3273 Monthly Expense(贪心+二分)
题目:http://poj.org/problem?id=3273 题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和. 思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max) ...
- 补充一下我对 POJ 3273 的理解,这肯定是我一生写的最多的题解。。。
题目:http://poj.org/problem?id=3273 当分成的组数越多,所有组的最大值就会越小或不变,这一点不难证明: 如果当前分成了group组,最大值是max,那么max的这一 ...
- POJ 3273 Monthly Expense 二分枚举
题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...
- poj 3273 Monthly Expense (二分搜索,最小化最大值)
题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...
- POJ 3273 Monthly Expense(二分搜索)
Description Farmer John is an astounding accounting wizard and has realized he might run out of mone ...
随机推荐
- 最长公共子序列(POJ1458)
给出两个字符串,求出这样的一个最长的公共子序列的长度:子序列中的每个字符都能在两个原串中找到,而且每个字符的先后顺序和原串中的先后顺序一致. Sample Input: abcfbc abfcabpr ...
- Unity插件系列之二维码
1.二维码常见的生成与识别途径 1.草料二维码 https://cli.im/text 2.在软件中实现生成和扫描二维码 使用zxing实现 zxing是一个用java写的开源项目,zxing.net ...
- U68364 _GC滑迷宫
题目背景 _GC买了一双蔡徐坤一代. 题目描述 _GC进入了一个n*m的迷宫.本题的特殊之处在于,_GC只能滑着走.具体来说就是,选定一个方向后,_GC会一直向该方向滑,直到撞到墙.会给出_GC的起始 ...
- [Ynoi2019模拟赛]Yuno loves sqrt technology I
题目描述 给你一个长为n的排列,m次询问,每次查询一个区间的逆序对数,强制在线. 题解 MD不卡了..TMD一点都卡不动. 强制在线的话也没啥好一点的方法,只能分块预处理了. 对于每个块,我们设lef ...
- 算法-动态规划 Dynamic Programming--从菜鸟到老鸟
算法-动态规划 Dynamic Programming--从菜鸟到老鸟 版权声明:本文为博主原创文章,转载请标明出处. https://blog.csdn.net/u013309870/ar ...
- (模拟 打好基础)nyoj1363-计划日
1363-计划日 内存限制:256MB 时间限制:3000ms 特判: No通过数:21 提交数:79 难度:1 题目描述: 为什么花那么多时间.精力还是学不好学不通,如何把握各科目的重难点,期中和期 ...
- Ansible-基础
Ansible架构 Inventory 主机清单,可以对主机分组 ansible-hoc ansible的命令,适用临时场景 ansible-playbook ansible是一个场景的集 ...
- CTR预估中GBDT与LR融合方案(转载)
1.背景 CTR预估,广告点击率(Click-Through Rate Prediction)是互联网计算广告中的关键环节,预估准确性直接影响公司广告收入.CTR预估中用的最多的模型是LR(Logis ...
- 全排列递归算法(元素有重复与无重复,C++实现)
元素无重复: 如:2,5,8,9. 思路:用递归的方法解决,对于2589,先输出所有以2开头的排列,然后输出5开头的排列.....(此处称为递归操作A).以2开头的排列中,第一位是2,后面的是589, ...
- springSecurity有毒啊啊啊啊啊啊啊
心累 的一天呀 昨天跑的demo 怎么都不拦截 我服: 第二天 又重新 建立 了个 重新做了 一遍 就神奇的 拦击了 我 ......有毒 我哩个去..抓狂 !!!!! <吐血总结,好想死&g ...