题意:给出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#操作sql时注意点

    ①创建必要的索引 ②使用预编译查询 ③使用参数化sql会执行预编译,第一次执行的时候DBMS会为这个SQL语句进行查询优化并执行预编译 ④调整where子句中的连接顺序 ⑤DBMS一般次用自上而下的顺 ...

  2. 微信小程序 | canvas绘图

    1.新的尺寸单位 rpx rpx(responsive pixel): 可以根据屏幕宽度进行自适应. 规定屏幕宽为750rpx.如在 iPhone6 上,屏幕宽度为375px,共有750个物理像素,则 ...

  3. paratest

    class Program { static void Main(string[] args) { long result = 0; Stopwatch Watch = new Stopwatch() ...

  4. Git 版本控制原理

    git 工作原理图 如上图所示,有三个区域Working Directory.stage.master. 名词解释: 工作区(Working Directory) 在我们直接编辑文件(文件夹)的根目录 ...

  5. LeetCode Golang 单向链表相加 反向实现

    LeetCode 两数之和, 反向实现 1 -> 2 -> 3 -> 4 +                  3 -> 4 ------------------------- ...

  6. (转)JobTracker和TaskTracker概述

    一 概述: (1)Hadoop MapReduce采用Master/Slave结构. *Master:是整个集群的唯一的全局管理者,功能包括:作业管理.状态监控和任务调度等,即MapReduce中的J ...

  7. 一些html5

    ---匿名函数(funcation(){}())---- 一.拖拽 draggable=ture-----A拖动元素上事件 1. 拖拽开始:ondragstart2. 拖拽中:ondrag3. 拖拽结 ...

  8. Set&Map区别Array

    Set&Map区别Array 在Set内部,两个NaN是相等.两个对象总是不相等的.可以用length来检测 四个操作方法: add(value):添加某个值,返回Set结构本身. delet ...

  9. PL/SQL基本语法

    *2.PL/SQL基本语法   1)匿名块   一段不能在数据库存储的PL/SQL. 基本结构如下:   DECLARE     //变量的声明定义区域(可省略)   BEGIN     //业务处理 ...

  10. angular-基础

    AngularJs特点: 1.依赖注入 2.模块化 3.双向绑定 4.语义化标签 当网页加载完毕,AngularJS 自动开启. ng-app 指令告诉 AngularJS,<div> 元 ...