题意:给出n天的花费,需要将这n天的花费分成m组,使得每份的和尽量小,求出这个最小的和

看题目看了好久不懂题意,最后还是看了题解

二分答案,上界为这n天花费的总和,下界为这n天里面花费最多的那一天

如果mid>=m,说明mid偏小,l=mid+1,

如果mid<m,说明mid偏大,r=mid,

 #include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std; typedef long long LL;
const int INF = (<<)-;
const int mod=;
const int maxn=; int a[maxn];
int n,m,k; int ok(int v){
int ans=;
int cnt=;
for(int i=;i<=n;i++){
ans+=a[i];
if(ans>v){
ans=a[i];
cnt++;
}
}
if(cnt>=m) return ;
return ;
} int main(){
while(scanf("%d %d",&n,&m)!=EOF){
int minn=-,maxx=;
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
maxx+=a[i];
minn=max(minn,a[i]);
} LL l=minn,r=maxx,mid;
while(l<r){
mid=(l+r)/;
if(ok(mid)) r=mid;
else l=mid+;
// printf("l=%d\n",l);
// printf("r=%d\n",r);
// printf("mid=%d\n",mid);
} printf("%I64d\n",l);
}
return ;
}

POJ 3273 Monthly Expense 【二分答案】的更多相关文章

  1. POJ 3273 Monthly Expense(二分答案)

    Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36628 Accepted: 13620 Des ...

  2. POJ 3273 Monthly Expense二分查找[最小化最大值问题]

    POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...

  3. POJ 3273 Monthly Expense(二分查找+边界条件)

    POJ 3273 Monthly Expense 此题与POJ3258有点类似,一开始把判断条件写错了,wa了两次,二分查找可以有以下两种: ){ mid=(lb+ub)/; if(C(mid)< ...

  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 (二分搜索,最小化最大值)

    题目:http://poj.org/problem?id=3273 思路:通过定义一个函数bool can(int mid):=划分后最大段和小于等于mid(即划分后所有段和都小于等于mid) 这样我 ...

随机推荐

  1. C#测量程序运行时间及cpu使用时间实例方法

    private void ShowRunTime() { TimeSpan ts1 = Process.GetCurrentProcess().TotalProcessorTime; Stopwatc ...

  2. BZOJ3529: [Sdoi2014]数表 莫比乌斯反演_树状数组

    Code: #include <cstdio> #include <algorithm> #include <cstring> #define ll long lo ...

  3. Oracle查询优化之减少统计的数据量

    统计各部门人数很简单,通过部门分组即可,要统计部门以下下级部门的人数也简单,通过递归.要统计所有有下级部门的部门人数(包含下级)页比较简单, 先查询出有下级的部门,在对每个部门进行递归查询,如下: ) ...

  4. WoSign全球可信网站安全认证签章安装指南

    您购买了WoSign SSL证书后,将免费获得一个能直观地显示贵网站的认证信息的可信网站安全认证标识,能大大增强用户的在线信任,促成更多在线交易.所以,建议您在安装成功SSL证书后马上在网站的首页和其 ...

  5. 分清encodeURIComponent encodeURI 和 escape

    encodeURIComponent encodeURI escape 目的:这三个函数的作用都是让一段字符串在所有电脑(所有国家区域语言)上可读. escape对字符串进行处理: encodeURI ...

  6. bootstrap fileinput控件 + django后台上传、回显简单使用

    一.控件下载:https://github.com/kartik-v/bootstrap-fileinput/ 官网:http://plugins.krajee.com/file-input 需要引入 ...

  7. UVA10082-WERTYU(紫书例题3.2)

    A common typing error is to place the hands on the keyboard one row to the right of the correct posi ...

  8. 每日Linux命令--不完整命令

    配置文件优化,即把默认的空行还有#注释行去掉,优化前先拷贝一份配置文件 egrep -v '^$|#' 拷贝的配置文件 > 原配置文件 mysql如何修改root用户的密码 方法1: 用SET ...

  9. layui select change

    <select lay-filter="test"></select> layui.use([ 'form'], function() { var form ...

  10. Hash大法

    内容参考<算法竞赛进阶指南> 之前集训的时候听老师讲过,字符串题目中,hash一般不是正解,但是是一个优秀的暴力,可以拿比较多的部分分. hash涉及内容很多,这里只讨论字符串hash 可 ...