题目:POJ3273、洛谷P2884。

题目大意:有n个数,要分成m份,每份的和要尽可能小,求这个情况下和最大的一份的和。

解题思路:二分答案,对每个答案进行贪心判断,如果最后得出份数>m,则说明答案在[mid+1,r]内,反之在[l,mid]内。我的二分上界是所有数的总和,所以时间复杂度$O(n\log (\sum_{i=1}^{n}a[i]))$。

结果我贪心没考虑全,爆了一发O(≧口≦)O

C++ Code:

#include<cstdio>
using namespace std;
int n,m,a[100005];
long long s;
bool ok(int x){
int p=1;
long long ns=0;
for(int i=1;i<=n;++i){
if(ns+a[i]<=x)ns+=a[i];
else{
++p;
ns=a[i];
if(ns>x)return false;
}
}
return p<=m;
}
int main(){
scanf("%d%d",&n,&m);
s=0;
for(int i=1;i<=n;++i){
scanf("%d",&a[i]);
s+=a[i];
}
long long l=0,r=s,ans;
while(l<=r){
long long mid=l+r>>1;
if(ok(mid)){
ans=mid;
r=mid-1;
}else l=mid+1;
}
printf("%lld\n",ans);
return 0;
}

[USACO07MAR]每月的费用Monthly Expense的更多相关文章

  1. bzoj1639 / P2884 [USACO07MAR]每月的费用Monthly Expense

    P2884 [USACO07MAR]每月的费用Monthly Expense 二分经典题 二分每个段的限制花费,顺便统计下最大段 注意可以分空段 #include<iostream> #i ...

  2. 洛谷—— P2884 [USACO07MAR]每月的费用Monthly Expense

    https://www.luogu.org/problemnew/show/P2884 题目描述 Farmer John is an astounding accounting wizard and ...

  3. P2884 [USACO07MAR]每月的费用Monthly Expense

    题目描述 Farmer John is an astounding accounting wizard and has realized he might run out of money to ru ...

  4. POJ-3273 Monthly Expense (最大值最小化问题)

    /* Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10757 Accepted: 4390 D ...

  5. BZOJ【1639】: [Usaco2007 Mar]Monthly Expense 月度开支

    1639: [Usaco2007 Mar]Monthly Expense 月度开支 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 700  Solved: ...

  6. POJ3273 Monthly Expense —— 二分

    题目链接:http://poj.org/problem?id=3273   Monthly Expense Time Limit: 2000MS   Memory Limit: 65536K Tota ...

  7. 【POJ 3273】 Monthly Expense (二分)

    [POJ 3273] Monthly Expense (二分) 一个农民有块地 他列了个计划表 每天要花多少钱管理 但他想用m个月来管理 就想把这个计划表切割成m个月来完毕 想知道每一个月最少花费多少 ...

  8. Divide and Conquer:Monthly Expense(POJ 3273)

    Monthly Expense 题目大意:不废话,最小化最大值 还是直接套模板,不过这次要注意,是最小化最大值,而不是最大化最小值,判断的时候要注意 联动3258 #include <iostr ...

  9. Monthly Expense(二分查找)

    Monthly Expense Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 17982 Accepted: 7190 Desc ...

随机推荐

  1. BZOJ 2190 欧拉函数

    思路: 递推出来欧拉函数 搞个前缀和 sum[n-1]*2+3就是答案 假设仪仗队是从零开始的 视线能看见的地方就是gcd(x,y)=1的地方 倒过来一样 刨掉(1,1) 就是ans*2+1 再加一下 ...

  2. (转载)Android学习之Intent使用

    ndroid学习之Intent使用   1.使用显示Intent Intent intent = new Intent(FirstActivity.this,SecondActivity.class) ...

  3. (二)React简介

    React简介 2-1: React v16 (React Fiber) React比Vue更灵活 Vue更简单 2-2 开发环境搭建 如何开始:(两种方式) 1.传统方式script标签引入.js文 ...

  4. Pytorch搭建简单神经网络 Task2

    1>建立数据集(并绘制图像) # -*- coding: utf-8 -*- #demo.py import torch import torch.nn.functional as F # 主要 ...

  5. DIV+CSS布局中自适应高度的解决方法

    div乱跑问题  (文件<DIV+CSS布局中自适应高度的解决方法.rar>)   float 是个很危险的东西 得小心使用 本来有一很好的关于CSS+DIV的论坛 不过现在关门了 甚是可 ...

  6. HashMap和Hashtable的区别。

    HashMap是Hashtable的轻量级实现(非线程安全的实现),他们都完成了Map接口,主要区别在于HashMap允许空(null)键值(key),由于非线程安全,效率上可能高于Hashtable ...

  7. windows下matlab代码到ubuntu下中文注释出现乱码

    转自:https://blog.csdn.net/kouyi5627/article/details/81513329 环境:Ubuntu18.04,Matlab R2017b. 把matlab文件从 ...

  8. 'mingw32-make' 不是内部或外部命令,也不是可运行的程序 或批处理文件。(的解决方案)

    问题如上. 解决方案:找到mingw32-make,方法是在计算中搜索 然后将其复制到C:Windows\System32下,需要管理员权限才能复制的情况下直接点继续.然后就可以了.

  9. unity Android在streamingAssets路径下文件无法读取的的解决方法

    unity Android在streamingAssets路径下文件,有时候plugin下的.jar或者.so无法直接读取: 解决方法之一,拷贝至其他路径: #if UNITY_ANDROID str ...

  10. System and method for assigning a message

    A processor of a plurality of processors includes a processor core and a message manager. The messag ...