题目:http://poj.org/problem?id=3273

题意:把n个数分成m份,使每份的和尽量小,输出最大的那一个的和。

思路:二分枚举最大的和,时间复杂度为O(nlog(sum-max));

一道很好的题。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = +;
int a[maxn]; int main()
{
int n, m, i, Max, sum;
while(~scanf("%d%d", &n, &m))
{
Max = -;
sum = ;
for(i = ; i < n; i++)
{
scanf("%d", &a[i]);
sum += a[i];
if(Max < a[i])
Max = a[i];
}
int high = sum, low = Max, mid;
while(high>low)
{
mid = (high+low)/;
int cou = , w = ;
for(i = ; i < n; i++)
{
w += a[i];
if(w>mid)
{
cou++; //cou记录最大值为mid的情况下,可以分成几份
w = a[i];
}
}
if(cou>m)
low = mid+;
else
high = mid-;
}
printf("%d\n", high);
}
return ;
}

以后还是用这种形式的二分吧

while(left<right)

{

if()

left=mid+1;

else right=mid;

}

代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
using namespace std;
const int maxn = +;
int a[maxn]; int main()
{
int n, m, i, Max, sum;
while(~scanf("%d%d", &n, &m))
{
Max = -;
sum = ;
for(i = ; i < n; i++)
{
scanf("%d", &a[i]);
sum += a[i];
if(Max < a[i])
Max = a[i];
}
int high = sum, low = Max, mid;
while(high > low)
{
mid = (high+low)/;
int cou = , w = ;
for(i = ; i < n; i++)
{
w += a[i];
if(w>mid)
{
cou++;
w = a[i];
}
}
if(cou <= m)
high = mid;
else
low = mid+;
}
printf("%d\n", low);
}
return ;
}

poj 3273 Monthly Expense(贪心+二分)的更多相关文章

  1. [ACM] POJ 3273 Monthly Expense (二分解决最小化最大值)

    Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14158   Accepted: 5697 ...

  2. POJ 3273 Monthly Expense 【二分答案】

    题意:给出n天的花费,需要将这n天的花费分成m组,使得每份的和尽量小,求出这个最小的和 看题目看了好久不懂题意,最后还是看了题解 二分答案,上界为这n天花费的总和,下界为这n天里面花费最多的那一天 如 ...

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

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

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

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

  5. 二分搜索 POJ 3273 Monthly Expense

    题目传送门 /* 题意:分成m个集合,使最大的集合值(求和)最小 二分搜索:二分集合大小,判断能否有m个集合. */ #include <cstdio> #include <algo ...

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

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

  7. POJ 3273 Monthly Expense 二分枚举

    题目:http://poj.org/problem?id=3273 二分枚举,据说是经典题,看了题解才做的,暂时还没有完全理解.. #include <stdio.h> #include ...

  8. poj 3273 Monthly Expense (二分)

    //最大值最小 //天数的a[i]值是固定的 不能改变顺序 # include <algorithm> # include <string.h> # include <s ...

  9. poj 3273 Monthly Expense (二分搜索,最小化最大值)

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

随机推荐

  1. ASP.NET Web - 回送

    如果希望把更改事件立即传送给服务器,可以把AutoPostback属性设置为true.这样就会使用客户端的JavaScript把窗体数据立即提交给服务器.当然,网络通信量也会增加.使用这个功能时要小心 ...

  2. HTML教程XHTML教程:HTML标记嵌套使用技巧

    网页制作Webjx文章简介:WEB标准-HTML元素嵌套. 先来看以下这样一段代码: <ul>    <li><h4><a href="" ...

  3. Matlab中cell2mat的使用

    binIndices是1*50的cell,每个cell是的1*n(n不定),那么cell2mat(binIndices)得到的是1*sum(cellfun(@length,binIndices))的行 ...

  4. jsp bean标签

    jsp中存在一个奇奇怪怪的bean标签. 例如 现在在java包中定义一个类test2 package bean; public class test { private int number; pu ...

  5. oracle 表空间 用户

    Oracle创建表空间.创建用户以及授权.查看权限 创建临时表空间 CREATE TEMPORARY TABLESPACE test_temp TEMPFILE 'C:\oracle\product\ ...

  6. winform Chart控件 获取鼠标处坐标值方法

    Chart控件本身功能强大,应用广泛,因此其属性.方法也很多.此处介绍在很多应用中需要查看鼠标位置处坐标值的一些方法 1,调用Chart事件  GetToolTip 利用ToolTipEventArg ...

  7. Calling Lua From a C Program

    Introduction From a running C program, you can call a Lua script. The C program can pass arguments t ...

  8. js和jquery获取文档对象以及滚动条位置

    <div style="width:120px;height:120px;border:1px solid red; position:absolute; left:800px; to ...

  9. hdu 4474 Yet Another Multiple Problem

    题意: 找到一个n的倍数,这个数不能含有m个后续数字中的任何一个 题解: #include<stdio.h> #include<string.h> #include<qu ...

  10. *[codility]Country network

    https://codility.com/programmers/challenges/fluorum2014 http://www.51nod.com/onlineJudge/questionCod ...